2

I'm trying to configure log4r with yml configuration file. Everything is configured as per the doc, but the log message won't include the class_name or trace. However it works fine with manual configuration without yml , but since it doesn't provide flexibility of configuring the logger, i can't do it that way. According to the doc, log4r config, logger should include trace with

trace: true

I added trace: true from config as well as from logger instance. But it's not working out. The %C in formatter pattern outputs the name of the logger used instead i.e.

logger = Log4r::Logger["development"]

gives log message in the format

150612 17:05:25 [development] DEBUG: hello there

My config.rb

application_config:
  # define all pre config ...
  pre_config:
    custom_levels:
      - DEBUG
      - INFO
      - PRINT
      - WARN
      - ERROR
      - FATAL
    global:
      level: DEBUG
      trace: 'true'
    root:
      level: DEBUG
      trace: 'true'
    parameters:
      - name   : x
        value  : aaa
      - name   : y
        value  : bbb

  # define all loggers ...
  loggers:
    - name      : development
      level     : DEBUG
      additive  : 'false'
      trace     : 'true'
      outputters:
        - stderr
        - logfile

    - name      : production
      level     : WARN
      additive  : 'false'
      trace     : 'true'
      outputters:
        - logfile


  # define all outputters (incl. formatters)
  outputters:
    - type     : StdoutOutputter
      name     : stderr
      level    : DEBUG
      formatter:
        date_pattern: '%y%m%d %H:%M:%S'
        pattern     : "%d [%c] %l: %m "
        type        : PatternFormatter


    - type        : DateFileOutputter
      name        : logfile
      level       : DEBUG
      date_pattern: '%Y%m%d'
      trunc       : 'false'
      dirname     : "logs"
      filename: "development.log"
      formatter   :
        date_pattern: '%y%m%d %H:%M:%S'
        pattern     : '%d %C %l: %m'
        type        : PatternFormatter

and my logger.rb class

require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/datefileoutputter'
require 'log4r/outputter/consoleoutputters'    
yml_config = YAML.load_file(File.expand_path('../../yaml/log4r.yml', __FILE__))
Log4r::YamlConfigurator.decode_yaml yml_config['application_config']
module MyLogger
  def ms_logger
    Log4r::YamlConfigurator['class_name']= class_name

    return @ms_logger if @ms_logger

    @ms_logger = Log4r::Logger["environment"]
    @ms_logger.trace = true
    @ms_logger
  end

end

I included the module in the class where i need to add logger and called the logger method

ms_logger.info("hello there")

Niroj
  • 66
  • 6

1 Answers1

1

Why don't you try to configure manually without including YML configurator, that worked for me

Subash
  • 3,128
  • 6
  • 30
  • 44