【问题标题】:Log4r. How to write two log files from one logger.日志4r。如何从一个记录器写入两个日志文件。
【发布时间】:2014-12-24 15:54:40
【问题描述】:

我使用以下教程在我的 Rails 应用程序 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

在我的 application.rb 中:

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

如何配置记录器以制作两个单独的文件:

1) debug.log 记录了优先级为 DEBUG 及以下的所有消息。

2) app.log 显示与 debug.log 中相同的消息,除了 DEBUG 和 PARAMS

所以

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

【问题讨论】:

    标签: ruby-on-rails logging log4r


    【解决方案1】:

    我意识到我可以使用两个具有不同错误级别的 FileOutputter

    - 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
    

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 2015-12-29
      • 2017-11-09
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多