Deeplearning4j Disable Logging - java

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.

Related

log4j1.2 - central appender on top of several appenders

I'm quite new at Java and log4j, and i have been asked to create a new log on top of those existing.
The situation is : batchLog takes care of BATCH, wesLog takes cares of WES. Both INFO level. That's easy. My task is to create an errorLog that will gather ERROR from BOTH Logs. And it is getting weird now. Here are the main lines of my code :
log4j.rootLogger=INFO, stdout, errorLog
log4j.logger.batchLog=INFO, batchLog
log4j.logger.wesLog=INFO, wesLog
log4j.appender.stdout.Threshold=INFO
log4j.appender.wesLog.File=/opt/apache-tomcat-8.0.18/logs/ECL_WES.log
log4j.additivity.wesLog=false
log4j.appender.errorLog.File=/opt/apache-tomcat-8.0.18/logs/ECL_ERROR.log
log4j.additivity.errorLog=false
log4j.appender.batchLog.File=/opt/apache-tomcat-8.0.18/logs/ECL_BATCH.log
log4j.additivity.batchLog=false
And I got some issues, with WES writing in wesLog (as intended) but BATCH was writing in batchLog AND wesLog (errorLog working fine).
I have been tempted by creating each first Logs its own ERROR appender and both writing in the same file, but i heard it is working badly.
Help will greatly appreciated !
Alex
PS : in the main program, they keep referring to the batchLog and wesLog as batchLogger and wesLogger (see the +ger) and it seems to be working fine, and i don't get why, as for me it refers to another, non existing, non described, object. Any idea ?

Selecting file logging level on package level

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.

read .properties file using slf4j

I want to read data from .properties file using slf4j,T i am able to output the data on console but what i want is to output the data on some file so i need file Appender for this which is declared in .properties file and i am not able to read the .properties file using slf4j.Can anyone help.
PS:I need an example that explains how to use.properties file in slf4j and how to initialize logger factory for that.
See http://slf4j.org/faq.html.
SLF4J is only a facade, meaning that it does not provide a complete
logging solution. Operations such as configuring appenders or setting
logging levels cannot be performed with SLF4J.
slf4j-simple doesn't provide extra configuration features at all.
For other implementations you should use the way to configure they provide.
For example, log4j.properties for slf4j-log4j. See http://logging.apache.org/log4j/1.2/manual.html#Configuration.
If using log4j, alternatively it can be used "Log4jLoggerAdapter", defining the configuration on a .properties file.
Code below.
The required jars:
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
If desired the source code (useful when debugging):
slf4j-api-1.7.5-sources.jar
slf4j-log4j12-1.7.5-sources.jar
The testing java class:
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.Log4jLoggerAdapter;
public class Slf4j_log4j_main {
private static Log4jLoggerAdapter log = (Log4jLoggerAdapter) LoggerFactory.getLogger(Slf4j_log4j_main.class);
public static void main(String[] args) {
PropertyConfigurator.configure(Slf4j_log4j_main.class.getClassLoader().getResource("basic/log4j.properties"));
log.debug( "a debug" );
log.info( "an info" );
log.warn("a warn");
log.error("an error");
//log.fatal("a fatal"); // slf4j misses fatal log.
log.trace("a fatal");
System.out.println("");
System.out.println("[INFO]: done");
}
}
The basic/log4j.properties
##FROM: log4j_slf4j.basic
##BASED: [BIN319P17]/[BIN319P42]
#using your own named logger.
# defining appender file
log=/home/alsdias/work/dev/java/lab/slf4j/log4j/log4j_slf4j/src/basic
# root logger setup
log4j.rootLogger = DEBUG, A1, FILE
#setting your own named logger. If more loggers, set additivity false (below)
log4j.logger.log4j.level=INFO,A1
log4j.additivity.log4j.level=false
# console appender config
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# file appender config
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
#setting the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
#setting the threshold
log4j.appender.FILE.Threshold=debug
#setting the append to false, overwrite
log4j.appender.FILE.Append=false
#set a layout for the appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d [%t] %-5p %c - %m%n
The generated output:
2013-06-14 11:47:00,473 [main] DEBUG basic.Slf4j_log4j_main - a debug
2013-06-14 11:47:00,474 [main] INFO basic.Slf4j_log4j_main - an info
2013-06-14 11:47:00,474 [main] WARN basic.Slf4j_log4j_main - a warn
2013-06-14 11:47:00,475 [main] ERROR basic.Slf4j_log4j_main - an error
[INFO]: done
slf4j is an API - if you consider it to consist only of interfaces and no classes, you are not far off.
The behavior you need is in the implementation of the interfaces, which for slf4j may be logback, AVSL, JDK14 (java.util.logging), log4j or Simple. Some can read properties, some cannot.
For logback you can use
<property file="src/main/java/chapters/configuration/variables1.properties" />
See http://logback.qos.ch/manual/configuration.html#definingProps for details.

LogBack RollingFileAppender Not Writing Log File (Though FileAppender works)

I'm trying to programmatically configure LogBack's RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender) and it doesn't seem to be working. When I'm using FileAppender, everything seems to be working fine with exact same configuration (less policies/trigger) so I'm guessing it's not a permission issue. I tried commenting out all policy configuration and that didn't help either. Below is my sample code, with some hard-coded values. Also, there's no error at all what so ever. When I debug the LogBack source code, I didn't see anything that could have gone wrong.
Any hint is appreciative. I need to get this working without a configuration file since that's a restriction in my organization. I'm testing this out on a MacBook.
Logger logger = (Logger)LoggerFactory.getLogger(applicationName);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.reset();
RollingFileAppender<ILoggingEvent> fileAppender =
new RollingFileAppender<ILoggingEvent>();
fileAppender.setAppend(true);
fileAppender.setFile("/Users/Jack/Desktop/logs/" + applicationName + ".log");
fileAppender.setContext(lc);
SizeBasedTriggeringPolicy<ILoggingEvent> rPolicy =
new SizeBasedTriggeringPolicy<ILoggingEvent>("20MB");
fileAppender.setTriggeringPolicy(rPolicy);
TimeBasedRollingPolicy<ILoggingEvent> tPolicy =
new TimeBasedRollingPolicy<ILoggingEvent>();
tPolicy.setFileNamePattern("/archive/" + applicationName + ".%d");
tPolicy.setMaxHistory(180);
tPolicy.setParent(fileAppender);
tPolicy.setContext(lc);
PatternLayout pl = new PatternLayout();
pl.setPattern("%d %5p %t [%c:%L] %m%n)");
pl.setContext(lc);
pl.start();
fileAppender.setLayout(pl);
fileAppender.start();
logger.addAppender(fileAppender);
logger.setLevel(Level.DEBUG);
logger.debug("Test message");
The key issues are as follow:
RollingFileAppender must have RollingPolicy
RollingFileAppender requires PatternLayoutEncoder instead of PatternEncoder
RollingPolicy must also be started or certain properties will be null
What made this very difficult to figure out is that I couldn't figure out how to make BasicStatusManager print out error message. I ended up having to use the following code to print everything out.
for(Status status : logger.getLoggerContext().getStatusManager().getCopyOfStatusList()){
System.out.println(status.getOrigin() + " - " + status.getMessage());
}
There is a separate thread going on as mentioned in the comment above on why LogBack log messages are not printing out. I also have an email thread going on with Nabble. Will post the solution in that thread as soon as I or someone can figure this out.

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