1 # logger.rb - saimple logging utility
2 # Copyright (C) 2000-2003, 2005 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>.
6 # Simple logging utility.
8 # Author:: NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
9 # Documentation:: NAKAMURA, Hiroshi and Gavin Sinclair
11 # You can redistribute it and/or modify it under the same terms of Ruby's
12 # license; either the dual license version in 2003, or any later version.
13 # Revision:: $Id: logger.rb 11708 2007-02-12 23:01:19Z shyouhei $
17 # The Logger class provides a simple but sophisticated logging utility that
18 # anyone can use because it's included in the Ruby 1.8.x standard library.
20 # The HOWTOs below give a code-based overview of Logger's usage, but the basic
21 # concept is as follows. You create a Logger object (output to a file or
22 # elsewhere), and use it to log messages. The messages will have varying
23 # levels (+info+, +error+, etc), reflecting their varying importance. The
24 # levels, and their meanings, are:
26 # +FATAL+:: an unhandleable error that results in a program crash
27 # +ERROR+:: a handleable error condition
29 # +INFO+:: generic (useful) information about system operation
30 # +DEBUG+:: low-level information for developers
32 # So each message has a level, and the Logger itself has a level, which acts
33 # as a filter, so you can control the amount of information emitted from the
34 # logger without having to remove actual messages.
36 # For instance, in a production system, you may have your logger(s) set to
37 # +INFO+ (or +WARN+ if you don't want the log files growing large with
38 # repetitive information). When you are developing it, though, you probably
39 # want to know about the program's internal state, and would set them to
44 # A simple example demonstrates the above explanation:
46 # log = Logger.new(STDOUT)
47 # log.level = Logger::WARN
49 # log.debug("Created logger")
50 # log.info("Program started")
51 # log.warn("Nothing to do!")
54 # File.each_line(path) do |line|
55 # unless line =~ /^(\w+) = (.*)$/
56 # log.error("Line in wrong format: #{line}")
60 # log.fatal("Caught exception; exiting")
64 # Because the Logger's level is set to +WARN+, only the warning, error, and
65 # fatal messages are recorded. The debug and info messages are silently
70 # There are several interesting features that Logger provides, like
71 # auto-rolling of log files, setting the format of log messages, and
72 # specifying a program name in conjunction with the message. The next section
73 # shows you how to achieve these things.
78 # === How to create a logger
80 # The options below give you various choices, in more or less increasing
83 # 1. Create a logger which logs messages to STDERR/STDOUT.
85 # logger = Logger.new(STDERR)
86 # logger = Logger.new(STDOUT)
88 # 2. Create a logger for the file which has the specified name.
90 # logger = Logger.new('logfile.log')
92 # 3. Create a logger for the specified file.
94 # file = File.open('foo.log', File::WRONLY | File::APPEND)
95 # # To create new (and to remove old) logfile, add File::CREAT like;
96 # # file = open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
97 # logger = Logger.new(file)
99 # 4. Create a logger which ages logfile once it reaches a certain size. Leave
100 # 10 "old log files" and each file is about 1,024,000 bytes.
102 # logger = Logger.new('foo.log', 10, 1024000)
104 # 5. Create a logger which ages logfile daily/weekly/monthly.
106 # logger = Logger.new('foo.log', 'daily')
107 # logger = Logger.new('foo.log', 'weekly')
108 # logger = Logger.new('foo.log', 'monthly')
110 # === How to log a message
112 # Notice the different methods (+fatal+, +error+, +info+) being used to log
113 # messages of various levels. Other methods in this family are +warn+ and
114 # +debug+. +add+ is used below to log a message of an arbitrary (perhaps
117 # 1. Message in block.
119 # logger.fatal { "Argument 'foo' not given." }
121 # 2. Message as a string.
123 # logger.error "Argument #{ @foo } mismatch."
127 # logger.info('initialize') { "Initializing..." }
131 # logger.add(Logger::FATAL) { 'Fatal error!' }
133 # === How to close a logger
137 # === Setting severity threshold
139 # 1. Original interface.
141 # logger.sev_threshold = Logger::WARN
143 # 2. Log4r (somewhat) compatible interface.
145 # logger.level = Logger::INFO
147 # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
152 # Log messages are rendered in the output stream in a certain format. The
153 # default format and a sample are shown below:
156 # SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
159 # I, [Wed Mar 03 02:34:24 JST 1999 895701 #19074] INFO -- Main: info.
161 # You may change the date and time format in this manner:
163 # logger.datetime_format = "%Y-%m-%d %H:%M:%S"
164 # # e.g. "2004-01-03 00:54:26"
166 # There is currently no supported way to change the overall format, but you may
167 # have some luck hacking the Format constant.
173 /: (\S+),v (\S+)/ =~ %q$Id: logger.rb 11708 2007-02-12 23:01:19Z shyouhei $
174 ProgName = "#{$1}/#{$2}"
176 class Error < RuntimeError; end
177 class ShiftingError < Error; end
190 # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
193 # Logging program name.
194 attr_accessor :progname
196 # Logging date-time format (string passed to +strftime+).
197 def datetime_format=(datetime_format)
198 @default_formatter.datetime_format = datetime_format
202 @default_formatter.datetime_format
205 # Logging formatter. formatter#call is invoked with 4 arguments; severity,
206 # time, progname and msg for each log. Bear in mind that time is a Time and
207 # msg is an Object that user passed and it could not be a String. It is
208 # expected to return a logdev#write-able Object. Default formatter is used
209 # when no formatter is set.
210 attr_accessor :formatter
212 alias sev_threshold level
213 alias sev_threshold= level=
215 # Returns +true+ iff the current severity level allows for the printing of
217 def debug?; @level <= DEBUG; end
219 # Returns +true+ iff the current severity level allows for the printing of
221 def info?; @level <= INFO; end
223 # Returns +true+ iff the current severity level allows for the printing of
225 def warn?; @level <= WARN; end
227 # Returns +true+ iff the current severity level allows for the printing of
229 def error?; @level <= ERROR; end
231 # Returns +true+ iff the current severity level allows for the printing of
233 def fatal?; @level <= FATAL; end
238 # Logger.new(name, shift_age = 7, shift_size = 1048576)
239 # Logger.new(name, shift_age = 'weekly')
244 # The log device. This is a filename (String) or IO object (typically
245 # +STDOUT+, +STDERR+, or an open file).
247 # Number of old log files to keep, *or* frequency of rotation (+daily+,
248 # +weekly+ or +monthly+).
250 # Maximum logfile size (only applies when +shift_age+ is a number).
254 # Create an instance.
256 def initialize(logdev, shift_age = 0, shift_size = 1048576)
259 @default_formatter = Formatter.new
263 @logdev = LogDevice.new(logdev, :shift_age => shift_age,
264 :shift_size => shift_size)
271 # Logger#add(severity, message = nil, progname = nil) { ... }
276 # Severity. Constants are defined in Logger namespace: +DEBUG+, +INFO+,
277 # +WARN+, +ERROR+, +FATAL+, or +UNKNOWN+.
279 # The log message. A String or Exception.
281 # Program name string. Can be omitted. Treated as a message if no +message+ and
284 # Can be omitted. Called to get a message string if +message+ is nil.
288 # +true+ if successful, +false+ otherwise.
290 # When the given severity is not high enough (for this particular logger), log
291 # no message, and return +true+.
295 # Log a message if the given severity is high enough. This is the generic
296 # logging method. Users will be more inclined to use #debug, #info, #warn,
297 # #error, and #fatal.
299 # <b>Message format</b>: +message+ can be any object, but it has to be
300 # converted to a String in order to log it. Generally, +inspect+ is used
301 # if the given object is not a String.
302 # A special case is an +Exception+ object, which will be printed in detail,
303 # including message, class, and backtrace. See #msg2str for the
304 # implementation if required.
308 # * Logfile is not locked.
309 # * Append open does not need to lock file.
310 # * But on the OS which supports multi I/O, records possibly be mixed.
312 def add(severity, message = nil, progname = nil, &block)
314 if @logdev.nil? or severity < @level
317 progname ||= @progname
327 format_message(format_severity(severity), Time.now, progname, message))
333 # Dump given message to the log device without any formatting. If no log
334 # device exists, return +nil+.
343 # Log a +DEBUG+ message.
345 # See #info for more information.
347 def debug(progname = nil, &block)
348 add(DEBUG, nil, progname, &block)
352 # Log an +INFO+ message.
354 # The message can come either from the +progname+ argument or the +block+. If
355 # both are provided, then the +block+ is used as the message, and +progname+
356 # is used as the program name.
360 # logger.info("MainApp") { "Received connection from #{ip}" }
362 # logger.info "Waiting for input from user"
364 # logger.info { "User typed #{input}" }
366 # You'll probably stick to the second form above, unless you want to provide a
367 # program name (which you can do with <tt>Logger#progname=</tt> as well).
373 def info(progname = nil, &block)
374 add(INFO, nil, progname, &block)
378 # Log a +WARN+ message.
380 # See #info for more information.
382 def warn(progname = nil, &block)
383 add(WARN, nil, progname, &block)
387 # Log an +ERROR+ message.
389 # See #info for more information.
391 def error(progname = nil, &block)
392 add(ERROR, nil, progname, &block)
396 # Log a +FATAL+ message.
398 # See #info for more information.
400 def fatal(progname = nil, &block)
401 add(FATAL, nil, progname, &block)
405 # Log an +UNKNOWN+ message. This will be printed no matter what the logger
408 # See #info for more information.
410 def unknown(progname = nil, &block)
411 add(UNKNOWN, nil, progname, &block)
415 # Close the logging device.
418 @logdev.close if @logdev
423 # Severity label for logging. (max 5 char)
424 SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
426 def format_severity(severity)
427 SEV_LABEL[severity] || 'ANY'
430 def format_message(severity, datetime, progname, msg)
431 (@formatter || @default_formatter).call(severity, datetime, progname, msg)
436 Format = "%s, [%s#%d] %5s -- %s: %s\n"
438 attr_accessor :datetime_format
441 @datetime_format = nil
444 def call(severity, time, progname, msg)
445 Format % [severity[0..0], format_datetime(time), $$, severity, progname,
451 def format_datetime(time)
452 if @datetime_format.nil?
453 time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec
455 time.strftime(@datetime_format)
464 "#{ msg.message } (#{ msg.class })\n" <<
465 (msg.backtrace || []).join("\n")
475 attr_reader :filename
481 def initialize(log = nil, opt = {})
482 @dev = @filename = @shift_age = @shift_size = nil
483 @mutex = LogDeviceMutex.new
484 if log.respond_to?(:write) and log.respond_to?(:close)
487 @dev = open_logfile(log)
490 @shift_age = opt[:shift_age] || 7
491 @shift_size = opt[:shift_size] || 1048576
496 @mutex.synchronize do
497 if @shift_age and @dev.respond_to?(:stat)
501 raise Logger::ShiftingError.new("Shifting failed. #{$!}")
509 @mutex.synchronize do
516 def open_logfile(filename)
517 if (FileTest.exist?(filename))
518 open(filename, (File::WRONLY | File::APPEND))
520 create_logfile(filename)
524 def create_logfile(filename)
525 logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
527 add_log_header(logdev)
531 def add_log_header(file)
533 "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
540 if @shift_age.is_a?(Integer)
541 # Note: always returns false if '0'.
542 if @filename && (@shift_age > 0) && (@dev.stat.size > @shift_size)
547 if @dev.stat.mtime <= previous_period_end(now)
548 shift_log_period(now)
554 (@shift_age-3).downto(0) do |i|
555 if FileTest.exist?("#{@filename}.#{i}")
556 File.rename("#{@filename}.#{i}", "#{@filename}.#{i+1}")
560 File.rename("#{@filename}", "#{@filename}.0")
561 @dev = create_logfile(@filename)
565 def shift_log_period(now)
566 postfix = previous_period_end(now).strftime("%Y%m%d") # YYYYMMDD
567 age_file = "#{@filename}.#{postfix}"
568 if FileTest.exist?(age_file)
569 raise RuntimeError.new("'#{ age_file }' already exists.")
572 File.rename("#{@filename}", age_file)
573 @dev = create_logfile(@filename)
577 def previous_period_end(now)
582 eod(now - ((now.wday + 1) * SiD))
584 eod(now - now.mday * SiD)
591 Time.mktime(t.year, t.month, t.mday, 23, 59, 59)
599 # Application -- Add logging support to your application.
603 # 1. Define your application class as a sub-class of this class.
604 # 2. Override 'run' method in your class to do many things.
605 # 3. Instantiate it and invoke 'start'.
609 # class FooApp < Application
610 # def initialize(foo_app, application_specific, arguments)
611 # super('FooApp') # Name of the application.
616 # log(WARN, 'warning', 'my_method1')
618 # @log.error('my_method2') { 'Error!' }
623 # status = FooApp.new(....).start
626 include Logger::Severity
634 # Application.new(appname = '')
638 # +appname+:: Name of the application.
642 # Create an instance. Log device is +STDERR+ by default. This can be
643 # changed with #set_log.
645 def initialize(appname = nil)
647 @log = Logger.new(STDERR)
648 @log.progname = @appname
653 # Start the application. Return the status code.
658 log(INFO, "Start of #{ @appname }.")
661 log(FATAL, "Detected an exception. Stopping ... #{$!} (#{$!.class})\n" << $@.join("\n"))
663 log(INFO, "End of #{ @appname }. (status: #{ status.to_s })")
669 # Sets the log device for this application. See the class Logger for an
670 # explanation of the arguments.
672 def set_log(logdev, shift_age = 0, shift_size = 1024000)
673 @log = Logger.new(logdev, shift_age, shift_size)
674 @log.progname = @appname
683 # Set the logging threshold, just like <tt>Logger#level=</tt>.
691 # See Logger#add. This application's +appname+ is used.
693 def log(severity, message = nil, &block)
694 @log.add(severity, message, @appname, &block) if @log
700 raise RuntimeError.new('Method run must be defined in the derived class.')