启动log4j2.properties的配置代码段?

时间:2016-07-07 19:55:25

标签: java log4j2

我正在寻找一个log4j2.properties代码段,我可以将其放入src/test/resources只是为了让No logj2 configuration file found警告消失。如果它包含其他有用的示例,例如配置示例也很好。

基本上我正在寻找.properties版本:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} %c{1.} [%t] %-5level} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

1 个答案:

答案 0 :(得分:1)

我还没有尝试过,但这应该可以解决问题。

status = error
name = PropertiesConfig

property.filename = target/logs/test.log

appender.file.type = File
appender.file.name = LogFile
appender.file.fileName = ${filename}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n

rootLogger.level = error
rootLogger.appenderRef.stdout.ref = LogFile

这是我在一些单元测试中实际使用的log4j2.xml。

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="ERROR">
  <properties>
    <property name="LOG_DIR">target/logs</property>
  </properties>
  <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{ABSOLUTE} %-5level # %class.%method %m%n" />
    </Console>

    <RollingFile name="log4j" fileName="${LOG_DIR}/log4j.txt" filePattern="${LOG_DIR}/archive/log4j.txt.%d{yyyyMMdd_HH}-%i">
      <PatternLayout>
        <MarkerPatternSelector defaultPattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} %C{1.}.%M:%L - %m%n">
          <PatternMatch key="FLOW" pattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} -------- %C{1.}.%M:%L %msg --------%n"/>
        </MarkerPatternSelector>
      </PatternLayout>
      <Policies>
        <SizeBasedTriggeringPolicy size="30 MB"/>
      </Policies>
      <DefaultRolloverStrategy min="1" max="20"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="log4j" />
    </Root>
  </Loggers>
</Configuration>