ruby-on-railslogginglog4r

Log4r. How to write two log files from one logger.


I used the following tutorial to configure logging in my rails application http://blog.mmlac.com/log4r-for-rails/

log4r.yml

log4r_config:
  pre_config:
    custom_levels:
    - DEBUG
    - PARAMS
    - INFO
    - WARN
    - ERROR
    - EXCEPTION
    - FATAL
  # define all loggers ...
  loggers:
    - name          : rails
      level         : DEBUG
      trace         : 'false'
      outputters    :
      - console
      - railsfiledebug

    - name          : mysql
      level         : DEBUG
      trace         : 'false'
      outputters    :
      - console
      - railsfile


  # define all outputters (incl. formatters)
  outputters:
  - type: StdoutOutputter
    name: console
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m'
      type        : PatternFormatter

  - type: FileOutputter
    name: railsfile
    filename: "log/cryptoserver_#{ENV}.log" # notice the file extension is needed!
    trunc: false
    formatter:
      date_pattern: '%Y %m %d %H:%M:%S.%L %z'
      pattern     : '[#{EC2_INSTANCE}][%p][#{APPNAME}]|[RAILS][%d][%l]: %m '
      type        : PatternFormatter

In my application.rb:

Rails.logger = Log4r::Logger['rails']

How can I configure the logger to make two separate files:

1) debug.log where logged all messages with priority DEBUG and less.

2) app.log where presents the same messages as in debug.log except DEBUG and PARAMS

So

Rails.logger.info 'this should go in two log files'
Rails.logger.debug 'this should go in debug.log only'

Solution

  • I realized I can use two FileOutputters with different error levels

    - type: FileOutputter
        name: railsfile
        level: INFO
        filename: "log/cryptoserver_#{ENV}.log" # notice the file extension is needed!
        trunc: false
        formatter:
          date_pattern: '%Y %m %d %H:%M:%S.%L %z'
          pattern     : '[#{EC2_INSTANCE}][%p][#{APPNAME}]|[RAILS][%d][%l]: %m '
          type        : PatternFormatter
    
      - type: FileOutputter
        name: railsfiledebug
        level: DEBUG
        filename: "log/debug_#{ENV}.log" # notice the file extension is needed!
        trunc: false
        formatter:
          date_pattern: '%Y %m %d %H:%M:%S.%L %z'
          pattern     : '[#{EC2_INSTANCE}][%p][#{APPNAME}]|[RAILS][%d][%l]: %m '
          type        : PatternFormatter