Ole Ole - 4 months ago 142
Java Question

Start configuration snippet for log4j2.properties?

I'm looking for a

log4j2.properties
snippet that I can drop in to
src/test/resources
just to make the
No logj2 configuration file found
warning disappear. If it contains other useful sample like configuration examples that's great as well.

Basically I'm looking for the
.properties
version of this:

<?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>

Answer

I haven't tried it but this should do the trick.

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

Here is a log4j2.xml that I actually use in some of my unit tests.

<?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>
Comments