Richard Chen
Richard Chen

Reputation: 125

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:

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.

Upvotes: 3

Views: 5152

Answers (3)

Daniel Murphy
Daniel Murphy

Reputation: 3

For the complete noob, like myself, this line in Unknown's answer above:

    <Logger name="your package" level="trace">

is the key. When it says "your package" it really means the "package foo.bar" you declared at the top of the .java file where you created the Logger, not the class name you passed to create the Logger, like:

    LOGGER = LogManager.getLogger(MyClass.class.getName());

It took me a ridiculous amount of time today to get my log4j2 output going to a file, playing with various "name" values until the light bulb went on.

Upvotes: 0

Remko Popma
Remko Popma

Reputation: 36754

Glad the other answer helped. Do you need the named logger though? Why not just have the root logger? Also, you declared an Async Appender but are not using it. Is that on purpose? This is what I would suggest:

<?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>
        <Root level="trace">
            <AppenderRef ref="STDOUT" level="info" />
            <AppenderRef ref="Async" />
        </Root>
    </Loggers>
</Configuration> 

Note that you can also specify a level on the AppenderRef. I made the Console Appender level INFO to illustrate this point.

If you want the log to show location information and use Async loggers or appenders then you need to specify includeLocation="true" on the Async Appender.

Upvotes: 0

Unknown
Unknown

Reputation: 2137

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

Upvotes: 2

Related Questions