| Class | Logger::LogDevice |
| In: |
lib/logger.rb
|
| Parent: | Object |
LogDevice — Logging device.
| SiD | = | 24 * 60 * 60 |
| dev | [R] | |
| filename | [R] |
Logger::LogDev.new(name, :shift_age => 'daily|weekly|monthly') Logger::LogDev.new(name, :shift_age => 10, :shift_size => 1024*1024)
| name: | A String (representing a filename) or an IO object (actually, anything that responds to write and close). If a filename is given, then that file is opened for writing (and appending if it already exists), with sync set to true. |
| opts: | Contains optional arguments for rolling ("shifting") the log
file. :shift_age is either a description (e.g.
‘daily’), or an integer number of log files to keep.
shift_size is the maximum size of the log file, and is only
significant is a number is given for shift_age.
These arguments are only relevant if a filename is provided for the first argument. |
Creates a LogDevice object, which is the target for log messages. Rolling of log files is supported (only if a filename is given; you can’t roll an IO object). The beginning of each file created by this class is tagged with a header message.
This class is unlikely to be used directly; it is a backend for Logger.
# File lib/logger.rb, line 492
492: def initialize(log = nil, opt = {})
493: @dev = @filename = @shift_age = @shift_size = nil
494: if log.respond_to?(:write) and log.respond_to?(:close)
495: @dev = log
496: else
497: @dev = open_logfile(log)
498: @dev.sync = true
499: @filename = log
500: @shift_age = opt[:shift_age] || 7
501: @shift_size = opt[:shift_size] || 1048576
502: end
503: end
Log a message. If needed, the log file is rolled and the new file is prepared. Log device is not locked. Append open does not need to lock file but on an OS which supports multi I/O, records could possibly be mixed.
# File lib/logger.rb, line 511
511: def write(message)
512: if shift_log?
513: begin
514: shift_log
515: rescue
516: raise Logger::ShiftingError.new("Shifting failed. #{$!}")
517: end
518: end
519:
520: @dev.write(message)
521: end
# File lib/logger.rb, line 547
547: def add_log_header(file)
548: file.write(
549: "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
550: )
551: end
# File lib/logger.rb, line 540
540: def create_logfile(filename)
541: logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
542: logdev.sync = true
543: add_log_header(logdev)
544: logdev
545: end
# File lib/logger.rb, line 615
615: def eod(t)
616: Time.mktime(t.year, t.month, t.mday, 23, 59, 59)
617: end
# File lib/logger.rb, line 532
532: def open_logfile(filename)
533: if (FileTest.exist?(filename))
534: open(filename, (File::WRONLY | File::APPEND))
535: else
536: create_logfile(filename)
537: end
538: end
# File lib/logger.rb, line 578
578: def shift_log
579: # At first, close the device if opened.
580: if @dev
581: @dev.close
582: @dev = nil
583: end
584: if (@shift_age.is_a?(Integer))
585: (@shift_age-3).downto(0) do |i|
586: if (FileTest.exist?("#{@filename}.#{i}"))
587: File.rename("#{@filename}.#{i}", "#{@filename}.#{i+1}")
588: end
589: end
590: File.rename("#{@filename}", "#{@filename}.0")
591: else
592: now = Time.now
593: postfix_time = case @shift_age
594: when /^daily$/
595: eod(now - 1 * SiD)
596: when /^weekly$/
597: eod(now - ((now.wday + 1) * SiD))
598: when /^monthly$/
599: eod(now - now.mday * SiD)
600: else
601: now
602: end
603: postfix = postfix_time.strftime("%Y%m%d") # YYYYMMDD
604: age_file = "#{@filename}.#{postfix}"
605: if (FileTest.exist?(age_file))
606: raise RuntimeError.new("'#{ age_file }' already exists.")
607: end
608: File.rename("#{@filename}", age_file)
609: end
610:
611: @dev = create_logfile(@filename)
612: return true
613: end
# File lib/logger.rb, line 555
555: def shift_log?
556: if !@shift_age or !@dev.respond_to?(:stat)
557: return false
558: end
559: if (@shift_age.is_a?(Integer))
560: # Note: always returns false if '0'.
561: return (@filename && (@shift_age > 0) && (@dev.stat.size > @shift_size))
562: else
563: now = Time.now
564: limit_time = case @shift_age
565: when /^daily$/
566: eod(now - 1 * SiD)
567: when /^weekly$/
568: eod(now - ((now.wday + 1) * SiD))
569: when /^monthly$/
570: eod(now - now.mday * SiD)
571: else
572: now
573: end
574: return (@dev.stat.mtime <= limit_time)
575: end
576: end