I'd like a very simple XML configuration file with a console and a file appender using log4j2.
(The Apache Website is killing me with much Information.)
System. setProperty("logfilename", "a_cool_logname"); Once that is set you can go ahead and get your loggers as normal and they will log to the dynamic file (be careful of those static Loggers that create loggers before your main method executes).
Log4j2 with XML configuration provides a simple way of setting up logging in your Java application. Apache Log4j2 is the new version of the log4j and is used for printing logs when used in a Java program. In this tutorial we will setup a Maven project and use log4j2 to print logs from a simple Java class.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <File name="MyFile" fileName="all.log" immediateFlush="false" append="false"> <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </File> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="Console" /> <AppenderRef ref="MyFile"/> </Root> </Loggers> </Configuration>
Notes:
Logger logger = LogManager.getLogger();
to initialize your loggerHere is my simplistic log4j2.xml
that prints to console and writes to a daily rolling file:
// java private static final Logger LOGGER = LogManager.getLogger(MyClass.class); // log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="logPath">target/cucumber-logs</Property> <Property name="rollingFileName">cucumber</Property> </Properties> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="[%highlight{%-5level}] %d{DEFAULT} %c{1}.%M() - %msg%n%throwable{short.lineNumber}" /> </Console> <RollingFile name="rollingFile" fileName="${logPath}/${rollingFileName}.log" filePattern="${logPath}/${rollingFileName}_%d{yyyy-MM-dd}.log"> <PatternLayout pattern="[%highlight{%-5level}] %d{DEFAULT} %c{1}.%M() - %msg%n%throwable{short.lineNumber}" /> <Policies> <!-- Causes a rollover if the log file is older than the current JVM's start time --> <OnStartupTriggeringPolicy /> <!-- Causes a rollover once the date/time pattern no longer applies to the active file --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile> </Appenders> <Loggers> <Root level="DEBUG" additivity="false"> <AppenderRef ref="console" /> <AppenderRef ref="rollingFile" /> </Root> </Loggers> </Configuration>
TimeBasedTriggeringPolicy
interval (integer) - How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers would occur every 4 hours. The default value is 1.
modulate (boolean) - Indicates whether the interval should be adjusted to cause the next rollover to occur on the interval boundary. For example, if the item is hours, the current hour is 3 am and the interval is 4 then the first rollover will occur at 4 am and then next ones will occur at 8 am, noon, 4pm, etc.
Source: https://logging.apache.org/log4j/2.x/manual/appenders.html
Output:
[INFO ] 2018-07-21 12:03:47,412 ScenarioHook.beforeScenario() - Browser=CHROME32_NOHEAD [INFO ] 2018-07-21 12:03:48,623 ScenarioHook.beforeScenario() - Screen Resolution (WxH)=1366x768 [DEBUG] 2018-07-21 12:03:52,125 HomePageNavigationSteps.I_Am_At_The_Home_Page() - Base URL=http://simplydo.com/projector/ [DEBUG] 2018-07-21 12:03:52,700 NetIncomeProjectorSteps.I_Enter_My_Start_Balance() - Start Balance=348000
A new log file will be created daily with previous day automatically renamed to:
cucumber_yyyy-MM-dd.log
In a Maven project, you would put the log4j2.xml
in src/main/resources
or src/test/resources
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With