Eclipse4 RCP: Configure log4j with fragment plugin - log4j

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

Related

Can't turn down HtmlUnit logging

I'm using org.apache.log4j.Logger and want to turn off debug messages for htmlunit. Nothing I do seems to work. I keep getting Http.wire, Http.headers, etc debug messages. I have the root logger set to debug
I tried placing this line in my code:
org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(org.apache.log4j.Level.OFF);
I also tried placing this line in my log4j.properties file:
log4j.logger.com.gargoylesoftware.htmlunit=WARN
This is the contents of my log4j.properties file:
log4j.logger.com.gargoylesoftware.htmlunit=ERROR
log4j.logger.org.apache.commons.httpclient=ERROR
# Tell the root lodger what appenders and level to use
log4j.rootLogger=DEBUG, A1, A2
##### Console Appender #####
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
##### File Appender #####
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=/var/log/mylogfile.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.A2.Append=false
Any help would be appreciated.
Edit 11/15/16 (Adding test code)
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import org.junit.*;
import static org.junit.Assert.*;
import org.apache.commons.logging.*;
import org.apache.log4j.*;
public class Test01
{
#Test
public void homePage() throws Exception
{
LogFactory.getFactory().setAttribute("com.gargoylesoftware.htmlunit", "org.apache.commons.logging.impl.Log4JLogger");
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.WARN);
org.apache.log4j.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.WARN);
Logger.getRootLogger().setLevel(Level.DEBUG);
Logger.getRootLogger().debug("Start");
WebClient webClient = new WebClient()
HtmlPage page = webClient.getPage("https://google.com");
Logger.getRootLogger().debug("End");
}
For log4j, you should set log4j.logger.com.gargoylesoftware.htmlunit to ERROR:
log4j.logger.com.gargoylesoftware.htmlunit=ERROR
Second option : in your code, add this after declaring your web client:
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
So after much experimenting and observation of the log output I have figured this out. As it turns out the documentation for HttpClient on the Apache HTTPCLient website is not correct (as a matter of fact there are broken links). It turns out the names of the loggers used by HTTPClient are not as specified in the doc. The correct logger root name is "http" not "httpclient" which means all the trials I was performing had zero effect.
I am using org.apache.httpcomponents.httpclient_4.5.2 and org.apache.httpcomponents.httpcore_4.4.5 which as of today (11/15/16).
Here is an example log4j.properties file that will allow fine control of the HTMLClient logging
# Tell the root logger what appenders and level to use
log4j.rootLogger=DEBUG, A1, A2
# Controls detailed wire protocol
log4j.logger.org.apache.http.wire=WARN
# Controls headers (good for debugging)
log4j.logger.org.apache.http.headers=WARN
# Controls http context (what you are sending and geting)
log4j.logger.org.apache.http=WARN
# Controls htmlunit details
log4j.logger.com.gargoylesoftware.htmlunit=WARN
##### Console Appender #####
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
##### File Appender #####
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=mylogfile.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.A2.Append=false

log4j Warning : No appenders found

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.

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

logger.IsDebugEnabled() always return false

i am new to log4j and i have the following log4j.properties file in my java application
i am working on this in websphere 6.1
log4j.properties file
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.package_name=debug
However this is working if i am using only one project as part of my application.If there are multiple projects and i want to use logging facility,logger.isDebugEnabled() always return false.. can anybody suggest a solution for this?
Thanks in advance
Websphere use a classloader for default for each EAR. If you have several Web modules or EJB modules and several files for log4j, only one is loaded by the classloader.
See A Powerful, Easy-to-Use Logging System for configure log4j with several projects in a EAR.
# Set root logger level to INFO and appender to STDOUT.
log4j.rootLogger=INFO, STDOUT
#------------------------------------STDOUT-----------------------------------#
# STDOUT is set to be a ConsoleAppender.
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
# STDOUT uses PatternLayout.
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d %-5p (%c.java:%L).%M - %m%n
log4j.appender.STDOUT.Encoding=UTF-8
#-----------------------------------------------------------------------------#
# Specify the logging level for loggers from other libraries
log4j.logger.org.apache.commons.beanutils.BeanUtils=DEBUG
log4j.logger.org.apache.struts.action=DEBUG
log4j.logger.org.apache.struts.tiles=DEBUG
log4j.logger.org.apache.struts.util.ModuleUtils=DEBUG
log4j.logger.org.apache.struts.util.RequestUtils=DEBUG
log4j.logger.org.apache.struts.util.PropertyMessageResources=ERROR
log4j.logger.com.ibm._jsp=DEBUG
May you are missing the log4j.logger. prefix for each particular package.
See more of log4j in http://logging.apache.org/log4j/1.2/manual.html

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