Reputation: 16629
I have added log4r in my Rails 2.3.8 project. I want to create 2 log files.
Following are my yml configuration files for each logger
error_config.yml (errors.log)
# *** YAML2LOG4R ***
error_config:
# define all pre config ...
pre_config:
custom_levels:
- DEBUG
- INF
- PRT
- WRN
- ERR
- FAT
global:
level: DEBUG
root :
level: DEBUG
parameters:
- name : x
value : aaa
- name : y
value : bbb
# define all loggers ...
loggers:
- name : mylogger
level : DEBUG
additive : 'false'
trace : 'false'
outputters:
- stderr
- logfile
# define all outputters (incl. formatters)
outputters:
- type : StderrOutputter
name : stderr
level : DEBUG
only_at :
- INF
- WRN
- FAT
formatter:
date_pattern: '%y%m%d %H:%M:%S'
pattern : '%d %l: %m '
type : PatternFormatter
- type : DateFileOutputter
name : logfile
level : DEBUG
date_pattern: '%Y%m%d'
trunc : 'false'
dirname : "/home/sameera/workspace/project/log"
filename : "errors.log"
formatter :
date_pattern: '%m/%d/%Y %H:%M:%S'
pattern : '%d %l - %m'
type : PatternFormatter
application_config.yml (application.log)
# *** YAML2LOG4R ***
application_config:
# define all pre config ...
pre_config:
custom_levels:
- DEBUG
- INF
- PRT
- WRN
- ERR
- FAT
global:
level: DEBUG
root :
level: DEBUG
parameters:
- name : x
value : aaa
- name : y
value : bbb
# define all loggers ...
loggers:
- name : application
level : DEBUG
additive : 'false'
trace : 'false'
outputters:
- stderr
- logfile
# define all outputters (incl. formatters)
outputters:
- type : StderrOutputter
name : stderr
level : DEBUG
only_at :
- INF
- WRN
- FAT
formatter:
date_pattern: '%y%m%d %H:%M:%S'
pattern : '%d %l: %m '
type : PatternFormatter
- type : DateFileOutputter
name : application
level : DEBUG
date_pattern: '%Y%m%d'
trunc : 'false'
dirname : "/home/sameera/workspace/project/log"
filename : "application.log"
formatter :
date_pattern: '%m/%d/%Y %H:%M:%S'
pattern : '%d %l - %m'
type : PatternFormatter
and following is the code to initialize both the loggers
h = YAML.load(File.open("config/error_config.yml"))
app_log = YAML.load(File.open("config/application_config.yml"))
log_cfg = YamlConfigurator
log_cfg['TEST'] = 'foobar'
log_cfg.decode_yaml h['error_config']
applog_cfg = YamlConfigurator
applog_cfg['TEST'] = 'foobar'
applog_cfg.decode_yaml app_log['application_config']
$ERRORLOG = Log4r::Logger['error_config']
$APPLICATION = Log4r::Logger['application_config']
$ERRORLOG.debug "info message"
$APPLICATION.debug "info message"
This code works fine, but it print both the messages in the errors.log (where as I want the second message to be print on the application.log).
Is there any simple way of having 2 logs files through one log4r instance?
Upvotes: 1
Views: 496
Reputation: 21569
I noticed that your "error_config.yml" file defines the output level as "debug".
loggers:
- name : mylogger
level : DEBUG
maybe you want it to be "WARN" ?
loggers:
- name : mylogger
level : WARN
p.s. ^_^ naming convension is important. please be friendly to the readers of your code. (usually is yourself). which is more clearer?
h = YAML.load(File.open("config/error_config.yml"))
error_config = YAML.load(File.open("config/error_config.yml"))
Upvotes: 1