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());
Related
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
I am trying to make my Web Services Application Log using Wildfly 8.1 Final Log System Categories.
My App declares
private static final Logger LOGGER = Logger.getLogger("myapp");
It calls in parts of the code for
LOGGER.debug("something");
Using the log4j property file definition below it behaves as expected. The problem is when I access the Wildfly console (http://localhost:9990/console/App.html#logging) and update the Log Category named "myapp" from INFO to DEBUG. After tha I call my Web Service that calls the instruction LOGGER.debug("something"); inside it´s code.
The expected behavior is to show the "something" message written in my log file, but it does not happens because it behaves obeying only the log4j property file supplied below.
How to fix this?
#------------------------------------------------------------------------------
#log4j.rootLogger=DEBUG, stdout
log4j.logger.org.hibernate=INFO, stdout, myapp
log4j.logger.org.hibernate.SQL=INFO, stdout, myapp
log4j.logger.org.hibernate.type.descriptor.sql=ALL, stdout, myapp
log4j.logger.myappaudit=INFO, myappaudit
log4j.logger.myapp=INFO, stdout, myapp
log4j.logger.performance=INFO, myapp
#
# default Appender
#------------------------------------------------------------------------------
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.SSS} %c{1} [%p] %C{1}.%M - %m%n
#
# Appender for myappaudit
#------------------------------------------------------------------------------
log4j.appender.myappaudit = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappaudit.File = ${jboss.server.log.dir}/logs/myappaudit.log
log4j.appender.myappaudit.Append = true
log4j.appender.myappaudit.DatePattern = '.'yyy-MM-dd
log4j.appender.myappaudit.layout = org.apache.log4j.PatternLayout
log4j.appender.myappaudit.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#
# Appender for application
#------------------------------------------------------------------------------
log4j.appender.myapp = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myapp.File = ${jboss.server.log.dir}/logs/console.log
log4j.appender.myapp.Append = true
log4j.appender.myapp.DatePattern = '.'yyy-MM-dd
log4j.appender.myapp.layout = org.apache.log4j.PatternLayout
log4j.appender.myapp.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#Spring logs
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.security=DEBUG
log4j.logger.org.springframework.ws=DEBUG
#Spring Transaction Logs
log4j.logger.org.springframework.orm.jpa=DEBUG
log4j.logger.org.springframework.transaction=DEBUG
#Spring Async Logs
log4j.logger.org.springframework.scheduling=ERROR
You will not be able to use the logger options in the console. The reason is that if you include a log4j config an entirely isolated logging instance is created for that deployment. If you want to use jboss's tools, you must remove your logging config and define in the standalone or domain xml. Alternatively, you can create your own servlet or mbean that is part of your deployment to make runtime adjustments to the logging level or any other changes, e.g adding or removing appenders
So, to be clear, are you packaging a log4j.properties file with your application? It that being respected correctly, but you hoped to use the jboss console to amend the logging level at runtime?
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.
In my app, I need to download 3 different artifacts from a remote ftp server. After I download each artifact, I validate the artifacts. And, if there are any validation errors, I need to log them aka
validation errors with Artifact A => A.log
validation errors with Artifact B => B.log
validation errors with Artifact C => C.log
My log4j.properties looks like:
log4j.rootLogger=DEBUG, rollingfile
log4j.logger.artifactALogger=INFO, AppenderA
log4j.logger.artifactBLogger=INFO, AppenderB
log4j.logger.artifactCLogger=INFO, AppenderC
log4j.appender.AppenderA.File=A.log
log4j.appender.AppenderA.MaxFileSize=5000KB
log4j.appender.AppenderA.MaxBackupIndex=1
log4j.appender.AppenderA.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.AppenderB.File=B.log
log4j.appender.AppenderB.MaxFileSize=5000KB
log4j.appender.AppenderB.MaxBackupIndex=1
log4j.appender.AppenderB.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderB.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.AppenderC.File=C.log
log4j.appender.AppenderC.MaxFileSize=5000KB
log4j.appender.AppenderC.MaxBackupIndex=1
log4j.appender.AppenderC.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderC.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
As you can see, I have 1 appender per logger and the configuration for each appender is exactly the same (except for the name of the log file)...alot of duplication! Down the line, if I end up with more artifacts to download, I would need to declare another logger and another appender for each artifact. Something just doesn't seem right to me doing it this way. Maybe, I'm just not using log4j the way it was designed to be used? I'm currently filtering log output by class rather than by log level. Should I be using a different logging API that is more suitable?
We can create a custom logger for log4j for this purpose. We had a similar requirement once and ended up creating a custom logger.
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.