I have read several times through the Log4J documentation and I am currently testing an example given by mkyong, but no matter what I do, Log4J keeps placing the output on my console.
This is the current file:
# DRAGONS AHEAD
# Root logger option
log4j.rootLogger=debug, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/pedro/Desktop/log4j.txt
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
# END
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=application.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
I keep thinking that I am doing something wrong, but I just cannot see what it can be. Can someone help me find what I am doing wrong?
Are you very sure that this property file is loaded in run time. You can loop through the loggers/appenders in runtime to see whether this configuration is loaded or not.
The only suggestion i have is to check the way you initialize log4j. Are you using BasicConfigurator.configure(); ?? Then your property settings wont get loaded.
Related
I would like to print log4j to both console and logfile, i have the following log4j.properties file:
# Root logger option
log4j.rootLogger=DEBUG,WARN,INFO,ERROR file, stdout
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\jeff\\logfile.txt
log4j.appender.file.MaxFileSize=20MB
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
# Redirect log messages to console
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
I use the Logger in the simple way:
final static Logger logger = Logger.getLogger(MyClass.class);
logger.warn("my message");
The logging works perfectly fine on console but not in the file, actually i believe that the cfg file isnt read at all, note that the log4j.poperties file is in the root directory of the project.
In various tutorials i have read that is necessary that log4j.properties is in the classpath, there is no need to manually register it from java code.
Any advice on how can i tell my logger to read the properties from the cfg file?
Don't try to beat me, I am running away... :-)
You missed a comma in between ERROR and file.
log4j.rootLogger=DEBUG,WARN,INFO,ERROR file, stdout
I have a webservice program which has multiple classes talking to each other.
After running the program using tomcat in eclipse, I copy the wsdl url in soap UI to test it out and if there's any exception then it has to be in printed in a log file as I am file appender logic from log4j. Spent almost three days with no luck
Here is log4j.properties file:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE, theConsoleAppender
log4j.category.com.wataniya.Wataniya_WebServiceSkeleton=DEBUG, FILE
### direct log messages to stdout ###
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=log.txt
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=true
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern= %d{dd MMM yyyy HH:mm:ss,SSS} %c %p %t %m%n%n
Imported the required jar files in to build path in eclipse. More info can be found at this link in importing the jar files and configuring the properties file: Link
In a standalone application I'm using log4j for logging and have configured it with the following properties file:
# Root logger option
log4j.rootLogger=WARN, file, stdout
log4j.logger.com.tr = INFO
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log4j.log
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
In Windows this works as expected, producing a log4j.log file in the current working directory. When I run the application in Linux, it reports no error but does not generate the log file. The logger outputs to console just fine.
I tried programatically changing the File property from a relative path to an absolute path and that changed nothing.
I've also verified that if I supply an invalid file name the tool reports an error saying the file cannot be generated. So I'm baffled as to why no error is being reported and no file is being generated.
I've searched my hard disk for the file to verify it's not being generated elsewhere, and it's not.
Any help here would be much appreciated.
i have the following log4j configuration:
# Root logger option
log4j.rootLogger=file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.Threshold=WARN
log4j.appender.file.File=logs/log.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.Append=true
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.Threshold=INFO
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
Logging into stdout works fine but nothing is logged into my file.
There are definately outputs for the file but nothing happens.
Why isnt he logging into my file?
you can change your configuration file like this.
# Root logger option
log4j.rootLogger= ALL,file, stdout
And log4j.appender.stdout.Threshold=INFO change level like ALL its print all level logs TRACE to FATAL
One reason could be that the Threshold for the stdout appender(INFO) is lower than the file appender(WARN). So, if there are only INFO messages being logged, you will only see them in the stdout and not in the file.
It depends on how you are running it. Is this just a normal java program? For example, if you are trying to run it within a container (such as Karaf) then sometimes they can have special log destinations set.
This is my log4j.properties.
# 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=${catalina.home}\MyLog\PmcDemo.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
I am using tomcat 6.0, in my application I have used Logger from log4j yet I don't see any output on server console or in the log file. My application is using struts2 as front end, Spring framework as middle layer and hibernate as the end layer. I don't see my application logging how can I enable it in tomcat 6?
You need to switch the backslashes for forward slashes:
${catalina.home}/MyLog/PmcDemo.log
or to escape them
${catalina.home}\\MyLog\\PmcDemo.log
If that doesn't help, let us know the structure of your project and where the log4j.properties file is stored.
Try this steps,
If running Tomcat 6.x:
1. If you have not already done so, modify the <<TOMCAT_HOME>>/conf/catalina.properties file so that the shared classloader mechanism work the same as Tomcat 5.x.
2. To do this, verify that the entry beginning with shared.loader= reads shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar If running Tomcat 5.x or higher:
If running Tomcat 5.x or higher:
3. If it does not already exist, create a "shared/classes" directory under <<TOMCAT_HOME>>.
4. If it does not already exist, create a "shared/lib" directory under <<TOMCAT_HOME>>.
5. Copy log4j-###.jar into <<TOMCAT_HOME>>/shared/lib.
**Note:** Any specific version of log4j-###.jar should work. You can download the stable log4j version 1.2 installation from http://logging.apache.org/log4j/1.2/download.html
6. Copy a log4j.properties file into <<TOMCAT>>/shared/classes.
Example
To get a log file named "initiate.log" to show up in the <<TOMCAT_HOME>>/logs directory, an initial version of log4j.properties file is:
log4j.rootLogger=ERROR, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/initiate.log
log4j.appender.R.MaxFileSize=1000KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} 5-5p %c{2} - %m %n
log4j.logger.org.springframework=ERROR
log4j.logger.org.springframework.web.servlet.mvc=ERROR
#set to DEBUG to see hibernate and connection-pool logging output
log4j.logger.org.hibernate=ERROR
log4j.logger.org.apache.commons.dbcp=ERROR
#set to DEBUG to see Initiate-specific logging output
log4j.logger.com.initiatesystems=DEBUG
#set to DEBUG to set Initiate-specific verbose logging output
log4j.logger.verbose=ERROR
Quoted from: http://pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.datatrust.doc%2Ftopics%2Ft_datatrust_configuring_log4j_logging_apachetomcat.html