kmon
kmon

Reputation: 55

Where I have to put the file for using file appender log4j2

I am new to stackoverflow. I am trying to set up log4j2 in my maven web application and I want to use file appender. I found examples showing like that..

<File name="MyFile" fileName="logs/app.log">      
<PatternLayout> 
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>

So. I create a folder "logs" and put app.log inside somewhere in my application. But log data are not written to the file.

When I change to write in console for testing purpose, I can see the log data in console.

I am not so sure where I have to put that "logs" folder.

My application directory is like that

src/main/java
src/main/resources
src/main/webapp
src/test/java

Thanks for your help.

Upvotes: 0

Views: 1645

Answers (2)

Remko Popma
Remko Popma

Reputation: 36794

Can you give your full log4j2.xml file?

By default, the File appender is bufferedIO=true, and immediateFlush=true, so output should be flushed to disk immediately. You could try adding an explicit immediateFlush attribute:

<File name="MyFile" fileName="logs/app.log" immediateFlush="true">...  

The logs folder is relative to your app's current directory. Where exactly that is may depend on your web app server. You could find out by logging the result of System.getProperty("user.dir") to the Console...

Another thing to try is to specify an absolute path for the log file and see if the file is still not generated.

Upvotes: 3

qieqie
qieqie

Reputation: 1

the log4j2 config filename should be log4j2.xml and try the path "src/log4j2.xml"

Upvotes: 0

Related Questions