log4j Warning : No appenders found - log4j

I have the following log4.properties :
log4j.rootLogger=WARN,console
log4j.rootCategory=debug,A1,D
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p %d{MM/dd/yyyy HH:mm:ss} %x %c - %m%n
log4j.appender=org.apache.log4j.RollingFileAppender
log4j.appender.D.File=c:/opt/logs/MyLogs.log
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%p %d{MM/dd/yyyy HH:mm:ss} %x %c - %m%n
I am linking it to my project using Java option "-Dlog4j.configuration=c:\opt\config\log4j.properties"
In my Java code I import log4j.Logger and then instantiate it :
public static final Logger logger = Logger.getLogger("testServlet.class");
However I am still getting log4j warnings and the log file is not getting generated :
log4j:WARN No appenders could be found for logger (testServlet.class).
log4j:WARN Please initialize the log4j system properly.
What am I doing wrong?

The file name should be log4j.properties. And the file should be located one of the folders in the classpath.
In Eclipse, I have also tried many options but the best solution in Eclipse is to create another 'source folder' in your project and put the log4j.properties file in the directory. As all the source folders are in the classpath, the Log4j system will locate your configuration file.

Related

Log4j configuration for multiple loggers

I have a single log4j.properties file in the server and to applications deployed in the server.The requirement is to create separate loggers for the application
I defined this in my application
# Root logger option
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/opt/ibm/WebSphere/AppServer/profiles/MDMServer/logs/damcoLoging.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
#logging for jbpm
log4j.logger.jbpmLogger=INFO, jbpmLogger
log4j.appender.jbpmLogger=org.apache.log4j.RollingFileAppender
log4j.appender.jbpmLogger.maxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.jbpmLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.jbpmLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.jbpmLogger.File=/opt/ibm/WebSphere/AppServer/profiles/MDMServer/logs/jbpmLogging.log
log4j.additivity.jbpmLogger=false
In my java class I have done this for the secondary logger
Logger logger=Logger.getLogger("jbpmLogger");
Now the logs are getting generated properly.But for the secondary logger I want to set the class name as well.So that I can know from which class the log is generating.
Currently the log for the secondary logger looks like this
INFO jbpmLogger:8 - Hi
Is it possible to set the class name as well?
You can add the class name to the output Pattern with %C, although the docs warn that this is slow. Is this what you want?
Or since onegetLogger() method takes a String, you could concatenate the class and your "jbpmLogger" if you wanted to. And since the naming is hierarchical, still just using your single logger in the configuration file. e.g.
Logger logger = getLogger("jbpmLogger." + this.getClass().getName());

Eclipse4 RCP: Configure log4j with fragment plugin

So this is a follow up issue that arrised with this question: Using log4j in eclipse RCP doesn't work.
So far I'm able to use the log4j API inside my RCP but when running it with the following command
Category CAT = Category.getInstance(getClass().getSimpleName());
CAT.debug("Application has been started");
I get that exception:
No appenders could be found for category (MyPlugin).
Please initialize the log4j system properly.
I have created a fragment plugin containing a file called log4j.properties and the fragment host is the plugin containing the log4j API. The property file lives in the "root" of the fragment plugin
My log4j.properties file looks like that:
# Set root logger level to debug and its only appender to default.
log4j.rootLogger=debug, default
# default is set to be a ConsoleAppender.
log4j.appender.default=org.apache.log4j.ConsoleAppender
# default uses PatternLayout.
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Any ideas what I'm doing wrong?
I am not sure, what is the purpose of using fragment for holding configuration. Try to place your log4j,properties (note that the name of the file is misspelled in your answer) directly to your plugin root folder. In the Activator.start() execute following code:
public void start(BundleContext context) throws Exception {
super.start(context);
URL installURL = plugin.getBundle().getEntry("/");
String installPath = Platform.asLocalURL(installURL).getFile();
PropertyConfigurator.configure(installPath +"/log4j.properties");
}
First of all, category documentation says: This class has been deprecated and replaced by the Logger subclass.
Secondly, Category has to be mapped to an appender:
log4j.category.MyPlugin=info,MyPlugin
log4j.appender.MyPlugin=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MyPlugin.layout=org.apache.log4j.PatternLayout
log4j.appender.MyPlugin.layout.ConversionPattern={%t} %d - [%p] %c: %m %n
log4j.appender.MyPlugin.file=C:/logs/MyPlugin.log
log4j.appender.MyPlugin.DatePattern='.' yyyy-MM-dd-HH-mm

log4j appender with a file url

I have a problem where log4j is not logging to one of my files and i'm not sure why. I have some code that runs scripts, the scripts can add logging which is logged to a file using log4j, I am trying to create an appender that only logs for a particular script.
log4j.logger.com.my.class=INFO, JS_LOG
log4j.appender.JS_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.JS_LOG.Encoding=UTF-8
log4j.appender.JS_LOG.File=${log.outputdir}/js_service.log
log4j.appender.JS_LOG.MaxFileSize=2MB
log4j.appender.JS_LOG.MaxBackupIndex=10
log4j.appender.JS_LOG.Append=true
log4j.appender.JS_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.JS_LOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
the above appender works and i get this in my log file
2012-04-18 11:25:52,043 [<MD> Inc Msg Dispatch-1 New] INFO com.my.class.file:/myfile - info
when the script logs something it logs to a logger of the form
com.my.class.file:/myfile this appears in the log using the above config in my log4j.properties file.
if I change the above to:
log4j.logger.com.my.class.file:/myfile=INFO, JS_LOG
log4j.appender.JS_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.JS_LOG.Encoding=UTF-8
log4j.appender.JS_LOG.File=${log.outputdir}/js_service.log
log4j.appender.JS_LOG.MaxFileSize=2MB
log4j.appender.JS_LOG.MaxBackupIndex=10
log4j.appender.JS_LOG.Append=true
log4j.appender.JS_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.JS_LOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
nothing is logged to the file. I would have thought this should work? I retrieve the file
I managed to solve this problem, its something to do with the file: in my log4j.properties I changed the appender to file_ then in my code I do some manipulation to the file url to make it match the appender.

Creating an application specific log under Tomcat 7?

I'm trying to capture logging messages, stdout, and stderr for my webapp, which is running under Tomcat 7.
I have a log4j.properties file in my war file, at WEB-IN/classes/log4j.properties, pretty much copied from the log4j docs, except with a filename "myapp.log":
# 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
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
WEB-INF/lib includes log4j-1.2.14.jar and commons-logging-1.0.4.
I'm expecting to see myapp.log appear in Tomcat's log directory, but it's not. Please explain to me what I'm doing wrong.
You're using a ConsoleAppender where you should be using a file appender:
log4j.appender.A1=org.apache.log4j.RollingFileAppender

Storing log into .log file using SLF4j/log4j

I am using SLF4J and as per requirement i have to store the logs into the .log file. But when i run the program the log are not written into thelog file.
Class :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestSLF4J {
// private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);
private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);
public static void main(String[] args) {
logger .debug("Sample debug message");
logger .info("Sample info message");
logger .warn("Sample warn message");
logger .error("Sample error message");
}
}
log4j.properties
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=C:/checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=DEBUG,file
i can see info,warn,error on console but not debug value..!!
Can anyone help me store log into the checkLog.log file.??
I just tried the example you gave and it worked fine for me. There are several things that I'd check/try:
Check if you can write to the root of C: - write this instead:
log4j.appender.file.File=checkLog.log
to log to the current folder
Add a console logger to see whether it works in console:
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=DEBUG,console,file
When you run the application, you should see logging in the console and in the file.
Check that all sl4j libraries are in the path - you will need slf4j-api and slf4j-log4j12 jars on your classpath
Ensure that log4j.properties is on the classpath
Hope this helps.
Make sure that you have binding with only one logging framework. If you have more than one logging framework jar then you might NOT see output.
I had similar problem then found that, class path had slf4j-simple-1.7.5.jar as well as log4j.jar. So log output was getting written only on console. Removing first one from class path helped.
Also check console, you should be getting a warning/error message.

Resources