Why I obtain this FileNotFoundException when I try to use log4j to write into a file?
Asked Answered
S

2

5

I am absolutly new using log4j and I have the following problem.

I am trying to print the logging line into a file named log.out.

So I create the following log4j.properties configuration file:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

The problem is that, when I perform my application and when it incurs in a logging operation, something like this:

logger.debug("INTO main()");

I obtain the following exception into the console (the error message related to the log.out file is access denied (in italian Accesso negato):

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: \log.out (Accesso negato)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at org.apache.log4j.FileAppender.setFile(FileAppender.java:289)
        at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:163)
        at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:2
56)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.j
ava:132)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.j
ava:96)
        at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigura
tor.java:654)

and don't write nothing into my log.out file (that I have created manually). This log.out file is at the same level of the performed jar file that represent my application.

Why? What am I missing? How can I solve this issue?

Tnx

Sailer answered 4/3, 2015 at 15:53 Comment(4)
log4j.appender.FILE.File=${log}/log.out is ${log} variable set ?Paten
mmm no. I thought that automatically referers to the position of my application. Exist a way to say to log4j that the file have been created into the folder that contains the executed jar?Sailer
just hardcode some folder location in log4 property file. where you know your user has access . Or just write log4j.appender.FILE.File=log.out the file will be created in the default directory of the user.Paten
tutorial source : tutorialspoint.com/log4j/log4j_sample_program.htmMessalina
K
6

I think $log is empty and it's trying to create a file on root and you are running program as a normal user. give it a check.

Kraft answered 4/3, 2015 at 16:4 Comment(3)
On the root of what? I want that the log file is created at the same level of the performed jar (my application)Sailer
Can you try it using only this log4j.appender.FILE.File=log.out it should solve permission related issue.Kraft
now you also might wanna check your $log variable. It seems it's empty.Kraft
F
5

For logging when using properties file following config should work. For the tomcat application server, please use ${catalina.base} to get the tomcat base directory.

log4j.appender.FILE.File=${catalina.base}/logs/debug.log

For XML config the syntax might be like the following

<param name="file" value="${catalina.base}/logs/debug.log"/>

If you are using Tomcat as your application server, I hope it will work.

Flashy answered 27/5, 2015 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.