log4j.properties - java

I have a debug,info,warn,error logs in my code. But i want to print only info,error logs in my console. I want to store all the logs in a file. Can anyone suggest a way.
I trie with
log4j.rootLogger=debug,R1,R2
log4j.appender.R1=org.apache.log4j.ConsoleAppender
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m%n
#OAPIFacade front logs
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=app/log4j/prop/log4j.log
log4j.appender.R1.DatePattern='.'dd-MM-yy
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m%n
In this case, all the logs are printing in the console, but i want only info and error logs in the console.

As far as I know, you can't see INFO, hide WARN, and see ERROR at the same time; this violates the design of the log levels. The levels are defined in this order:
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
However, if you wanted to see only WARN and ERROR in the console, you can do this using the Threshold setting of the appender. Specifically, you just need to add:
log4j.appender.R1.Threshold=WARN

Related

Log4j StringMatchFilter and denyAll filter properties configuration

Can anyone please let me know filter properties for a package where
I want to separate the logs in two different logs files. Below is the configuration which I am trying use but its not working. I want deny logs
based string like 'UserMgmt-' and allow rest logs in log1 appender and allow only log2 appender allow 'UserMgmt-' and deny rest allow logs.
log4j.logger.com.abc=DEBUG, log1
log4j.logger.com.abc=DEBUG, log2
log4j.appender.log1=org.apache.log4j.RollingFileAppender
log4j.appender.log1.Append=true
log4j.appender.log1.File=C:/Oracle/Middleware/user_projects/domains/logs/G_loAFg/UM.log
log4j.appender.log1.layout=org.apache.log4j.PatternLayout
log4j.appender.log1.layout.ConversionPattern=%d %-6p [%c] %m%n
log4j.appender.log1.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.log1.filter.1.StringToMatch=UserMgmt-
log4j.appender.log1.filter.1.AcceptOnMatch=false
log4j.appender.log2=org.apache.log4j.RollingFileAppender
log4j.appender.log2.Append=true
log4j.appender.log2.File=C:/Oracle/Middleware/user_projects/domains/logs/PM.log
log4j.appender.log2.layout=org.apache.log4j.PatternLayout
log4j.appender.log2.layout.ConversionPattern=%d %-6p [%c] %m%n
log4j.appender.log2.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.log2.filter.1.StringToMatch=UserMgmt-
log4j.appender.log2.filter.1.AcceptOnMatch=true
Added deny all filter in 1st log1 after that it worked
log4j.appender.log1.filter=org.apache.log4j.varia.DenyAllFilter
log4j.logger.com.abc=DEBUG, log1
log4j.logger.com.abc=DEBUG, log2
log4j.appender.log1=org.apache.log4j.RollingFileAppender
log4j.appender.log1.Append=true
log4j.appender.log1.File=C:/Oracle/Middleware/user_projects/domains/logs/G_loAFg/UM.log
log4j.appender.log1.layout=org.apache.log4j.PatternLayout
log4j.appender.log1.layout.ConversionPattern=%d %-6p [%c] %m%n
log4j.appender.log1.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.log1.filter.1.StringToMatch=UserMgmt-
log4j.appender.log1.filter.1.AcceptOnMatch=false
log4j.appender.log1.filter=org.apache.log4j.varia.DenyAllFilter
log4j.appender.log2=org.apache.log4j.RollingFileAppender
log4j.appender.log2.Append=true
log4j.appender.log2.File=C:/Oracle/Middleware/user_projects/domains/logs/PM.log
log4j.appender.log2.layout=org.apache.log4j.PatternLayout
log4j.appender.log2.layout.ConversionPattern=%d %-6p [%c] %m%n
log4j.appender.log2.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.log2.filter.1.StringToMatch=UserMgmt-
log4j.appender.log2.filter.1.AcceptOnMatch=true
using org.apache.log4j_1.2.17.jar I set the filter to deny what I didn't want and then allowed everything else.
log4j.appender.NormalAppender.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.NormalAppender.filter.1.StringToMatch=User.Dashboard
log4j.appender.NormalAppender.filter.1.AcceptOnMatch=false
log4j.appender.NormalAppender.filter.1=org.apache.log4j.varia.AllowAllFilter

Enable TRACE in custom DFS application (DfLogger, log4j)

Summary
We're struggling with enabling log4j's TRACE level in a custom Documentum DFS application. The application runs on Tomcat 7.0 and uses Log4j 1.2.13, and is packaged as a WAR archive.
We have an appender called CustomServiceAppender that works just fine with all the other levels, i.e. DEBUG and upwards. During development we have this appender configured for the DEBUG level, and for production we change it to INFO or WARN. The logging then behaves like we expect (it limits the logging).
However we have written some code that logs at the TRACE level that we want to see in our logs. We've tried restarting, redeploying, deleting cache, checking log4j and commons-logging versions (they are 1.2.13 and 1.1.1, respectively). Also, there is no XML configuration file, and we know that our properties file is being picked up.
We use the default logger from DFS, namely the DfLogger class.
Changing the configuration - why doesn't it work?
In log4j.properties we change the following line from
log4j.logger.com.mycompany.services=DEBUG,CustomerServiceAppender
to
log4j.logger.com.mycompany.services=TRACE,CustomerServiceAppender
... which does not work! However, the following does work:
log4j.logger.com.mycompany.services=INFO,CustomerServiceAppender
What's happening here? Why can't we enable TRACE?
log4j.properties
For reference, here's our complete configuration file (identifying names altered):
#------------------- COMMON LOGGING LEVELS --------------------------
log4j.logger.com.emc.documentum.fs.rt = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.datamodel = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.services = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.tools = WARN, A1, F1
log4j.logger.com.documentum.debug=ERROR, A1, F1
log4j.rootLogger=ERROR, A1
#------------------- CONSOLE LOG --------------------------
log4j.logger.com.mycompany.services=ERROR,A1
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %c - %m%n
#------------------- ALL --------------------------
log4j.appender.F1=org.apache.log4j.RollingFileAppender
log4j.appender.F1.File=E\:/Documentum/logs/dfs-runtime.log
log4j.appender.F1.MaxFileSize=10MB
log4j.appender.F1.layout=org.apache.log4j.PatternLayout
log4j.appender.F1.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %c- %m%n
#------------------- ASPECT_TRACE --------------------------
log4j.logger.com.emc.documentum.fs.tracing = DEBUG, ASPECT_TRACE
log4j.appender.ASPECT_TRACE=org.apache.log4j.RollingFileAppender
log4j.appender.ASPECT_TRACE.File=E\:/Documentum/logs/dfs-runtime-trace.log
log4j.appender.ASPECT_TRACE.MaxFileSize=10MB
log4j.appender.ASPECT_TRACE.layout=org.apache.log4j.PatternLayout
log4j.appender.ASPECT_TRACE.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %m%n
#------------------- Customer TBO --------------------------
log4j.logger.no.customer=DEBUG,CustomerTBOAppender
log4j.appender.CustomerTBOAppender=org.apache.log4j.RollingFileAppender
log4j.appender.CustomerTBOAppender.File=E\:/Documentum/logs/CustomerTBO.log
log4j.appender.CustomerTBOAppender.MaxFileSize=10MB
log4j.appender.CustomerTBOAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomerTBOAppender.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] [%t] %c - %m%n
#------------------- Customer Service --------------------------
log4j.logger.com.mycompany.services=TRACE,CustomerServiceAppender
log4j.appender.CustomerServiceAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CustomerServiceAppender.File=E\:/Documentum/logs/CustomerService.log
log4j.appender.CustomerServiceAppender.Append=true
log4j.appender.CustomerServiceAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomerServiceAppender.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] [%t] %c - %m%n
****************************************************************************************

Get spring messages from sysout to file using log4j

I'm trying to get Spring messages redirected to a log file.
I am able to get only some of the messages but not all of them.
This is my log4j file
log4j.rootLogger=DEBUG, stdout, file, fileDebug
log4j.logger = ERROR
log4j.category.org.springframework = DEBUG
log4j.category.org.springframework.jdbc.core = DEBUG
org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG
# Redirect log messages to console
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} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file INFO level
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.Threshold=INFO
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
# Redirect log messages to a log file DEBUG level
log4j.appender.fileDebug=org.apache.log4j.RollingFileAppender
log4j.appender.fileDebug.Threshold=DEBUG
log4j.appender.fileDebug.MaxFileSize=5MB
log4j.appender.fileDebug.MaxBackupIndex=10
log4j.appender.fileDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.fileDebug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# log locations
log4j.appender.file.File=logInfo.log
log4j.appender.fileDebug.File=debug.log
What I get in my debug.log is for example
2015-04-14 11:18:16 DEBUG ConfigurationImpl:120 - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator 2015-04-14 11:18:16 DEBUG ConfigurationImpl:140 - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory 2015-04-14 11:18:16 DEBUG ConfigurationImpl:154 - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy15 2015-04-14 11:18:16 DEBUG ValidationXmlParser:88 - Trying to load META-INF/validation.xml for XML based Validator configuration. 2015-04-14 11:18:16 DEBUG ValidationXmlParser:95 - No META-INF/validation.xml found. Using annotation based configuration only.
But I don't see there anything from jdbcTemplate but it is in my console.
11:19:19.673 [http-bio-8080-exec-6] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
So this should be debug level for org.springframework.jdbc.core.JdbcTemplate, but that doesn't seem to do anything.
I am probably missing something obvious but I just can't see it... Thanks for advice.
You need to Change following lines:
From
org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG
To
log4j.logger.org.springframework.jdbc.core.JdbcTemplate = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.JdbcTemplate=false
log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.StatementCreatorUtils=false

How to know in log4j , that ERROR level is triggered?

How to know in log4j , that ERROR level is triggered. In my code I have written like If an exception occurs i.e if an ERROR level is triggered then I will show log file path in console and wont show any message for other levels.
In Log4j you can configure different levels in the log4j.properties or xml file which ever you choose to use.
There is some predefined pattern with the error levels and runs from lower to upper.
Lowest id Debug its in this sequence.
1.Debug
2. Warn
3. Error etc
So if you want only Error messages then configure the Error level to Error. you wot get Debug and Warn
If you configure Debug you get everything
Ex.
<logger name="org.springframework...">
<level value="error" />
</logger>
This is the point of separating the code from the configuration.
E.g., your code could look like this:
try {
someMethod();
} catch (SomeException e) {
log.error("Error!!! " + e.getMessage());
}
And in your log4j configuration, your configure your log to output only ERROR level messages:
log4j.rootLogger=error, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Just an example of a pattern layout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Log Level per appender for a single Logger

Is it possible to configure different log levels for a single Logger based on the appender?
I realize this is similar to this question, and this is as far as I had already got myself, but the problem with this is that the threshold applies to all loggers that log to that appender, whereas I only want the threshold to apply to a single logger.
i.e. So far I have something like this:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=WARN
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.logger.mylogger=DEBUG,logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern=${roll.pattern.daily}
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %C{2} - %m%n
log4j.appender.logfile.File=mylogfile.log
I want mylogger DEBUG messages to be send to the logfile appender, but I also want mylogger INFO messages to be sent to the stdout appender (but for all other loggers only WARN ings). Using the Threshold to limit stdout to WARN restricts the output of mylogger.
Aha, I fixed it by changing
log4j.appender.stdout.Threshold=WARN
to
log4j.appender.stdout.Threshold=INFO
Should have been more careful first time round.

Categories