Log4J2 - Einfaches Konfigurationsfile

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

Die Log4J2 Konfigurationsdatei speichert im Ordner logs drei Dateien (Infos.log, Warn.log, Error.log)

Aus dem Java Program kann nun in die einzelnen Levels geschrieben werden:

<code=java>log.info("Hello world - info log");</code=java> <code=java>log.warn("Hello world - warn log");</code=java> <code=java>log.erro("Hello world - error log");</code=java>

In der Console kommt jede Ausgabe, in den Logfiles nur die der entsprechenden Ebene:

<code=xml><?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" /> </Console>

<RollingFile name="fileInfo" fileName="logs/Info.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.info.gz">

<PatternLayout> <pattern>%d %-5p [%t] %C{2} (%F:%L) - %m%n</pattern> </PatternLayout>

<Filters> <ThresholdFilter level="info" /> <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters>

<Policies> <SizeBasedTriggeringPolicy size="5 MB" /> </Policies>

<DefaultRolloverStrategy max="20" /> </RollingFile>


<RollingFile name="fileWarn" fileName="logs/Warn.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.warn.gz">

<PatternLayout> <pattern>%d %-5p [%t] %C{2} (%F:%L) - %m%n</pattern> </PatternLayout>

<Filters> <ThresholdFilter level="warn" /> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters>

<Policies> <SizeBasedTriggeringPolicy size="5 MB" /> </Policies>

<DefaultRolloverStrategy max="20" /> </RollingFile>


<RollingFile name="fileError" fileName="logs/Error.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.error.gz">

<PatternLayout> <pattern>%d %-5p [%t] %C{2} (%F:%L) - %m%n</pattern> </PatternLayout>

<Filters> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" /> </Filters>

<Policies> <SizeBasedTriggeringPolicy size="5 MB" /> </Policies>

<DefaultRolloverStrategy max="20" /> </RollingFile> </Appenders>

<Loggers> <Logger name="org.apache.log4j2.xml"> <appender-ref ref="fileInfo" /> <appender-ref ref="fileWarn" /> <appender-ref ref="fileError" /> </Logger>

<Root level="info"> <AppenderRef ref="STDOUT" /> <AppenderRef ref="fileInfo" /> <AppenderRef ref="fileWarn" /> <AppenderRef ref="fileError" /> </Root> </Loggers> </Configuration>

</code=xml>