Creating only one log every day using Ruby standard Logger -
i'm using ruby standard logger, want rotational daily one, in code have :
logger.new("#{$root_path}/log/errors.log", 'daily')
it working perfectly, created 2 files errors.log.20130217
, errors.log.20130217.1
.
how can forcefulness create 1 file day ?
your code right long-running application.
what's happening you're running code more 1 time on given day.
the first time run it, ruby creates log file "errors.log".
when day changes, ruby renames file "errors.log.20130217".
but somehow ran code again, perhaps you're running 2 apps (or processes, or workers, or threads) utilize similar code, , logger saw file name "errors.log.20130217" existed.
your logger didn't want clobber file, still needed rename "errors.log" date, logger instead created different file name "errors.log.20130217.1"
to solve this, run code once.
if you're running multiple apps called "foo" , "bar" utilize log file names "foo-errors.log" , "bar-errors.log". or if you're using multiple workers, give each worker own log file name (e.g. using worker's process id, or worker pool array index, or you're keeping track of workers).
if want solve using ruby logger, you'll need override logger #shift_log_period doesn't take ".1" suffix. subclass logger , write worn #shift_log_period observe there existing log file date, , if so, utilize instead of doing file rename.
this code causing logger:
def shift_log_period(period_end) postfix = period_end.strftime("%y%m%d") # yyyymmdd age_file = "#{@filename}.#{postfix}" if filetest.exist?(age_file) # seek avoid filename crash caused timestamp change. idx = 0 # .99 can overridden; avoid much file search 'loop do' while idx < 100 idx += 1 age_file = "#{@filename}.#{postfix}.#{idx}" break unless filetest.exist?(age_file) end end @dev.close rescue nil file.rename("#{@filename}", age_file) @dev = create_logfile(@filename) homecoming true
there no solution (afaik) using ruby logger, built-in rotator, manage logs written multiple apps (a.k.a. workers, processes, threads) simultaneously. because each of apps gets own log file handle.
alternatively, utilize of log rotator tools, such logrotate suggested tin man user in question comments: http://linuxcommand.org/man_pages/logrotate8.html
in general, logrotate best bet imho.
ruby-on-rails ruby logging
No comments:
Post a Comment