No appenders could be found for logger(log4j)?
This Short introduction to log4j guide is a little bit old but still valid.
That guide will give you some information about how to use loggers and appenders.
Just to get you going you have two simple approaches you can take.
First one is to just add this line to your main method:
BasicConfigurator.configure();
Second approach is to add this standard log4j.properties
(taken from the above mentioned guide) file to your classpath:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)
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.
log4j:WARN No appenders could be found for logger (org.apache.camel.impl.DefaultCamelContext)
This Short introduction to log4j guide is a little bit old but still valid.
That guide will give you some information about how to use loggers and appenders.
Just to get you going you have two simple approaches you can take.
First one is to just add this line to your main method:
BasicConfigurator.configure();
The second approach is to add this standard log4j.properties
(taken from the above-mentioned guide) file to your classpath:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Java log4j No appenders could be found for logger
The line
log4j.logger.datenImportLogger=datenImportFileAppender is problematic, it miss the level definition
The logger definition should be:
log4j.logger.loggerName=[level|INHERITED|NULL], [appenderName1, appenderName2,...]
source: The Complete Log4j Manual
e.g.
log4j.logger.datenImportLogger=INFO, datenImportFileAppender
try placing log4j.debug=true in top of the log4.propeties file it should provide additional logging information (although it will have affect only after the configurations are parsed )
How-to catch log4j:WARN No appenders could be found for logger?
There is no way to do what you want correctly because log4j doesn't throw any exceptions or notifies somehow in case of misconfiguration. But it is possible.
See PoC below
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.spi.Configurator;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
public class Log4jAlternativeConfig {
private static class PrintStreamCallbackSupportDecorator extends PrintStream {
public PrintStreamCallbackSupportDecorator(OutputStream out, Callback callback) {
super(out);
this.callback = callback;
}
public interface Callback {
public void onPrintln(String x);
}
private Callback callback;
@Override
public void println(String x) {
callback.onPrintln(x);
super.println(x);
}
}
public static void main(String[] args) {
PrintStreamCallbackSupportDecorator.Callback callback = new PrintStreamCallbackSupportDecorator.Callback() {
@Override
public void onPrintln(String x) {
if (x.startsWith("log4j:WARN No appenders could be found for logger")) {
Configurator configurator = new PropertyConfigurator();
URL url = Loader.getResource("log4j_alternative.properties");
configurator.doConfigure(url, LogManager.getLoggerRepository());
}
}
};
System.setErr(new PrintStreamCallbackSupportDecorator(System.err, callback));
Logger log = LogManager.getLogger(Log4jAlternativeConfig.class);
//causes "No appenders could be found for logger" warning
log.error("test");
//should be logged as configured in log4j_alternative.properties
log.error("test 2");
}
}
The solution isn't perfect but it works.
log4j:WARN No appenders could be found for logger (running jar file, not web app)
There are many possible options for specifying your log4j configuration. One is for the file to be named exactly "log4j.properties" and be in your classpath. Another is to name it however you want and add a System property to the command line when you start Java, like this:
-Dlog4j.configuration=file:///path/to/your/log4j.properties
All of them are outlined here http://logging.apache.org/log4j/1.2/manual.html#defaultInit
Related Topics
How to Extract a Substring Using Regex
Why Is There No Multiple Inheritance in Java, But Implementing Multiple Interfaces Is Allowed
What Is the "Execute Around" Idiom
Does Java Have Support for Multiline Strings
How to Connect to Oracle Using Service Name Instead of Sid
Why Use a Prime Number in Hashcode
Executorservice, How to Wait for All Tasks to Finish
How to Resolve the "Java.Net.Bindexception: Address Already in Use: Jvm_Bind" Error
Avoid Jackson Serialization on Non Fetched Lazy Objects
Http Servlet Request Lose Params from Post Body After Read It Once
What Is the Default Initialization of an Array in Java
Static Method in a Generic Class