1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Quick Reference: Logger

By

Quick Reference: Logger

A logger is an interface to a log file. It handles keeping the files open, adding timestamps and log levels, etc. Once set up, you just throw messages at it and it handles the rest. Ruby comes with a basic logging interface in the standard library called Logger, which you can use be requiring 'logger'.

Quick Example

The basic usage of logger is like so. Create the logger object with either the name of a file to write to or an IO object it can use and then use the methods like debug, info and warn to print messages to the logfile.


require 'logger'

logger = Logger.new('log.txt')

logger.debug('A debug message')
logger.info('Info messages are more important than debug')
logger.warning('Warning messages are even more important')
logger.error('Error messages are even more important')

Rotating Logs

An important feature of any logger is the log rotation. If you have an infinitely growing log file, it can quickly grow unmanageable and becomes difficult to prune without shutting your server off. When creating your log file, you can tell Logger how large you want your log files and how many to keep around. The following will create 10 log files with suffixes like .0, .1, etc (automatically incremented by logger) each a maximum of 1 megabyte, when the last fills up it'll go back to the first log file again.


require 'logger'

logger = Logger.new('test.log', 10, 1024*1024)

Log Levels

The following are the log levels implemented by the Logger::Severity module. The constant name is given, as well as the shorthand method you can call on your logger object. They are in order of least important to most important.

  • DEBUG, debug - Low level debugging information, mostly for use in development and debugging problems.

  • INFO, info - Generic information. Not necessarily errors or warnings, just information about the program running.

  • WARN, warn - Warnings from the running program, these are typically potential problems, or problems that could be worked around or ignored (such as a missing configuration file, and the program fell back to default configuration).

  • ERROR, error - An error is something seriously wrong. The program hasn't necessarily shut down yet though, that's for the next level.

  • FATAL, fatal - A fatal error, there's nothing left to do but log the error and shut down.

  • UNKNOWN, unknown - The default log level, these are always logged.

You can set the log levels you're interested in by using the level attribute on your logger object. If you set the level attribute to WARN, it will log warnings and anything more important, but not info or debug messages. By default, this is set to DEBUG, so all messages are logged.


require 'logger'

logger = Logger.new('log.txt')
logger.level = Logger::INFO

logger.debug 'This will be ignored'
logger.info 'This will be written to the log file'

Better Logging

And that's just about it. The logger interface is as simple as possible. For more comprehensive logging, see the log4r gem.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Quick Reference: Logger

©2014 About.com. All rights reserved.