Richard Chen Richard Chen - 7 months ago 146
Java Question

log4j 2: log file being created, but not being written to

I am using log4j 2. I am trying to determine why my log file is not being written to, but my console is. This is the output I have to the console:

2016-04-25 12:26:07,142 INFO [main] helperCode.LogPlus (LogPlus.java:50) -
----------------------------------------------------------------------------
------------ The test is starting now at 2016-04-25-12-26-07-135 -----------
----------------------------------------------------------------------------

2016-04-25 12:26:07,151 INFO [main] helperCode.LogPlus (LogPlus.java:50) -
--------------------------------------------------------------------------
------------ METHOD loginBadPasswordGoodUsername_3 starting: ------------
--------------------------------------------------------------------------


The issues I've considered already:


  • The output is not repeating itself, so the log output being redirected is not an issue.

  • I am NOT using java.util.logging.Logger anywhere in my project;

  • The output of the log file is formatted in log4j style rather than JUL style, so I know that JUL is not overriding it;



Here is my XML config file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FileLogger" fileName="${sys:logFilePath}" append="false">
<PatternLayout pattern="%d %t %-5p %c{2} - %m%n" />
</File>
<Async name="Async">
<AppenderRef ref="FileLogger" />
</Async>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="automationFramework" level="trace">
<AppenderRef ref="FileLogger" />
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>


I am directing the output to the file located at the system variable "logFilePath".

I am not sure what is going on, and the config files shown on answers for similar questions are not in XML format, so I am not sure how to commute the config file code to XML.

Answer

In your case as mentioned in the comment you do not have the package called applicatiionFramework. So change the name attribute by the package name of the classes where you want to record log events.

 <Logger name="your package" level="trace">
                <AppenderRef ref="FileLogger" />
        <Root level="trace">
            <AppenderRef ref="STDOUT" />
        </Root>
 </Logger>

For more information visit log4j-manual-configuration