Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j - Can't find the log file

I'm having trouble with an application that crashes when I deploy it to other computers who are running JRE 1.7. When I run this inside of NetBeans (or even directly from the JAR file) on my PC, everything is fine. But on another computer it fails at specific events (button clicks) during execution.

So, I learned about logging using the log4j library. This gave me some information on a problem in my application, and the logging works perfectly, again on MY computer. But when I deploy the JAR file to other computers, who are only running JRE (Java 7 Update 17), I can find no traces of any log files.

Here is my log4j.properties file:

 # Root logger option 
 log4j.rootLogger=INFO, file, stdout  

 # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender
 log4j.appender.file.File=C:\logging.log
 log4j.appender.file.MaxFileSize=1MB
 log4j.appender.file.MaxBackupIndex=1
 log4j.appender.file.layout=org.apache.log4j.PatternLayout
 log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}
 %-5p %c{1}:%L - %m%n  

 # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.Target=System.out
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}
 %-5p %c{1}:%L - %m%n

On my computer, I can see the logging.log file right inside of the project folder. To that extent, everything works perfectly. However, on the user PC, there is no sign of this file at all. Not in C:\ (where I thought it would be), not in C:\Program Files (x86)\ or anywhere else. I've done a complete search of my hard disk, but nothing comes back.

Where should this file be stored? Are my properties set correctly? Very confused...

Thank you!

like image 672
AndroidDev Avatar asked Mar 25 '13 17:03

AndroidDev


1 Answers

In case anybody ever stumbles on this post, I wanted to document how I solved it.

First of all, as DwB correctly pointed out, the problem was indeed that the user's account did not have sufficient privileges to create the logging.log file. In my code, I had a catch block that was being executed and contained code to exit the system in the event of a file write error. Since it was exactly the logging detail was what I was trying to write, I was unable to get any output to clue me into this as the source of my problem.

Once I realized this, I just had to change where I was writing the log file. Rather than write it to the root (C:\) or to the folder where the jar file was (C:\Program Files\), both of which were places where I could not guarantee that a user would have full privileges, I decided to create a folder in their AppData path.

The very first line in my program is now a call to this function (using the generic name MyProgram):

private static void makeAppDataFolder() {
     File dir = new File(System.getenv("APPDATA") + "\\MyProgram");
     if (!dir.exists()) {dir.mkdir();}
}

This creates a folder called MyProgram in the user's account at \AppData\Roaming. Since this path is part of the user account path, I believe that the user will always be able to write to it, thereby solving the permissions issue. If anybody knows otherwise, please let me know.

Then, in my log4j.properties file, I change the line

log4j.appender.file.File=logging.log

to

log4j.appender.file.File=${user.home}/Application Data/MyProgram/logging.log 

This directs the file to the folder that I just created.

Once I did this, and added some well-placed logging messages, I was able to find my other issues, and now everything works just fine.

I hope that this helps somebody out. If anyone has any suggestions, please post.

like image 67
AndroidDev Avatar answered Nov 16 '22 01:11

AndroidDev