0

I'm a newbie in ruby on rails. I struggled to find the solution for writing multiple logs and the daily rolling file appender.

The output will be like that: (logfile in folder {my project}/log)

db_access.log
db_access.log.20200513
...
user_operator.log
user_operator.log.20200513
...

And I have found a simple solution.

ngoctrambui
  • 141
  • 1
  • 4

1 Answers1

1
  1. Add gem 'multi_logger' in Gemfile and run bundle install.

  2. Create file logger.rb in /config/initializers and add below code

    MultiLogger.add_logger('user_operation', shift_age: 'daily', shift_period_suffix: '%Y%m%d') MultiLogger.add_logger('db_access', shift_age: 'daily', shift_period_suffix: '%Y%m%d')

  3. Now, you can write log in your project

    Rails.logger.user_operation.info 'your_message'

    Rails.logger.db_access.info 'your_message'

(write log in level info)

You can also customize your log by adding other options: logdev, shift_age, shift_size, level, progname, formatter, datetime_format, shift_period_suffix

ngoctrambui
  • 141
  • 1
  • 4