All Messages of Referenced Jar to Log File + Setting up Encoding of Log File to UTF-8 to display Japanese Characters (Log4j2)

huangapple go评论69阅读模式
英文:

All Messages of Referenced Jar to Log File + Setting up Encoding of Log File to UTF-8 to display Japanese Characters (Log4j2)

问题

以下是翻译好的内容:

  1. 关于Log4j2,我有两个问题。

    1. 如何设置,使得来自引用的Jar文件的警告和错误消息输出到日志文件而不是控制台?当前,我的应用程序中的日志语句被打印到日志文件中。但是那些来自引用的Jar文件的消息却被打印到控制台。

    2. 如何设置,使得日志文件中能够正确打印日文字符?目前,我已经设置了布局的字符集,但是日文字符的输出仍然是“?”或者乱码字符。

这是我的当前属性文件:

<code>
    status = error
    name = PropertiesConfig
    
    property.filename = ${sys:user.home}\\myApp\\logs\\application.log
     
    filters = threshold
     
    filter.threshold.type = ThresholdFilter
    filter.threshold.level = debug
    #filter.threshold.level = info
     
    appenders = rolling
     
    appender.rolling.type = RollingFile
    appender.rolling.name = RollingFile
    appender.rolling.fileName = ${filename}
    appender.rolling.filePattern = ${sys:user.home}\\myApp\\logs\\log-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.zip
    appender.rolling.layout.type = PatternLayout
    appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    appender.rolling.layout.charset = "UTF-8"
    appender.rolling.policies.type = Policies
    appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
    appender.rolling.policies.size.size=1MB
    appender.rolling.strategy.type = DefaultRolloverStrategy
    appender.rolling.strategy.max = 20
     
    loggers = rolling
     
    logger.rolling.name = org.mycom.myproj.tools.myapp
    logger.rolling.level = all
    logger.rolling.additivity = false
    logger.rolling.appenderRef.rolling.ref = RollingFile
    
    rootLogger.level = all
    rootLogger.appenderRefs = rolling
    rootLogger.appenderRef.rolling.ref = RollingFile
</code>
英文:

I have 2 questions regarding Log4j2.

  1. How can I set that warnings and errors of referenced Jar files to output in the log file instead of the console? Currently, log statements in my application are being printed in the log file. But those messages from the referenced Jar files are being printed in the console instead.

  2. How can I set Japanese characters to be printed in the generated logfile? Currently, I have set charset of layout but the output of Japanese characters are still in "?" or garbage characters.

This is my current property file:

<pre>
<code>

status = error
name = PropertiesConfig

property.filename = ${sys:user.home}\\myApp\\logs\\application.log
 
filters = threshold
 
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
#filter.threshold.level = info
 
appenders = rolling
 
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = ${sys:user.home}\\myApp\\logs\\log-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.layout.charset = &quot;UTF-8&quot;
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=1MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
 
loggers = rolling
 
logger.rolling.name = org.mycom.myproj.tools.myapp
logger.rolling.level = all
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = all
rootLogger.appenderRefs = rolling
rootLogger.appenderRef.rolling.ref = RollingFile

</code>
</pre>

答案1

得分: 0

我假设你所说的“referenced jar files”是指“外部第三方库”。这些库通常会使用诸如apache.commons.logging或slfj4的日志记录抽象来发出其日志输出。

您需要添加一个附加的依赖项,将这些日志输出正确地桥接到您的log4j2日志记录器。例如,“org.apache.logging.log4j:log4j-slf4j-impl:2.7”。

如果您不提供这样的桥接,日志抽象框架可能会进行默认初始化(可能会将日志记录到控制台)。

英文:

I assume you mean 'external third party libraries' by 'referenced jar files'. Libraries usually use a logger abstraction like apache.commons.logging or slfj4 for issuing its log outputs.

You need to add an additional dependency which bridges these log outputs correctly to your log4j2-Logger. e.g. "org.apache.logging.log4j:log4j-slf4j-impl:2.7".

If you do not provide such a bridge the log abstraction framework might do default initialization (which probably logs to console).

huangapple
  • 本文由 发表于 2020年10月13日 15:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64330548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定