Skip to content

101 the datefile adapter

rudionrails edited this page May 29, 2012 · 9 revisions

Yell 101 - How To: The Datefile Adapter

In many scenarios, you will want to have one file for each day, e.g. rollover of the log. This is where the Datefile adapter comes into place. A basic datefile adapter is defined as follows:

logger = Yell.new :datefile, 'development.log'

# The same logger defined within the block
logger = Yell.new do |l|
  l.adapter :datefile, 'development.log'
end

Although you have provided development.log as filename, by default, the adapter will replace it with development.YYYYMMDD.log, e.g. development.20121224.log.

Defining the Date Pattern (know your options)

The standard is a daily log rotate, but you are able to change that to your own likings. You only need to pass a :date_pattern option to the adapter, which will respect any possible string for Ruby's strftime method.

Example: Rotate the log every hour

Let's assume that Time.now results to 2012-12-24 15:00:30 +0100

logger = Yell.new do |l|
  l.adapter :datefile, 'development.log', :date_pattern => "%Y%m%d-%H"
end

logger.info "Hello World!"

The message would be logged into development.20121224-15.log. Any later hour will go into a new log file.

Example: Rotate the log every week

logger = Yell.new do |l|
  l.adapter :datefile, 'development.log', :date_pattern => "%Y-week-%V"
end

logger.info "Hello World!"

The message would be logged into development.2012-week-52.log

Experimental: Automatic cleanup of the log directory

There is a - sort-of experimental - option that you can pass to keep your logiles in check:

logger = Yell.new do |l|
  l.adapter :datefile, 'development.log', :keep => 5
end

As usual, the datefile adapter will roll your files over, but this time the :keep option will cleanup any datefile logs (of the same pattern as the current one) and only hold on to the given number.

If you want to know more, continue to How To: Different Adapters for Different Log Levels