rubylog4r

Can I set Log4r log level per file/class?


Note: I am talking about Ruby, not Rails.

Is there any way to have Log4r levels set based on the class or file it's logging from?

This is my current configuration for Log4r:

---
log4r_config:
  loggers:
    - name: logger
      level: INFO
      outputters:
        - default
  outputters:
    - name: default
      type: StdoutOutputter
      default: true
      formatter:
        type: PatternFormatter
        date_pattern: '%H:%M:%S'
        pattern: '[%l] %d: %m'

Now assume that I want to debug a single class. Is there any way I can take the same instance of the logger (because I want to use the same pattern format) and set it to a different level (DEBUG), without it affecting the level outside the file?

I suppose I couldn't do this from the code, but is there a configuration mechanism which sets the level per file/class?

As reference, in JBoss application server, the logging level can be configured per Java package.


Solution

  • For now, I am using a workaround with a separate logger for each log level I need. Currently, I need only levels DEBUG and INFO. My log4r.yml then looks like this:

    ---
    log4r_config:
      loggers:
        - name: debug
          level: DEBUG
          outputters:
            - default
        - name: info
          level: INFO
          outputters:
            - default
      outputters:
        - name: default
          type: StdoutOutputter
          default: true
          formatter:
            type: PatternFormatter
            date_pattern: '%H:%M:%S'
            pattern: '[%l] %d: %m'
    

    Then, I fetch a logger based on the level I need for a particular class.