RollingFileAppender在从Log4j迁移到Log4j2时的实现。

huangapple 未分类评论44阅读模式
英文:

RollingFileAppender implementation while migrating from Log4j to Log4j2

问题

以下是翻译好的内容:

private void initLogger(String logFile) {
    RollingFileAppender fileAppender = (RollingFileAppender) Logger.getRootLogger()
        .getAppender("FILE");

    if (logFile != null && !"".equals(logFile)) {
        if (null != fileAppender) {
            fileAppender.setFile(logFile);
            fileAppender.activateOptions();
            fileAppender.setImmediateFlush(true);
        }
    }
}

Log4j2 的 jar 包: log4j-api-2.12.1.jar, log4j-core-2.12.1.jar

请问有人能提供帮助,指导如何正确处理上述方法吗?

英文:

The following method was being used for a while now when the system I am currently working on has Log4j. Now that we are moving from Log4j to Log4j2, I am getting compilation issues with it.

private void initLogger(String logFile) {
    RollingFileAppender fileAppender = (RollingFileAppender) Logger.getRootLogger()
        .getAppender("FILE");

    if (logFile!=null && !"".equals(logFile)) {
        if (null != fileAppender) {
            fileAppender.setFile(logFile);
            fileAppender.activateOptions();
            fileAppender.setImmediateFlush(true);
        }
    } 
}

Log4j2 jars: log4j-api-2.12.1.jar, log4j-core-2.12.1.jar

Can someone provide please help with what might be the right approach to deal with the above method?

答案1

得分: 0

在我的情况下,我使用以下的xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <RollingFile name="RollingFile" fileName=".\logs\app.log"
                     filePattern=".\logs\$${date:yyyy-MM}\app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 KB"/>
      </Policies>
      <DefaultRolloverStrategy max="20"/>
    </RollingFile>
  </Appenders>

  <Loggers>
    <Root level="all">
      <AppenderRef ref="RollingFile" />
    </Root>
  </Loggers>
</Configuration>

如果您想使用属性、YAML、JSON而不是XML,请按照此链接中的步骤1到10执行"自动配置"。

英文:

In my case, I use xml configuration like below

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;WARN&quot;&gt;
  &lt;Appenders&gt;
    &lt;RollingFile name=&quot;RollingFile&quot; fileName=&quot;.\logs\app.log&quot;
                     filePattern=&quot;.\logs\$${date:yyyy-MM}\app-%d{MM-dd-yyyy}-%i.log.gz&quot;&gt;
      &lt;PatternLayout&gt;
        &lt;Pattern&gt;%d %p %c{1.} [%t] %m%n&lt;/Pattern&gt;
      &lt;/PatternLayout&gt;
      &lt;Policies&gt;
        &lt;TimeBasedTriggeringPolicy /&gt;
        &lt;SizeBasedTriggeringPolicy size=&quot;1 KB&quot;/&gt;
      &lt;/Policies&gt;
      &lt;DefaultRolloverStrategy max=&quot;20&quot;/&gt;
    &lt;/RollingFile&gt;
  &lt;/Appenders&gt;

  &lt;Loggers&gt;
    &lt;Root level=&quot;all&quot;&gt;
      &lt;AppenderRef ref=&quot;RollingFile&quot; /&gt;
    &lt;/Root&gt;
  &lt;/Loggers&gt;
&lt;/Configuration&gt;

If you want to use properties, YAML, JSON without xml than follow this Link and read steps from 1 to 10 at Automatic Configuration

huangapple
  • 本文由 发表于 2020年4月8日 14:59:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61094958.html
匿名

发表评论

匿名网友

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

确定