StrftimeLogger
A logger treats log rotation in strftime fashion.
What is this for?
The ruby's built-in logger does log rotation in the basis of log size.
In contrast, StrftimeLogger
provides a feature to rotate logs in the basis of time.
This logger allows to specify the log path with strftime
format such as:
logger = StrftimeLogger.new('/var/log/application.log.%Y%m%d')
which enables to rotate logs in each specific time.
In fact, this logger does not rotate logs, but outputs to the strftime formatted path directly.
This characteristic gives a side effect that it does not require to lock files in log rotation.
Installation
Add this line to your application's Gemfile:
gem 'strftime_logger'
And then execute:
$ bundle
How to use
Normal Usage
require 'strftime_logger'
logger = StrftimeLogger.new('/var/log/application.log.%Y%m%d')
logger.info("foo\nbar")
which outputs logs to /var/log/application.log.YYYYMMDD
with contents like
20140630T00:00:00+09:00 [INFO] foo\\
where the time is in ISO8601 format, and the line feed characters \n
in log messages
are replaced with \\n
so that the log message will be in one line.
Change the log level
logger.level = StrftimeLogger::WARN
Or, short-hand:
logger.log_level = 'WARN'
Change the Formatter
It is possible to change the logger formmater as:
logger.formatter = SampleFormatter.new
The interface which the costom formmatter must provide is only #initialize(opts = {})
and #call(sevirity, message = nil, &block)
. Following is a simple example:
class SampleFormatter
LEVEL_TEXT = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN)
def initialize(opts={})
end
# @param sevirity [int] log sevirity
def call(severity, = nil, &block)
if .nil?
if block_given?
= yield
else
= ""
end
end
"#{Time.now} #{LEVEL_TEXT[sevirity]} #{message}"
end
end
ToDo
- Support datetime_format
ChangeLog
See CHANGELOG.md for details.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright
See LICENSE.txt for details.