Selecting file logging level on package level - java

I have several log files logfile and debugLogFile. One - more debug info, second less, but still need to have some. In future I expect to have third file with info quantity something between of these two.
I'm asking log4j to log package MyPck in INFO level. This I need for logfile. But I need to have DEBUG level for MyPck for debugLogFile. This is a problem.
Both, logFile and debugLogFile have Threshold=ALL. I need to have possibility in each log file to write all level information. For example logfile will contain DEBUG level for MyPck and INFO for MyPck1 and debugLogFile will contain INFO level for MyPck and DEBUG for MyPck1. How to solve this problem?
log4j.rootLogger=ALL, logfile, debugLogFile
log4j.logger.MyPck=INFO
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logFile.log
log4j.appender.logfile.Threshold=ALL
log4j.appender.debugLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogFile.File=debugLogFile.log
log4j.appender.debugLogFile.Threshold=ALL

This should get you going in the right direction:
log4j.rootLogger=TRACE, defaultFile
log4j.appender.defaultFile=org.apache.log4j.RollingFileAppender
log4j.appender.defaultFile.File=defaultFile.log
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logFile.log
log4j.appender.debugLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogFile.File=debugLogFile.log
log4j.logger.MyPck=DEBUG,logFile
log4j.logger.MyPck1=INFO,logFile
log4j.logger.MyPck=INFO,debugLogFile
log4j.logger.MyPck1=DEBUG,debugLogFile
log4j.additivity.MyPck=false
log4j.additivity.MyPck1=false
The log4j.additivity.MyPck=false setting ensures that the output of MyPck does not appear in the rootLogger appender.

When you set the log level for MyPck to INFO, you actually set the threshold for this package to INFO, i.e. no message below that level will ever be emitted.
So you need to set the threshold of the package to the lowest common level you want to log.
The next step is then to configure the thresholds of the loggers to filter out any messages you don't want:
log4j.rootLogger=ALL, logfile, debugLogFile
log4j.logger.MyPck=DEBUG
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logFile.log
log4j.appender.logfile.Threshold=INFO
log4j.appender.debugLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogFile.File=debugLogFile.log
log4j.appender.debugLogFile.Threshold=DEBUG
Note that the second appender gets a copy of all messages of the first one.

Related

Tomcat logging properties filtering

I have simplified tomcat's logging properties to just this:
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tF %1$TT.%1tL [::] %4$s %3$s %5$s %n
org.springframework.aop.framework.CglibAopProxy.level = ERROR
My issue is that the last line seems to be completely ignored and I keep seeing logs like this:
2018-05-09 10:40:33.159 [::] INFO org.springframework.aop.framework.CglibAopProxy
I am absolutely sure it comes from this logger thanks to the log format I set in the logging.properties.
My issue is that the last line seems to be completely ignored...
It is ignored because ERROR fails to be parsed as valid level. Per the docs:
Valid values are integers between Integer.MIN_VALUE and Integer.MAX_VALUE, and all known level names. Known names are the levels defined by this class (e.g., FINE, FINER, FINEST), or created by this class with appropriate package access, or new levels defined or created by subclasses.
Change your logging line to one of the valid levels that is higher than INFO. Choose one of the following log lines:
org.springframework.aop.framework.CglibAopProxy.level = OFF
org.springframework.aop.framework.CglibAopProxy.level = SEVERE
org.springframework.aop.framework.CglibAopProxy.level = WARNING

Deeplearning4j Disable Logging

I have a deeplearning for java project which is producing huge amounts of logger output on STDO. I want to disable that but I cant seem to figure out how to do it.
I have a log4j.properties file in my src/main/resources folder which looks like this:
log4j.rootLogger=ERROR, Console
log4j.logger.play=WARN
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
log4j.appender.org.springframework=WARN
log4j.appender.org.nd4j=WARN
log4j.appender.org.canova=WARN
log4j.appender.org.datavec=WARN
log4j.appender.org.deeplearning4j=WARN
log4j.appender.opennlp.uima=OFF
log4j.appender.org.apache.uima=OFF
log4j.appender.org.cleartk=OFF
log4j.logger.org.springframework=WARN
log4j.logger.org.nd4j=WARN
log4j.logger.org.canova=WARN
log4j.logger.org.datavec=WARN
log4j.logger.org.deeplearning4j=WARN
log4j.logger.opennlp.uima.util=OFF
log4j.logger.org.apache.uima=OFF
log4j.logger.org.cleartk=OFF
log4j.logger.org.deeplearning4j.optimize.solvers.BaseOptimizer=OFF
slf4j.logger.org.deeplearning4j.optimize.solvers.BaseOptimizer=OFF
The specific output that is far too much is:
21:26:34.860 [main] DEBUG o.d.optimize.solvers.BaseOptimizer - Hit termination condition on iteration 0: score=1.2894165074915344E19, oldScore=1.2894191699433697E19, condition=org.deeplearning4j.optimize.terminations.EpsTermination#55f111f3
which happens multiple times a second while training.
The output of the log entry that you have provided look very much as the SLF4J output with Logback format (not LOG4J output).
Also dependencies of deeplearning4j-core advice SLF4J is used for logging.
Hence your log4j.properties have no effect on deeplearning4j logging. Try to add logback.xml configuration to the resources as well and switch to WARN or ERROR level for root logger, see https://logback.qos.ch/manual/configuration.html
There are some properties in the framework DL4J (1.0.0-beta7) that activate/deactivate the logs. I found some of them:
import org.nd4j.common.config.ND4JSystemProperties;
System.setProperty(ND4JSystemProperties.LOG_INITIALIZATION, "false");
System.setProperty(ND4JSystemProperties.ND4J_IGNORE_AVX, "true");
System.setProperty(ND4JSystemProperties.VERSION_CHECK_PROPERTY, "false");
Notice that this is an unconventional solution. On the other hand, there are some messages impossible to avoid:
MultiLayerNetwork.init()
In this method you can find a OneTimeLogger without validations:
OneTimeLogger.info(log, "Starting MultiLayerNetwork with WorkspaceModes set to [training: {}; inference: {}], cacheMode set to [{}]",
layerWiseConfigurations.getTrainingWorkspaceMode(),
layerWiseConfigurations.getInferenceWorkspaceMode(),
layerWiseConfigurations.getCacheMode());
If you find a better way to disable log messages inside DL4J please share it. There are some other ways outside the DL4J library.

How to log different loggers into different files

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

log4j - Why doesn't my appender show message?

At my work, I have inherited of the maintenance of a satandalone application.
The following code configures Log4J but no messages can be seen on the console.
LogManager.resetConfiguration();
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");
ConsoleAppender stderr = new ConsoleAppender();
stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
stderr.setLayout(layout);
stderr.addFilter(new CurrentThreadLogFilter());
stderr.setThreshold(Level.INFO);
stderr.activateOptions();
Logger loggerRECORD = getLoggerRECORD();
loggerRECORD.setAdditivity(false);
loggerRECORD.addAppender(stderr);
Logger root = Logger.getRootLogger();
root.setLevel(Level.WARN);
root.addAppender(stderr);
// some lines forward ...
loggerRECORD.info("Process starting...");
What am I missing ?
Unfortunately you have not sent the code that actually prints the message. However I can assume that you try to do something like this:
logger.info("my message");
In this case your message will not be printed because it is filtered out by root logger defined to print warnings.
Loggers in Log4J are stored as hierarchical structure. The root logger is the entry point to this tree. Each logger filters log messages according to configured level. Therefore if upper level logger (root in your case) filters log message it even does not arrive to lower level logger and definitely does not arrive to appender.
The solution for you is to define root logger to allow ALL messages.
And the last note: do you have any special reasons to configure logger programmatically? Log4J can be configured using properties or (better) xml file. Take a look on this document.
And yet another note. Log4J has been deprecated by its creator. If you are starting now go forward to Logback as a logger and SLF4J as a light weight log interface.
Thanks to AlexR, here how I solved my problem:
LogManager.resetConfiguration();
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");
ConsoleAppender stderr = new ConsoleAppender();
stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
stderr.setLayout(layout);
stderr.addFilter(new CurrentThreadLogFilter());
stderr.setThreshold(Level.INFO);
stderr.activateOptions();
Logger loggerRECORD = getLoggerRECORD();
loggerRECORD.setLevel( /* get Log Level from env. */ );
loggerRECORD.setAdditivity(false);
loggerRECORD.addAppender(stderr);
Logger root = Logger.getRootLogger();
root.setLevel(Level.WARN);
root.addAppender(stderr);
// some lines forward ...
loggerRECORD.info("Process starting...");
Since additivity is set to false for loggerRECORD, the appender sucessfully print any message from INFO to ERROR.

log4j in grails : how to log into file?

I have this log4j configuration in my grails config.groovy
log4j = {
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages' // GSP
warn 'org.mortbay.log'
appenders {
rollingFile name:'infoLog', file:'info.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:1024
rollingFile name:'warnLog', file:'warn.log', threshold: org.apache.log4j.Level.WARN, maxFileSize:1024
rollingFile name:'errorLog', file:'error.log', threshold: org.apache.log4j.Level.ERROR, maxFileSize:1024
rollingFile name:'custom', file:'custom.log', maxFileSize:1024
}
root {
info 'infoLog','warnLog','errorLog','custom', stdout
error()
additivity = true
}
}
the infoLog,warnLog and errorLog was from the previous question ... they were working well.
now I add new RollingFile wit name "custom" ...
I tried to log from my controller and service using log.info("something .... ${obj}");
but it seems that message was not inserted into custom.log, do I need to add something to the configuration ?
thank you !!
just got answer from the grails' mailing list:
i just need to add
debug "grails.app"
bellow warn "org.mortbay.log"
case closed ! :)
I have exact the same jetty/tomcat env's. Spent hours to figure it out. The trick is to define the file location (a relative path in my case) as a global variable inside Config.groovy, customized it in the environment blocks, and use the variable location inside log4j closure. Sample code is at: http://denistek.blogspot.com/2010/02/grails-environment-specific-logging-to.html
please see Log4j: How to write to a specific appender?
After all the solution is to put the additivity setting to the package configuration:
info specialLog:'activityLog', additivity:false

Categories

Resources