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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • log4j.appender.FILE.File=${log}/log.out is ${log} variable set ? – learningJava Mar 04 '15 at 16:01
  • 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? – AndreaNobili Mar 04 '15 at 16:04
  • 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. – learningJava Mar 04 '15 at 16:07
  • tutorial source : http://www.tutorialspoint.com/log4j/log4j_sample_program.htm – LoveMeow Oct 06 '15 at 08:48

2 Answers2

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.

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.

Md Moin Uddin
  • 355
  • 5
  • 12