Log4j not appending file extension - java

I've set my log4j.properties file to this configuration below
log4j.rootLogger=INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
The issue is when creating my logs (I'm using OS x Lion and Tomcat 6.0.32), the logs are created without the extension, just a final dot.
However, if I use this same configuration in a Windows box, it does the work OK.

log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
needs to be:
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.log

Related

Jenkins does not show log4j logs

my problem is that I can not see the JUnit log4j logs of my integration tests, when I run them on jenkins.
When I start them from my local eclipse everything works fine. I tried Java Utils Logging before that worked fine as well.
I tried lots of log4j.properties settings. The current one looks as follows:
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE
# Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis2.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
What am I doing wrong? The log4j.properties file is placed in /src/main/resources. As there is no No log4j appenders-warning in the log, I assume that it is in the classpath and consumed.
I found the solution in Logging while testing through Gradle
The problem was in the gradle settings:
testLogging {
events 'passed', 'failed', 'standardError'
showStandardStreams = true
}

Multiple process using the same log4j.properties

I'm actually on a project using slf4j/log4j.
For that, we use log4j.properties files to configure the logging, especially the DailyRollingFileAppender.
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.com.thales.ecosystem=DEBUG,stdout,file
log4j.additivity.com.thales.ecosystem=false
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# this is set dynamically
log4j.appender.file.File=${log.basedir}/decoders/nm-flight-decoder.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} v${release.version} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
# 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.SSS} v${release.version} %-5p %c{1}:%L - %m%n
And in code, we use the loggerFactory :
private static final Logger LOGGER = LoggerFactory.getLogger(xxxxxxxx.class);
But we have now to deal with multiple (but limited in number) identical process.
So the question is : how can we have two process (with the same class)
with only one log4j.properties per class, writting on a file each, and keeping the DailyRollingFileAppender ?
Thank's !
Make the file name in the configuration a property, and start each process with a different value for that property.
log4j.appender.file.File=${log.basedir}/decoders/${proc}.log
Start one process with -Dproc=nm-flight-decoder, and the other one with a different value.
(Also, consider upgrading to Log4j2. Log4j 1.2 has been End of Life since summer 2015 (archived here) and is known to be broken on Java 9 (archived here).)

How to make simple log4j configuration?

I want to make to log file to my application.
I try Appach log4j for this. In Java code i says:
Logger log = Logger.getLogger(uploadfile.class);
log.debug("Start");
I create file src\log4j.properties with:
# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, R, A
log4j.rootLogger=INFO, A
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.MaxFileSize=100mb
log4j.appender.R.MaxBackupIndex=1000
log4j.appender.R.File=c:/uploaded_files/server.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.Threshold=INFO
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.org.hibernate.type.BasicTypeRegistry=INFO
log4j.logger.com.opensymphony.xwork2.config.providers.XmlConfigurationProvider=INFO
I copy this there. And i get error:
13.02.2013 9:03:34 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [uploadfile] in context with path [/gis-mrsk-portlet] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.LogManager
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at test.uploadfile.doPost(uploadfile.java:35)
What i doing wrong? And how to configure log4j for simple?
UPDATE
If i use this config i get log file but its empty:
Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE, LOGFILE
Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:/uploaded_files/LogFile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
It seems correct.. have you set correctly the log4j Lib in your path?
The problem is not with your configuration, the problem is the non-existant support of log4j for catalina it's not as easy to get it to work together as it should be. To make log4j work with catalina follow the documentation available at http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j.
EDIT
To summarize the link I gave:
[...] put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.
Download or build tomcat-juli.jar and tomcat-juli-adapters.jar that are available as an "extras" component for Tomcat. See Additional Components documentation for details. http://tomcat.apache.org/tomcat-6.0-doc/extras.html
After following those steps your example should already work.
/EDIT
If you want the easy part in logging I would suggest using logback which comes with native servlet support for tomcat based applications. If you want to go one step further in logging: clusterlog.net (full disclosure I own part of that company)
java.lang.NoClassDefFoundError
It looks like your Apache does not know definition of org.apache.log4j.LogManager. Have you put required jar in Apache classpath?

Log4j output on console instead of configured file

I configured Log4j to log into a file but it is instead logging to stdout.
it creates the log file but it does not write to it, instead to stdout.
Here is my config file:
log4j.rootCategory=INFO, file, mail
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/path-to/jobs-batch.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[jobs-batch] %p [%t] %c{1}.%M(%L) | %m%n
log4j.logger.org.springframework.jdbc=WARN
# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=xxx#xxx.com
log4j.appender.mail.From=xxx#xxx.com
log4j.appender.mail.SMTPHost=mail.xxx.de
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.Subject=Jobs Batch Error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %-5p %c %x - %m%n
any ideas?
Thanks
Adjust to suite your needs:
log4j.rootCategory=DEBUG, C
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{MMddyyyy HH:mm} %-5p %c{1}:%L - %m%n
log4j.appender.C.Target=System.out
Things to check:
What is the name of your Log4j property file? The default is log4j.properties, which is what the Log4j library will look for on startup.
Related to what Thorbjorn asks in his comment, where is the log4j property file located? Placing it in the default package will ensure it's found (by default Log4j searches the classpath).
Optionally specify the location of the log4j property file using the log4j.configuration property when starting the JVM.
i was stupid by myself.
i was using the -Dlog4j.configuration to set my file.
BUT i was also configuring log4j programmatically to use the SAME configuration file.
i still do not understand why it did behave like that since i was just setting the SAME configuration file twice. But once i did it only once it worked as expected.

Web Service and Log4J Output to File

So, I have the log4j configurations set up and it output to the console just fine. But I need it to output to a file now. I am using Axis2 in Eclipse Galileo.
I edited the default log4j.properties file to this:
Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
log4j.rootCategory=INFO, CONSOLE, LOGFILE
Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:\Users\*username*\Desktop\Android\Android_WS\src\wtp\AndroidWS_Log.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
But the output is still not getting to the Log file. Any help would be much appreciated. Thanks Guys!
Remove line 2:
log4j.rootCategory=INFO, CONSOLE
Also, try using a forward slash, '/' for the directory separator:
log4j.appender.LOGFILE.File=C:/Users/*username*/Desktop/Android/Android_WS/src/wtp/AndroidWS_Log.log
Make sure that your server has write access rights to the log file.

Categories