log4j in grails : how to log into file? - java

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

Related

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.

Dropwizard custom logger does not dump logs into the file

I need to set maxfile size for my application but currently i am using Dropwizard core version 0.8.4 and its file appender does not support this feature.
So I approached like below by writing a custom appender as updating to latest dropwizard(which support my need) version not an option right now.
private void initLogging(Configuration configuration) throws JoranException {
final File logDir = new File("/tmp/enforcer");
final File logFile = new File(logDir, "wallet.log");
final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setFile(logFile.getAbsolutePath());
rollingFileAppender.setName("com.documents4j.logger.server.file");
rollingFileAppender.setContext(loggerContext);
FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() +"%i.gz");
fixedWindowRollingPolicy.setMaxIndex(7);
fixedWindowRollingPolicy.setContext(loggerContext);
fixedWindowRollingPolicy.setParent(rollingFileAppender);
fixedWindowRollingPolicy.start();
SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
sizeBasedTriggeringPolicy.setMaxFileSize("2KB");
sizeBasedTriggeringPolicy.setContext(loggerContext);
sizeBasedTriggeringPolicy.start();
rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
rollingFileAppender.start();
System.out.println("Logging: The log is written to " + logFile);
final ch.qos.logback.classic.Logger log = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
log.setLevel(Level.DEBUG);
log.addAppender(rollingFileAppender);
}
#Override
public void run(Configuration configuration, Environment environment) throws Exception
{
initLogging(configuration);
}
My yaml file config is
logging:
level: INFO
org.springframework.retry.support.RetryTemplate: DEBUG
appenders:
- type: file
currentLogFilename: /tmp/enforcer.log
threshold: ALL
archive: true
archivedLogFilenamePattern: /tmp/enforcer-%d.log
archivedFileCount: 5
timeZone: UTC
logFormat: '%-5level [%date{yyyy-MM-dd HH:mm:ss,SSS}] [%X{realdocRequestId}] %logger{15}: %m%n'
Now when i run my application i noticed, even though custom log file (/tmp/enforcer/wallet.log) is created in the particular directory but actual log is not dumped i.e. wallet.log file size is 0 kb where as the log file configured in yaml is created and size is certain kb and goes on increasing as log event is generated.
I am not able figure out what is wrong im doing, help will be appreciated.
your logger doesn't log anything because you never set an encoder to it. Set a breakpoint in:
OutputStreamAppender#start() and you will see that there is no encoder.
It will work once you add:
LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault());
formatter.setPattern("[%level] [%h] %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} [%thread] [%logger] %msg%n");
layoutEncoder.setLayout(formatter);
formatter.start();
rollingFileAppender.setEncoder(layoutEncoder);
Of course you can define whichever format you like (and formatter).
But keep in mind that this is a fairly hacky example of what you try to achieve. You now have 2 points in code where you configure logging (DW and your own). You have yaml configured logging as well as your own. I recommend investing a bit of work and adding a proper AppenderFactory that you can use.
Hope that helps,
Artur

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.

Log4J change File path dynamically

I want to change the path and file name of my log4j logfile dynamically.
I have read a lot of pages and nearly every tell me that I should use system properties like here:
how to change the log4j log file dynamically?
So my log4j.properties file looks like this:
log4j.logger.JDBC_LOGGER=INFO,jdbcTests
log4j.additivity.JDBC_LOGGER = false
log4j.appender.jdbcTests=org.apache.log4j.FileAppender
log4j.appender.jdbcTests.File=${my.log}
log4j.appender.jdbcTests.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbcTests.append = false
log4j.appender.jdbcTests.layout.ConversionPattern=%d{yyyy mm dd HH:mm:ss} %5p %C:Line %L - %m%n
In my main method I am going to set my new system property:
System.setProperty("{my.log", "C:/logfile.log");
But I just get an error:
log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException:
at java.io.FileOutputStream.open(Native Method)....
And when I try to read my set system property with:
System.out.println(System.getProperty("my.log"));
it return null.
What do I do wrong?
I think you meant "my.log" not "{my.log"
System.setProperty("my.log", "C:/logfile.log");
I wouldn't imagine you can change this once the logging has started so you need to set this as early in your program as possible.
BTW: You can sub-class FileAppender to make it behave any way you like.
You have a misspelling: "{my.log" instead of "my.log"
Just set property before instantiating logger, once you initilize the logger setting the property will not worth. just like below:
System.setProperty("my.log", "C:\\src\\com\\web\\automation\\logs\\Application.log");
PP_LOGS = Logger.getLogger("devpinoyLogger");

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.

Categories

Resources