I have below log4j configuration.
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.CA.Threshold = WARN
What is the difference between root log level DEBUG and Threshold WARN?
Threshold should always be greater than root log level?
The root level sets a global floor; the threshold applies to a specific appender. So yes, the threshold needs to be greater than the root level to be meaningful.
Take a look here for more information on the relationship of loggers, appenders, and reporting levels.
Related
Here is my log4j.properties file
# Define the root logger
log4j.rootLogger = DEBUG, toConsole
# Define the console appender
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsoleAppender.layout.ConversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
# Define the file appender
log4j.appender.success=org.apache.log4j.FileAppender
log4j.appender.success.File=logs/success.log
log4j.appender.success.Append=false
log4j.appender.success.layout=org.apache.log4j.PatternLayout
log4j.appender.success.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
# Define the file appender
log4j.appender.failure=org.apache.log4j.FileAppender
log4j.appender.failure.File=logs/failure.log
log4j.appender.failure.Append=false
log4j.appender.failure.layout=org.apache.log4j.PatternLayout
log4j.appender.failure.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
log4j.category.successLogger=DEBUG, success
log4j.additivity.successLogger=false
log4j.category.failureLogger=WARN, failure
log4j.additivity.failureLogger=false
and in java class I'm using it as:
static final Logger successLog = Logger.getLogger("successLogger");
static final Logger failureLog = Logger.getLogger("failureLogger");
and using it as:
successLog.warn("Connection is not established");
failureLog.error("Exception",e);
I'm able to get success.log and failure.log file but I was not able to print it on console. Right now, I need to print it on console as well (as just as the flow of our program i.e., need to print parallelly on console while writing to respective logs) and in future I may require only to print the statements only which are eligible to write into success.log
How can I achieve it ???? Thanks in advance
By default Log4j uses the appenders explicitly configured for a logger and the appenders of the parent logger. This behavior is called "additivity" by Log4j. You disabled it with the lines:
log4j.additivity.successLogger=false
log4j.additivity.failureLogger=false
You just need to restore additivity by deleting these lines or setting the value to true.
Alternatively, you can list multiple appenders for a given logger:
log4j.category.successLogger=DEBUG, success, toConsole
I have the following log4j configuration:
log4j.rootLogger=WARN, myappender, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=com.nasdaq.fbms.logging.custom.EscapedEnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern=%d{EEE, dd MMM yyyy HH:mm:ss,SSS z} %-5p [%t] %c{1.}#%M:%L - %m%n
log4j.appender.myappender=org.apache.log4j.RollingFileAppender
log4j.appender.myappender.File=D:/folder/myLog.log
log4j.appender.myappender.MaxFileSize=10MB
log4j.appender.myappender.MaxBackupIndex=10
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%m%n
log4j.appender.myappender.Threshold=ERROR
at this case I see only errors in myLog.log
but if I set log4j.appender.myappender.Threshold=TRACE - I see errors and warns, but don't see debug info and traces
Can I set log level for appender wider than respective logger ?
Short answer no.
You need to set the appender's threshold to TRACE and the level of the logger to TRACE too.
Here, you don't have any logger defined, so it uses the rootLogger parameter value which is WARN.
If you change it to TRACE you should see more logs.
If you want to keep your rootLogger this way you can limit the level to a specific logger like this:
log4j.category.my.custom.logger=TRACE
Here is what i have in my java code
private static final Logger SUCCESS = LogManager.getLogger("success");
private static final Logger ERROR = LogManager.getLogger("error");
And I need to create 2 log files for these logs. (i tried to follow Creating multiple log files of different content with log4j) but I think its not exactly the same. But here goes what i created in log4j.properties.
log4j.rootLogger=TRACE, SuccessAppender, ErrorAppender
# setup SuccessAppender
log4j.appender.SuccessAppender=org.apache.log4j.RollingFileAppender
log4j.appender.SuccessAppender.Threshold=INFO
log4j.appender.SuccessAppender.File=success.log
log4j.appender.SuccessAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.SuccessAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# setup ErrorAppender
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.Threshold=INFO
log4j.appender.ErrorAppender.File=error.log
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
I was wondering how to map this "success" log into the "SuccessAppender".
First of all you have to make sure that the RootLogger is not logging anything. This can be achieved in different ways but my first solution is to let it append to the NullAppender.
log4j.rootLogger=OFF, NullAppender
# setup NullAppender
log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender
Then you'll have to set the log level and the appender for your loggers (success and error):
log4j.logger.success = TRACE, SuccessAppender
log4j.logger.error = TRACE, ErrorAppender
And the rest of your log4j.properties is left as is.
An alternate solution if you don't want to use the NullAppender would be to set the additivity flag on the loggers:
log4j.rootLogger=OFF, SuccessAppender, ErrorAppender
log4j.logger.success = TRACE, SuccessAppender
log4j.additivity.success = false
log4j.logger.error = TRACE, ErrorAppender
log4j.additivity.error = false
Some more information about the additivity can be found in the Log4J manual.
Appender Additivity
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".
However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.
Loggers have their additivity flag set to true by default.
try
log4j.logger.success=TRACE, SuccessAppender
I have two java app working together.
I'm trying to configure my log4j.properties file to have two RollingFileAppender catching same packages but with different log levels.
I want it.pack.subpack1, it.pack.subpack2, it.pack.subpack3 log to two different file appenders, one catching all at DEBUG and above, one catching all at INFO and above.
I did the trick for appB but it doesn't work for appA. Why?
My properties file looks like:
log4j.rootLogger=INFO
#appA INFO file appender
log4j.appender.bocfe=org.apache.log4j.RollingFileAppender
log4j.appender.bocfe.File=${server.log.dir}/bocfe.log
log4j.appender.bocfe.Append=true
log4j.appender.bocfe.Threshold=INFO
log4j.appender.bocfe.MaxFileSize=15MB
log4j.appender.bocfe.MaxBackupIndex=5
log4j.appender.bocfe.layout=org.apache.log4j.PatternLayout
log4j.appender.bocfe.layout.ConversionPattern=%d [%c{1}] %p - %m%n
#appA DEBUG file appender
log4j.appender.bocfe-debug=org.apache.log4j.RollingFileAppender
log4j.appender.bocfe-debug.File=${server.log.dir}/bocfe-debug.log
log4j.appender.bocfe-debug.Append=true
log4j.appender.bocfe-debug.Threshold=DEBUG
log4j.appender.bocfe-debug.MaxFileSize=15MB
log4j.appender.bocfe-debug.MaxBackupIndex=5
log4j.appender.bocfe-debug.layout=org.apache.log4j.PatternLayout
log4j.appender.bocfe-debug.layout.ConversionPattern=%d [%c{1}] %p - %m%n
#appB INFO file appender
log4j.appender.bocws=org.apache.log4j.RollingFileAppender
log4j.appender.bocws.File=${server.log.dir}/bocws.log
log4j.appender.bocws.Append=true
log4j.appender.bocws.Threshold=INFO
log4j.appender.bocws.MaxFileSize=15MB
log4j.appender.bocws.MaxBackupIndex=5
log4j.appender.bocws.layout=org.apache.log4j.PatternLayout
log4j.appender.bocws.layout.ConversionPattern=%d [%c{1}] %p - %m%n
#appB DEBUG file appender
log4j.appender.bocws-debug=org.apache.log4j.RollingFileAppender
log4j.appender.bocws-debug.File=${server.log.dir}/bocws-debug.log
log4j.appender.bocws-debug.Append=true
log4j.appender.bocws-debug.Threshold=DEBUG
log4j.appender.bocws-debug.MaxFileSize=15MB
log4j.appender.bocws-debug.MaxBackupIndex=5
log4j.appender.bocws-debug.layout=org.apache.log4j.PatternLayout
log4j.appender.bocws-debug.layout.ConversionPattern=%d [%c{1}] %p - %m%n
#loggers
log4j.logger.com.eclipsesource.json=DEBUG, bocfe, bocfe-debug
log4j.logger.it.secservizi.boc=DEBUG, bocfe, bocfe-debug
log4j.logger.it.secservizi.abgmf.communication=DEBUG, bocfe, bocfe-debug
log4j.logger.it.sec.ws.boc=DEBUG, bocws, bocws-debug
#additivity
log4j.additivity.com.eclipsesource.json=false
log4j.additivity.it.secservizi.boc=false
log4j.additivity.it.sec.ws.boc=false
log4j.logger.it.secservizi.abgmf.communication=false
Could you please try adding the appender to log4j.rootLogger?
log4j.rootLogger = INFO, bocfe, bocfe-debug
How do I set the log4j configuration to log level DEBUG only for my own classes - domain, controllers etc? I dont want the information from grails classes (regarding fetching resources etc.), I just want my own debug traces..
I've tried a whole lot of combination, none seems to work i.e.
debug 'grails.app.controller.test.MainController' - doesnt work
Try this config snippet in your log4j closure (in Config.groovy):
all additivity: false, console: [
'grails.app.controllers.your.package',
'grails.app.domain.your.package',
'grails.app.services.your.package',
'grails.app.taglib.your.package',
'grails.app.conf.your.package',
'grails.app.filters.your.package'
]
You need to add you log4j info into Config.groovy. Below is a snippet from one of our projects
appenders {
//console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
console name:'stdout', layout:pattern(conversionPattern: '%d %p %t [%c] - <%m>%n')
rollingFile name:'file', file:'igive.log', maxFileSize:'2000KB', maxBackupIndex:'5',append:'true',layout:pattern(conversionPattern: '%d{[EEE, dd-MMM-yyyy # HH:mm:ss.SSS]} [%t] %-5p %c %x - %m%n')
}
root {
info 'file', 'stdout'
//debug 'file', 'stdout'
additivity = true
}
//error file: "StackTrace"
//StackTrace="error,file"
warn 'org.codehaus.groovy.grails',
'org.springframework',
'org.hibernate',
'org.apache',
'net.sf.ehcache.hibernate'
debug 'com.yourpackage'
The last line is the important line. This allows com.yourpackage (replace with your package) to be logged at a debug level.