سؤال

I use logger in my application and direct the output to a file like this:

Logger.new("nemobuild.log")

My logger appends to this logfile once its created.

What i want it to do is to clear the logfile on each program start.

In the examples is a description for creating a new logfile:

file = File.open('foo.log', File::WRONLY | File::APPEND | File::CREAT)

What File:: params would i have to use to get the desired behaivior?

هل كانت مفيدة؟

المحلول 2

I ended up deleting the file before setting up the logger:

File.delete("nemobuild.log")
logger = Logger.new("nemobuild.log")

نصائح أخرى

Which gem do you use? Can you provide a MWE?

With Logger.new("nemobuild.log")

I get the error:

`<main>': uninitialized constant Logger (NameError)

Do you use the standard Logger?

Then you can use a file object instead a filename for the logfile:

require 'logger'
log = Logger.new(File.new("nemobuild.log",'w'))
log.error('XX')

If you want back the append-mode, you can switch the w-option (write) to a (append):

log = Logger.new(File.new("nemobuild.log",'a'))

Assuming you can/want use log4r:

The feature you need is an option trunc of the FileOutputter:

require 'log4r' 

log = Log4r::Logger.new('log')
log.outputters << Log4r::FileOutputter.new(
    'log_file', :filename => 'mini_example.log', 
    :trunc => true, #Delete old log
    )
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top