A lock-free logger with timebased file rotation.
Ruby's stdlib Logger
wraps IO#write
in mutexes. ChronoLogger
removes these mutexes.
ChronoLogger
provides time based file rotation such as:
logger = ChronoLogger.new('/log/production.log.%Y%m%d')
Time.now.strftime('%F')
# => "2015-01-26"
File.exist?('/log/production.log.20150126')
# => true
# one day later
Time.now.strftime('%F')
# => "2015-01-27"
logger.write('hi next day')
File.exist?('/log/production.log.20150127')
# => true
Current my projects uses ::Logger
with cronolog. So
- Reduce dependency such as cronolog
- Remove mutexes in ruby world because os already does when some environments (ex: ext4 file system)
- Support time based rotation without renaming file because file renaming sometime makes problem
Add this line to your application's Gemfile:
gem 'chrono_logger'
And then execute:
$ bundle
Or install it yourself as:
$ gem install chrono_logger
Same interfaces ruby's stdlib Logger
except for new
method.
require 'chrono_logger'
# specify path with `Time#strftime` format
logger = ChronoLogger.new('development.%Y%m%d')
logger.error("Enjoy")
logger.warn("logging!")
logger.info("Enjoy")
logger.debug("programming!")
With Rails:
# in config/environments/{development,production}.rb
config.logger = ChronoLogger.new("#{config.paths['log'].first}.%Y%m%d")
You only change Logger.new
into ChronoLogger.new
:
# for instance your setup is like the following
Logger.new(IO.popen("/usr/sbin/cronolog production.%Y%m%d", "w"))
# turns into
ChronoLogger.new('production.%Y%m%d')
- High performance logging only daily based time formatting path for example
'%Y%m%d'
. You can create pull request if you need other time period.
- Fork it ( https://github.com/ma2gedev/chrono_logger/fork )
- 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 a new Pull Request
MIT. See LICENSE.txt for more details.