Log4J: Warn No Appenders could be found for Logger error - java

Below is my log4j.properties file content, file is placed with the src folder in eclipse.
#Application Logs
log4j.rootlogger=INFO, logg
log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=D:\\SandhyaFiles\\SeleniumWorkspace\\InterviewProject\\Logs\\Testlogs.log
log4j.appender.logg.layout=org.apache.log4j.PatternLayout
log4j.appender.logg.layout.ConversionPattern=%d -%c -%p - %m%n
log4j.appender.logg.maxFileSize=5MB
log4j.appender.logg.maxBackupIndex=3
Inside Library package i have initialised and used logj as below:
public class Library
{
public static final Logger Log = Logger.getLogger(Library.class);
public void initialized(){
Log.info("Inside initialise")
}}
calling initialize from testcase throws log4j warning:
log4j:WARN No appenders could be found for logger (library.Library).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Please Help me fix this.

It seems like log4j is not able to find the log4j properties file.
One could specify the location of the log4j.properties file explicitly via the log4j.configuration system property.
-Dlog4j.configuration=file:mylogging.properties
In case the system property log4j.configuration is not defined, then the resource is set to its default value log4j.properties and looked for in the classpath of the project (at root).

After so many trials, weirdly re arranging the properties file to this everything is going no warning and could get the logs.
# Root logger option
log4j.rootLogger=INFO, logg
log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=.\\Logs\\Testlogs.log
log4j.appender.logg.MaxFileSize=5MB
log4j.appender.logg.MaxBackupIndex=3
log4j.appender.logg.layout=org.apache.log4j.PatternLayout
log4j.appender.logg.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I wonder why this is so? if somebody can explain it will be great help.
Thanks

Related

How to add logs to java agent where log level can be changed dynamically

I need to add debug points in my javaagent. I am having my premain method in one class and AgentClassFileTransformer in another class. Since my organization use following approach which use org.apache.commons.logging package I also tried to use that method.
private static final Log log = LogFactory.getLog(InstrumentingAgent.class);
But, if I add only the log4j.properties file to the resource folder of the project, it prints the logs from the class with premain method.
But for the class which implements ClassFileTransformer it says no appenders can be found. But, again if I add the commons-logging.properties file to the same resources folder, then it'll print all the logs required in the console. (Even if I use the logger of type log4j as follows, it still says no appenders can be found for Transformer class)
Commons-logging.properties
org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger
Warning for Transformer class
log4j:WARN No appenders could be found for logger (InstrumentationClassTransformer).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
In my case I'll be adding on debug level logs in my agent. But when the agent run with a product it shouldn't print debug logs on the console. In terms of log4j I have read about setting the log level of the logger using setLevel(Log.level) method. But since I am using commons-logging's LogFactory it doesn't give any option to set the level also. So I would like to have some suggestions on how to get over this situation.
How can I get this to work? Is it something that can be achieved or do I need to change the logging mechanism?
Update 1
I have even added the package name, as mentioned by madhawa, but still it doesn't print logs of ClassFileTransformer.
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.logger.org.javaagent.instrumentation=DEBUG
It seems you haven't configure log4j.properties file correctly. Please add relevant packages and try again.

Log4j properties giving error

I m getting the error message
log4j:WARN No appenders could be found for logger (com.faktorZehn.socialNetwork.socialNetwork.PersonGenerator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
my properties looks like
This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender
# settings for the console appender
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
any ides where do i do wrong ?
your resources folder is not on your classpath. Right click on the folder -> Build Path -> Use as source folder.
Then rerun your app and your log4j.properties should be used by log4j framework.
Be sure that log4j.properties is in the classpath.
Make the resources folder actually a resource folder. It looks like a maven project so you can move it to src/main/resources
Alternatively you can set it with JVM argument.
-Dlog4j.configuration=resources/log4j.properties

Why am i receiving the errors that I am getting?

I have this code below for a Log4j logger. I have to say that a lot of people have given me help on this on a different question. But I thought that since it was getting to a new question I would open one up.
My question is why am I getting these errors? Do I have everything configured correctly? How do I add an appender. I think that I am missing something somewhere but i am not certain as to what it is.
Code:
package TestMenu;
import org.apache.log4j.Logger;
import javax.swing.*;
public class TestLogs {
private static Logger logger = Logger.getLogger(TestLogs.class.getName());
public TestLogs() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}
private void LogTester() {
logger.info("It works");
}
public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}
Error:
log4j:WARN No appenders could be found for logger (TestMenu.TestLogs).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Properties File:
# Root logger
log4j.rootLogger=INFO, file, stdout
# Write to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File="C:\Users\itpr13266\Desktop\test.log"
log4j.appender.file.MaxFileSize=50MB
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
# Write 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
Log4j doesn't seem to find your configuration file. Have you tried running your application with a parameter like:
-Dlog4j.configuration=file:config/log4j.properties
So that Log4j finds your configuration file ("config/log4j.properties" using System Property log4j.configuration):
Alternatively, you can also put this configuration file in your classpath so that Log4j finds it, like in your source directory for example:
Else, here is what Log4j documentation explains or this error message:
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?

WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)

My log4j properties file
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:\\Selenium\\eclipse-jee-juno-SR1-win32\\eclipse\\Workspace\\Core_Hybrid\\src\\com\\logs\\Application.log
log4j.appender.dest1.Append=false
I get the Error msg as stated below :
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
I have placed my properties files inside the src folder and imported the log4j.jar in the build path.
Please help on this
As I remember the log4j.properties file must be in the root-directory of the execution and added to the classPath. So this means the file must be on the same level as the src-Folder and not within this folder. Or you have to change the root-directory of your execution to your src-folder.
There's a typo in your configuration file. Remove the last ',' in the first line:
log4j.logger.devpinoyLogger=DEBUG, dest1
You should define a root logger to avoid such "No appender" warning.
log4j.rootLogger=DEBUG, dest1
May be it will help:
org.apache.log4j.PropertyConfigurator.configure(log4j.propertiesLocation);
You could add the add part of your package URL and all sub-packages will be printed to the log:
Here is an example:
log4j.logger.org.apache=DEBUG, stdout
Question :-- log4j:WARN Please initialize the log4j system properly.
ANSWER you should add below line in main method class its working
PropertyConfigurator.configure("src/main/java/com/nsettle/config/log4j.properties");

Please initialize the log4j system properly. While running web service

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)));
}

Categories

Resources