I'm trying to configure logging in my app where log4j.properties is put to /resources folder an has the following content:
#log level
log4j.rootLogger=ERROR, file
log4j
#
log4j.appender.file=org.apache.log4j.RollingFileAppender
#
log4j.appender.file.File=D:\agent\temp\panel log.log
#
log4j.appender.file.MaxFileSize=1MB
#
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
and on my app start I see the following in the console:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
what does this mean and how to fix it?
I had the same problem, I was using tomcat 7.
I solved the problem by adding conf path to the /bin/setenv.sh (if you are on linux, create the file if it does not exist) like this:
export JAVA_OPTS="$JAVA_OPTS -Dconfig.location=file -Dlog4j.configuration=file:///opt/apache-tomcat-7.0.61/lib/log4j.properties"
if you are on windows edit or create setenv.bat and write something like this (not sure about the path format):
set JAVA_OPTS=%JAVA_OPTS%-Dconfig.location=file -Dlog4j.configuration=file://c:/opt/apache-tomcat-7.0.61/lib/log4j.properties
Forgot to say: this will affect all the apps on the server
Try putting the log4j.properties in the root of your source folder. Or alternatively add resources to your classpath.
The root problem is log4j not seeing your config file, so you need to make it accessible.
Extract from log4j manual:
Default Initialization under Tomcat
The default log4j initialization is particularly useful in web-server
environments. Under Tomcat 3.x and 4.x, you should place the
log4j.properties under the WEB-INF/classes directory of your
web-applications. Log4j will find the properties file and initialize
itself. This is easy to do and it works.
You can also choose to set the system property log4j.configuration
before starting Tomcat. For Tomcat 3.x The TOMCAT_OPTS environment
variable is used to set command line options. For Tomcat 4.0, set the
CATALINA_OPTS environment variable instead of TOMCAT_OPTS.
i've changed log4j.properties to log4j.xml and put it to resources folder and now everything is correct. maybe my log4j version doesn't supprot .properties format
Related
I have a package called Resources in which I have placed my log4j.properties file. When I run my code, I get the following error:
log4j:WARN No appenders could be found for logger.
log4j:WARN Please initialize the log4j system properly.
How should I update the classpath to include Resource folder ?
Solution tried:
I added the log4j.properties file directly under 'source' and it worked.
Here is the contents of properties file:
#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=C:\\Users\\kagarwal\\Desktop\\Application.log
log4j.appender.dest1.Append=false
Right click on the folder, select Build Path then Use as a Source Folder.
Just remember that when you will deploy you will need to create a directory for resources and add it to class path.
You should also consider to use maven for your java project, it may seem overkill initially but it will pay off in the long run.
We have about 19 applications in our weblogic 8.1 server. Each application is an .Ear application with a few ejbs, mdb etc., Each application has a log4j properties defined in a .properties file somewhere in the filesystem.
We keep getting this error(below) in the server logs for all the applications. This exception is not caught in the application, the flow of the application does not stop, but we miss a log statement or two when this exception occurs.
log4j:ERROR Attempted to append to closed appender named
Note: I've looked up in the internet, i've not seen duplicate log4j.jar files anywhere in the system. I can post the .properties files if you need.
EDIT: Including a couple of log4j properties. These property files are same for all the other applications as well.
1st LOG file.
# Define the root logger with appender file
log=.//SI_AppLogs
log4j.rootLogger=WARN, SomeApp
log4j.logger.SomeAppLoggers = INFO, SomeApp
# Define the file appender
log4j.appender.SomeApp=org.apache.log4j.DailyRollingFileAppender
log4j.appender.SomeApp.File=${log}/CPEPortBasedAuthorization.log
log4j.appender.SomeApp.DatePattern='.'yyyy-MM-dd
log4j.appender.SomeApp.Append=true
# Define the layout for file appender
log4j.appender.SomeApp.layout=org.apache.log4j.PatternLayout
log4j.appender.SomeApp.layout.conversionPattern=%d %-5r %-5p [%c] (%t:%x) %m%n
log4j.additivity.org.apache=false
log4j.additivity.SomeAppLoggers = false
2nd Log file
# Define the root logger with appender file
log=.//SI_AppLogs
log4j.rootLogger=WARN, AnotherApp
log4j.logger.AnotherAppLoggers = INFO, AnotherApp
# Define the file appender
log4j.appender.AnotherApp=org.apache.log4j.DailyRollingFileAppender
log4j.appender.AnotherApp.File=${log}/CPEPortBasedAuthorization.log
log4j.appender.AnotherApp.DatePattern='.'yyyy-MM-dd
log4j.appender.AnotherApp.Append=true
# Define the layout for file appender
log4j.appender.AnotherApp.layout=org.apache.log4j.PatternLayout
log4j.appender.AnotherApp.layout.conversionPattern=%d %-5r %-5p [%c] (%t:%x) %m%n
log4j.additivity.org.apache=false
log4j.additivity.AnotherAppLoggers = false
Update:
I've not seen duplicate log4j.jars in the class path. I've ran the following code and verified.
String classpath = System.getProperty("java.class.path");
I've not seen any two log4j.properties files have the same Appender defined. Verified it by search the whole weblogic deploy directory.
It was a tough find for me as I have multiple applications in the VM. As per the article log4j jar file needs to be added to each application individually. Most of the classes in the log4j are singleton, so the root logger is getting created as a singleton for all the applications.(EAR applications). I've modified my application to have one jar per application, so far no issues.
I have a requirement wherein I need to put log4j.properties file inside a folder named 'resources' under src directory.
log4j.properties
log = D:\\XYZ
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
resources.log4j.appender.FILE.File=${log}/logger.txt
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
but my when I navigate through my application nothing gets logged into logger.txt file..I have explicitly not given any configuration for log4j.properties file...
Please point my mistake and suggest solution..I am new to this concept..
Thanks
Notice that log4j is using a system property in the configuration, ${log} this needs to be defined when the VM is started. Essentially this is a variable piece of the configuration that is defined by a configuration parameter passed to the application.
java -Dlog=C:\SomeDirectory\ theApp
If your using Eclipse system properties can be set via the Run Configuration for your application.
Maybe it looks silly to ask this but I am confused. I referred to Configuring Log4j property but it doesn't seem to help.
I have written a simple web service HelloWorld. And while running it I am getting the error something like this :
log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet).
log4j:WARN Please initialize the log4j system properly.
I am not sure why its happening.
I am generating the web-service using Eclipse and deployed in Tomcat 6.0. I check on Axis Developer's Guide and according to them
log4j.configuration=log4j.properties
Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties. A log4j.properties file is provided in axis.jar.
I didn't find log4j.properties in the axis.jar.
Any help on this?
Those messages are something tricky, enough so that people created this to make it clearer:
https://issues.apache.org/bugzilla/show_bug.cgi?id=25747
What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties (or log4j.xml) file, but also if the file is fine and dandy but its content is not complete from a configuration point of view.
The following paragraph is taken from here:
http://svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html
Logging output is written to a target by using an appender. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:
log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.
Log4j does not have a default logging target. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.
You can find info on how to configure the root logger (log4j.rootLogger) in the log4j documentation, basically adding something as simple as this at the beginning of the file:
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
This should clear those WARN messages you get on startup (make sure you don't already have an appender named stdout; also be carefull of what level you give the root logger, debug will be very verbose and every library in your app will start writing stuff to the console).
As about the log4j.properties/log4j.xml, I suggest you place this file in /WEB-INF/classes as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc). You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes.
You have to create your own log4j.properties in the classpath folder.
Well, if you had already created the log4j.properties you would add its path to the classpath so it would be found during execution. Yes, the thingy will search for this file in the classpath.
Since you said you looked into axis and didnt find one, I am assuming you dont have a log4j.properties, so here's a crude but complete example.
Create it somewhere and add to your classpath. Put it for example, in c:/proj/resources/log4j.properties
In your classpath you simple add .......;c:/proj/resources
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# 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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:/project/resources/t-output/log4j-application.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
If the below statment is present in your class then your log4j.properties should be in java source(src) folder , if it is jar executable it should be packed in jar not a seperate file.
static Logger log = Logger.getLogger(MyClass.class);
Thanks,
Warning No appenders could be found for logger means that you're using log4j logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.
There are three ways to configure log4j: with a properties file (log4j.properties), with an XML file and through Java code (rootLogger.addAppender(new NullAppender());).
If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.
classpath
Here are some command suggestions how to determine your classpath value:
$ echo $CLASSPATH
$ ps wuax | grep -i classpath
$ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf
or from Java: System.getProperty("java.class.path").
Tomcat
If you're using Tomcat, you may place your log4j.properties into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.
Solr
For the reference, Solr log4j.properties looks like:
# Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
If you are using Logger.getLogger(ClassName.class) then place your log4j.properties file in your class path:
yourproject/javaresoures/src/log4j.properties (Put inside src folder)
You can configure log4j.properties like above answers, or use org.apache.log4j.BasicConfigurator
public class FooImpl implements Foo {
private static final Logger LOGGER = Logger.getLogger(FooBar.class);
public Object createObject() {
BasicConfigurator.configure();
LOGGER.info("something");
return new Object();
}
}
So under the table, configure do:
configure() {
Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
}
I'm trying to permit debug logging per a particular class using Log4j, and I've got the following:
log4j.rootLogger=stdout, daily
log4j.logger.com.mycompany.myapplication.mymodule=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.stdout.threshold=warn
log4j.appender.daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.daily.layout=org.apache.log4j.PatternLayout
log4j.appender.daily.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.daily.File=/some/file/path/stuff
log4j.appender.daily.DatePattern=MMdd'.log'
log4j.appender.daily.threshold=info
If this is the WEB-INF/classes/log4j.properties file as part of tomcat should debug messages from mycompany.myapplication.mymodule be seen or do the entries later in the file override it? (or am I changing the logging level per class completely wrong?)
Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?
You tell your class to log at level DEBUG but tell the appenders to ignore anything below WARN and INFO, so you won't see the log messages.
As for the order in which the log4j.properties will be discovered:
WEB-INF/classes
Any JAR in WEB-INF/lib
common/classes (in the tomcat directory)
Any JAR that you put into common/endorsed
Any JAR that you put into common/lib
shared/classes
Any JAR that you put into shared/lib
The discovery will stop with the first file found.
For your first question, I would say that log4j.appender.daily.threshold=info is a problem. I might not pick any DEBUG message.
Normally, we don't give a threshold to an appender, levels are configured for loggers.
I suggest you use the log4j.xml instead of the log4j.properties.
In addition to the useful validation, it adds some interesting features or default values.
I don't recall exactly which though...
Yes, you seem to be configuring your individual loggers correctly.
No, they will not be seen because you are declaring thresholds on both of your appenders which are above DEBUG:
log4j.appender.stdout.threshold=warn
log4j.appender.daily.threshold=info
As for "Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?", I'm not sure what you mean by this. log4j attempts to load a log4j.xml or log4j.properties from the classpath, and will use whichever it finds first. IIRC, entries in the shared lib folder of Tomcat supercede the classpath of each application. If you have a log4j configuration in both a jar in WEB-INF/lib and in a plaintext file in WEB-INF/classes, then you are essentially flipping a coin at runtime as to which will be used.
I think you have to give mymodule an appender, as in
log4j.logger.com.mycompany.myapplication.mymodule=DEBUG, Daily
log4j.additivity.com.mycompany.myapplication.mymodule=false
Also, if you set the additivity to false, you make sure messages get logged only
by the appender you specify.
The 'root' of a jar is normally not included in the CLASSPATH, so a log4j.properties within a jar should be invisible to the JVM.