Log Level per appender for a single Logger - java

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.

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
****************************************************************************************

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

log4j.properties

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

Log4J attach only one class to an appender

I need to poll the JVM memory stats of my running application on a regular basis. I'm running a service that does this and writes the stats to the root Logger (I don't have much control over Root Logger usage or not).
What I want to do is route just these logging messages to a single appender. This appender should only process logging messages from this one class, and not from any other class. The other appenders shouldn't receive the messages from this one class.
So far I have the log messages only going to this 'memoryStats' appender. However, all the other logging message are also going to that appender, and I need to get rid of them, but I'm not sure how short of listing out every single class which would be a nightmare.
log4j.rootCategory=info, A1, R, MEM
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/ui.log
log4j.appender.R.MaxFileSize=100MB
# Keep backup files
log4j.appender.R.MaxBackupIndex=9
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n
log4j.category.foo.bar.services.App.MemoryStats=debug, MEM
log4j.additivity.foo.bar.services.App.MemoryStats=false
log4j.appender.MEM=org.apache.log4j.RollingFileAppender
log4j.appender.MEM.File=logs/memStats.log
log4j.appender.MEM.MaxFileSize=100MB
# Keep backup files
log4j.appender.MEM.MaxBackupIndex=9
log4j.appender.MEM.layout=org.apache.log4j.PatternLayout
log4j.appender.MEM.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n
You should remove MEM from log4j.rootCategory.
Change:
log4j.rootCategory=info, A1, R, MEM
to:
log4j.rootCategory=info, A1, R

Categories