Log4j - Can't find the log file - java

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!

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.

Related

Where on the client to place the log4j.properties file?

A Java application makes use of the Apache logging but am unable to turn it on.
The application is a Java Web Start application which uses a signed Jar file so I am unable to make any changes to the Jar file or the code.
A log4j.properties file with the following content was created and place in all the Java_Home/bin directories on my machine to ensure it will be picked up.
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=c:/MyLoggingDir/application6.log
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.append=true
log4j.rootCategory=ALL, rollingFile
When the application starts no output is visible in the Java Console and no files are created in the c:/MyLoggingDir directory.
Is there another requirement missing that needs to be set in order to obtain output?
From the documentation of log4j:
Log4j will inspect the "log4j.configurationFile" system property and,
if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
So, for example, if you put the property file in C:\Configuration\log4j.properties, you should run your application with the flag -Dlog4j.configuration="file:C:\Configuration\log4j.properties"
See also this other answer.

Log4j doesn't write logs to file

I'm using slf4j over log4j.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
The log4j.properties is in WEB-INF folder and has the following content:
log4j.rootLogger=DEBUG, stdout, file
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
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
On another enviroment with same version of Tomcat the same respository works just fine, logging to the file...
You said that this same configuration file was working fine on a different environment. Can you give a little detail on the differences between the environments. e.g. are they both on Windows? The fact that is was working fine on another environment suggests that your configuration is fine, but there is some issue with your environment. Here are a few things to check:
Does the tomcat process has permissions to write to that file - i.e. are administrator rights required to write to D:\logs\log4j.log?
Make sure that there aren't any temporary files lying around tomcat's directories which may be preventing your changes from taking effect. To be sure, stop tomcat, delete the expanded war from %CATALINA_HOME%\webapps, delete the contents of %CATALINA_HOME%\temp and %CATALINA_HOME%\work
Check that you don't have any other log4j config files on your classpath, as these could be overriding your log4j file and preventing it from taking effect. To double check this, you could try temporarily removing your log4j.properties file to see if you get messages saying that the logging system is not initialised properly.
Perhaps your log4j.properties isn't being read at all. If it's not found, then log4j will use a default configuration and you might not even know about it.
Try to pass this line
-Dlog4j.configuration=file:///D:/yourPathToFile/log4j.properties
as a VM Argument in your run configuration and see if it helps.
For a Maven Based Project keep your log4j.properties in src/main/resources
I had the same problem, putting the log4j.log in source work. I thought it was getting compiled in the EAR file, but was not. Assuming it was using default.
Instead of:
log4j.appender.mainAppender.File=mainloggs.log
log4j.appender.mainAppender.Append=true
use:
log4j.appender.file.File=mainloggs.log
log4j.appender.file.Append=true
Your log4j.properties file should be in WEB-INF/classes, not WEB-INF. WEB-INF is not in the web application classpath, but WEB-INF/classes is.
Try with
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.Append=true
If it not working. Please put your web.xml
The issue might be coming when you won't add the log4j.properties file in your classpath. To fix the same follow the steps below:-
Create a folder resources in project and put your log4j.properties in it.
Go to project properties --> Java Build Path -->Click on Add Folder -->Check the resources folder -->Click ok and its done. It should solve your problem.
I had the same problem. It used to work a while ago but not working now.
I guess this is not a problem with log4j2 or slf4j(as was in my case) but instead with the relative path to the config file.
If you have updated java or update IDE or changed some system variable or relative path of the folders then your problem is similar to mine.
Possible fixes/workarounds -
Try including the log4j2 configuration file in the artifact while
building it.
Alternatively you can get config from propertied file and hardcode
the logging in code itself - This was what worked for me.
Try using the scheme when mentioning file paths on windows. Does not need to mention it this way on other platforms but on windows you need to. I have observed issues regarding file paths without scheme.
log4j.appender.file.File=file://d:/logs/log4j.log
So I had a similar issue with the .log file not being written to when writing a web app on tomcat. In the end I changed the file path to be relative with something like .\log.log. Then I noticed that when I ran the program using tomcat 7 I found the log file in the \bin directory instead of the \log directory inside my tomcat installation.
log4j.appender.file.File=${user.home}\\Logs\\mylogfile.log
I had the same issue. Turns out, I have another web application inside Tomcat that contain another log4j.properties file. After removing this application, things are working as expected
I had this problem when migrating log4j1 to log4j2 through the Log4j 1.x bridge (log4j-1.2-api)
My logj1 configuration was correct and the log4j2 and bridge JARs were correctly placed but I forgot this step. So adding this JVM parameter worked:
-Dlog4j1.compatibility=true

Log is not working inside the java file. But works inside the servlet

I have issue in executing the logs. I have two projects: one is a servlet and the other one is a simple java file. private static Logger logger=Logger.getLogger(myServiceServlet.class); This was working fine. Its writing log wen i start running the Tomcat server. But the same thing is not achieve in my simple java file, it converted into WS and try to deploy in axis/Tomcat server
Servlet is directly running in Tomcat server.
But java file is converted into Webservice.aar inside the Axis2--->Tomcat server.
Log Properites
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=C:/res backup/apache-tomcat-6.0.35/webapps/mylog/logs/servicelog.log
log4j.appender.R.MaxFileSize=2048KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=50
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n`
The .aar files are not running in the same context as the JARs in WEB-INF/lib.
They share some kind of context as you can load references to the WEB-INF/lib classes, however its not 100% consistent at this point.
I would recommend you build some kind of wrapper in your .aar files that do nothing else then forwarding the calls into methods of JAR files that are situated in WEB-INF/lib.
You just need to make sure you load the reference from the .aar to the methods in WEB-INF/lib context correctly.
To throw errors in the .aar classes I would recommend you catch them and then throw an "AxisFault".
throw new AxisFault(err.getMessage());
http://axis.apache.org/axis2/java/core/api/org/apache/axis2/AxisFault.html
That way we have have been able to solve almost all our issue with the context of .aar files.
Sebastian

Reduce logs of log4j in catalina.out

I am using the log4j API for logging in my Java based Tomcat application and using version 1.2.14 (log4j-1.2.14.jar). The problem is that it creates more logs in the catalina.out log file, which are very micro level logs and not required for me. I am facing the problem of memory in my machine, and I want to reduce the logging of the log4j logger. Is there a way to reduce logs such that I can save my memory?
Setting a properties or a XML configuration file will allow you to control log4j output as stated in the log4j manual.
An example of the properties file would be like this
# Root logger option
log4j.rootLogger=**ERROR**, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\mylogging.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{ABSOLUTE} %5p %c{1}:%L - %m%n
Note the rootLogger parameter, as it controls how much information you get, whether you need only error messages or everything down to information messages.
In order to set it properly, look in Log4j Configuration Using Properties File for extra information as you can also use it in the XML version (see Multiple Appenders Using XML File). Tip: place it in src/ or in project root.
You will have to look at CATALINA_HOME/conf folder for log4j.properties and raise the logging level to say, warn or error depending upon your needs

Why can't tomcat find my log4j.properties file?

My web app seems to be working fine. I decided to add some logging. When I deployed the war file, the application failed with an exception because tomcat didn't know about log4j.jar.
I added the jar to the WEB-INF/lib folder and redeployed. The exception went away. This lets me know that WEB-INF/lib is on the classpath.
However, now logs/catalina.out has the following typical error message:
log4j:WARN No appenders could be found for logger (com.this.that.Validate).
log4j:WARN Please initialize the log4j system properly.
So, I copied my log4j.properties file into the WEB-INF/lib folder.
The log4j warning still gets displayed, and nothing is getting logged as far as I can tell.
I'd appreciate some guidance on solving this. I'm chasing my tail.
Update
I believe my first issue is resolved - I no longer get the warning message. I think what was happening was that I had a unix shell with a pwd in the exploded file hierarchy and this prevented Tomcat from successfully removing the old project.
LOG4j
My log4j.properties file is
log4j.rootLogger=DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=prog1.log
log4j.appender.FILE.MaxFileSize=100KB
# Keep one backup file
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%p %t %c - %m%n
Resolution
I changed the ' log4j.appender.FILE.File=prog1.log' line to instead have an explicit path to the tomcat logging folder.
Thanks all, your ideas helped me solve this and get 3.18% smarter.
Copy log4j.properties into WEB-INF/classes.

Categories