ロガーとして log4r を使用しています。fourum に 2 つの質問があります。
- ログ メッセージが STDOUT に 2 回出力されるなど、不思議な動作を見つけました。発生する理由と修正方法がわかりません。
- レベルタグの色分けを提供したいと思います (FATAL は赤、ERROR は紫、WARN はオレンジ、INFO は緑など)。これは、STDOUT に対してのみ有効である必要があり、ログ ファイルに特殊な (メタ) 文字が含まれないようにする必要があります (読みやすくするため)。私はいくつかの試みを試みましたが、これを達成できませんでした。直し方?
以下は、私が使用しているコードの詳細です。この問題の解決にご協力ください。
私の YAML 構成は次のとおりです: ファイル:yml.cfg
# *** YAML2LOG4R ***
log4r_config:
# define all pre config global log4r settings ...
pre_config:
custom_levels:
- DEBUG2
- DEBUG
- PRINT
- INFO
- WARN
- ERROR
- FATAL
global:
level : ALL
root :
level : DEBUG2
trace : 'true'
# define all outputters (stderr, stdout and logfile with custom formatters)
outputters:
- type : StderrOutputter
name : stderr
level : ERROR
only_at :
- ERROR
- FATAL
formatter:
pattern : '%-7l: %m'
type : PatternFormatter
- type : StdoutOutputter
name : stdout
level : INFO
only_at :
- INFO
- WARN
- ERROR
- FATAL
formatter:
pattern : '%-7l: %m'
type : PatternFormatter
- type : FileOutputter
filename : debug.log
name : logfile
level : ALL
trace : 'true'
trunc : 'false'
formatter :
type : MyLogFormatter
loggers:
- name : MyAppClass
additive : 'false'
trace : 'true'
level : ALL
outputters :
- stderr
- stdout
- logfile
ログ構成クラス (logConfig.rb) があります。
require 'log4r'
require 'log4r/lib/yamlconfigurator'
class LogConfig
class Log4r::MyLogFormatter < Log4r::Formatter
def format(event)
buff = "%-7s: " % Log4r::LNAMES[event.level]
buff += "(Trace: %-30s): " % [event.tracer[0].split(File::SEPARATOR)[-1]]
if event.data.kind_of?(String) then buff += event.data
elsif event.data.kind_of?(Array) then buff += "\n\t\t%s : %s\n" % [event.data.class, event.data.inspect]
elsif event.data.kind_of?(Hash) then buff += "\n\t\t%s : %s\n" % [event.data.class, event.data.inspect] end
return buff + "\n"
end
end # class MyLogFormatter
def initialize
cfg = Log4r::YamlConfigurator
cfg.load_yaml_file('yml.cfg')
Log4r::StderrOutputter.new 'console'
Log4r::StdoutOutputter.new 'console'
end # def initialize
end # class LogConfig
私のアプリケーション クラス (myClass.rb) で
require 'logConfig'
class MyAppClass
ClassName = self
def initialize
LogConfig.new
@log = Log4r::Logger["#{ClassName}"]
end # initialize
def print_logs
@log.fatal 'this is fatal'
@log.error 'this is error'
@log.warn 'this is warning'
@log.info 'this is info'
@log.print 'this is print'
@log.debug 'this is debug'
@log.debug2 'this is debug2'
end
end
MyAppClass.new().print_logs
debug.log ファイルで、予期されるメッセージが適切に取得されます。
FATAL : (Trace: myClass.rb:11:in `print_logs' ): this is fatal
ERROR : (Trace: myClass.rb:12:in `print_logs' ): this is error
WARN : (Trace: myClass.rb:13:in `print_logs' ): this is warning
INFO : (Trace: myClass.rb:14:in `print_logs' ): this is info
PRINT : (Trace: myClass.rb:15:in `print_logs' ): this is print
DEBUG : (Trace: myClass.rb:16:in `print_logs' ): this is debug
DEBUG2 : (Trace: myClass.rb:17:in `print_logs' ): this is debug2
ただし、コンソール (端末) では、ERROR および FATAL メッセージが 2 回出力されます。
>ruby myClass.rb
FATAL : this is fatal
FATAL : this is fatal
ERROR : this is error
ERROR : this is error
WARN : this is warning
INFO : this is info
上記の解決にご協力ください。