Ole Ole - 1 year ago 323
Java Question

Start configuration snippet for log4j2.properties?

I'm looking for a

snippet that I can drop in to
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
version of this:

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

Answer Source

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">
    <property name="LOG_DIR">target/logs</property>
  <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{ABSOLUTE} %-5level # %class.%method %m%n" />

    <RollingFile name="log4j" fileName="${LOG_DIR}/log4j.txt" filePattern="${LOG_DIR}/archive/log4j.txt.%d{yyyyMMdd_HH}-%i">
        <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"/>
        <SizeBasedTriggeringPolicy size="30 MB"/>
      <DefaultRolloverStrategy min="1" max="20"/>
    <Root level="debug">
      <AppenderRef ref="log4j" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download