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

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.

Related

Cannot find log file in my project logs folder in spring mvc project

I use log4j in my spring mvc project and I create log file uing log4j.properties in the following.
# Root logger option
log4j.rootLogger= INFO, info, file, stdout, error
# Direct log messages to a log file [INFO]
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=.logs\\mylog.log
log4j.appender.info.Threshold=INFO
log4j.appender.info.MaxFileSize=1Kb
log4j.appender.info.MaxBackupIndex=10
log4j.appender.info.DatePattern='.'yyyy-MM-dd
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I want it to be stored in myapp/logs folder in my web application . what part i should for the file.
here is the directory
Right now it will be saved in the current working directory of your IDE (Eclipse or STS, I guess). Its not a good idea to put the file in the project structure, because that structure wont exist, once you deployed the app anywhere.
In Spring you can define a property in youe web.xml, to make the location variable:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>mywebapp.root</param-value>
</context-param>
Then add a variable to your config:
log4j.appender.info.File=${mywebapp.root}/logs/mylog.log
If you have to store it in the application, you can also define that programmatically, by makeing use of "ServletContext.getRealPath(".") to find out where your app is deployed, and then set the path to the file.
In the normal scenario web-app checks for your log file at the root of your class path. To load a file from custom location , have a look at below posts
Change location of log4j.properties
How to use custom file instead of log4j.properties
EDIT :
From op's comment to generate your log file in the projects path , where you have placed your logs directory . you can simply specify it as ,
log4j.appender.file.File= logs/mylog.log

Seperate logs for different jars in war file

I have a war file which is deployed on jboss server. This war contains some jar files.
What i need is to create seperate logs for different jars.
I am using log4j for logging purpose and also intergrated log4j properties file in each jar.
My log4j for every jar have different paths.
Still the logs are generated in server.log of jboss.
I need help to generate seperate logs for every jar.
If you want to make different log file for different jar, you will have to configure it in your log4j.properties file related to every jar(I assume that every jar have different package naming convention).
Here is an example like if you want to log message in different file whose package name starts with org.jar1 and org.jar2.
log4j.debug=true
log4j.logger.org.jar1=DEBUG,jar1logger
log4j.logger.org.jar2=DEBUG,jar2logger
log4j.additivity.org.jar1=false
log4j.additivity.org.jar2=false
log4j.appender.jar1logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar1logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar1logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar1logger.File=/var/log/mylog/jar1.log
log4j.appender.jar1logger.Append=true
log4j.appender.jar1logger.MaxFileSize=1024KB
log4j.appender.jar1logger.MaxBackupIndex=5
log4j.appender.jar2logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar2logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar2logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar2logger.File=/var/log/mylog/jar2.log
log4j.appender.jar2logger.Append=true
log4j.appender.jar2logger.MaxFileSize=1024KB
log4j.appender.jar2logger.MaxBackupIndex=5
Based on your requirement whether you want to create log on daily basis or on size basis you have to change your configuration.
Keep in mind if any jar whose package name in any jars start with org.jar1 then all logs will be written into jar1.log file.

Using simple Log4j.properties in my java code

I made a simple Hibernate 3.1 java code (in eclipse) which successfully creates a table in a database. I have slfj-api-1.6.0.jar and slfj-simple-1.6.0.jar in my project. But, I don't see any Log4j logging warnings or messages in the console output. How do I make log4j log things to my console ?
My tutorial says that one of the lines I should get in the console is -
log4j.logger.org.hibernate.type = debug
I saw a couple of posts online which tell me to place a Log4j.properties file in my java project, either in src folder, or make a resources folder inside src and put it there. I found one Log4j.properties in my computer and placed it in the resources folder. I am not sure if I should use the settings in that file or how to edit this file as per my requirement. How do I do this correctly ?
EDIT -
The log4j.properties file -
### 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{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - ###
log4j.rootLogger=warn, stdout log4j.logger.org.hibernate.info
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=debug
###log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
Thanks.
First of all, the file name must be in lowercase. The Log4j.properties file is wrong, it's log4j.properties. This must be put in the root folder of your project (in most cases the src folder).
Secondly, you need to download the log4j jar file if you want to use SLF4J bridge with log4j. Download the latest library that you require from Apache Logging Services. Don't forget that there are 2 versions of Apache Log4J, so I suggest you try version 1.x first.
Good luck!
One thing to note is that the file should be called log4j.properties and should be placed in the root of your src or resource folder (by default at least) ... you seem to be calling it Log4j.properties
I also think that you are missing the following dependency:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.1</version>
</dependency>
The resources folder is only an option if you identify it as a source folder on the project properties!java build path!source tab.
Slf4j does not use log4j to write to the logs unless you include the slf4j-log4j[version].jar (for example, slf4j-log4j12-1.7.7.jar) jar in your project. If you include this jar, then you must not include the slfj-simple-1.6.0.jar jar in your project.
Here is a link to the slf4j online manual

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

Categories