Writing Only INFO logs to console and ERROR logs to file - java

I have a requirement to write the INFO (not error and lower levels of INFO) logs to console and ERROR logs to file. Need to use log4j for this.

You need to set a Threshold on your appender for console output. Here's an example modified from the log4j distro:
# A1 is set to be a FileAppender sending its output to
# System.out. However, only info messages and above will be printed
# in A1 because A1's threshold is set to Level.INFO.
# The fact that the root level is set to Prority.DEBUG only influences
# log requests made to the root logger. It has no influence on the
# *appenders* attached to root.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=INFO
This way, anything printed to your ConsoleAppender A1 will be ignored if it's not at least INFO level. If you set the logging level to INFO then ONLY the INFO messages will be logged.
There's an FAQ question about this on the apache log4j site.

Try this.
log.file.path=C:/log
datestamp=yyyy-MM-dd HH:mm:ss.SSS
# Define the root logger with appender file
log4j.rootLogger=INFO, stdout
log4j.logger.biz.netweb=error, LOG_FILE
#, stdout
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=%5p,[%d{${datestamp}}],[Thread:%t],[%C.%M(%L)]==>: %m%n
# LOG_FILE
log4j.appender.LOG_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOG_FILE.File=${log.file.path}/system-log/system-log.txt
log4j.appender.LOG_FILE.ImmediateFlush=true
log4j.appender.LOG_FILE.MaxFileSize=5MB
log4j.appender.LOG_FILE.MaxBackupIndex=50
log4j.appender.LOG_FILE.Append=true
log4j.appender.LOG_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG_FILE.layout.conversionPattern=%5p,[%d{${datestamp}}],[%t,%C.%M(%L)]==>, %m%n

log4j.rootLogger = debug,stdout,D,E
print 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{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
print to file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## ERROR log info!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

Following log4j configuration file will logs only "INFO" level logs in to console and between "INFO" and "FATAL" logs in to log.log file.
I you want to change the log level, you can change the MinLevel/MaxLevel parameters of LevelRangeFilter section on both console and Rolling file appender.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--Redirect log messages to console-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<!--Redirect log messages to a log file, support file rolling.-->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="log.log"/>
<param name="maxFileSize" value="2Mb"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="INFO"></level>
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>

Related

Unable to print debug statements in the file

I am using log4j in my java application to log the messages.log4j-1.2.17.jar is the jar file used. Below is the log file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="Threshold" value="debug" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="D/logs/projectLog.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<root>
<priority value="info"></priority>
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
I am using above log file in my application. Log file got generated successfuly but in the log file , debug messages are not getting printed.
In my java class,below are the statements i have included.
LOGGER.info("In TestMVCController, logger info");
System.out.println("is debug enabled" + LOGGER.isDebugEnabled());//always false even when server is started in debug mode
LOGGER.debug("In TestMVCController, logger debug");
LOGGER.error("In TestMVCController, logger errror");
In my log file, I can see only info and error messages being printed. In which scenario does the debug statements print and how can i make my log statements to print debug statements?Any advice would be helpful.
Change the root's priority value from "info" to "debug":
<root>
<priority value="debug"></priority>
<appender-ref ref="fileAppender" />
</root>
This will configure the root logger to send log messages at level "DEBUG" or higher to the appender "fileAppender". The "fileAppender" can then set the threshold it wants to log (DEBUG, INFO, WARNING etc).
Note: The appender's "threshold" parameter is optional. If you omit it, the appender will log at the level defined by the root logger. The "threshold" can be used if there are multiple appenders. For example: DEBUG for a console appender and INFO for a file appender.

can't log Hibernate queries log4j

I am trying to log hibernate queries into a webapp specific log file.
I can log any message but I am still unable to log hibernate queries.
Them are being logged inside the server.log, even in a different file, but I need an app-specific log for each app running on the same server.
I am using log4j 1.2, I am able to create the log files, to log every information passed to console but hibernate queries.
Inside a database.properties file I found the hibernate.show_sql=true that is responsible for the logging output in console.
This is the kind of console's logging output I refer:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](here is outputted the correct query passed, with the ? of the prepared statement)
Anyway, what I need is to log that output.
I tried different log4j.xml settings, but still with no success, it creates log files but does not log those outputs.
Here is the current setting, that is logging everything inside the same file, when I will be able to log even those information I will go on separating the logs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="50" />
<param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
<param name="Append" value="false"/><!--value="true" /-->
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<!-- <appender-ref ref="stdout"/> -->
<appender-ref ref="mainFileAppender2"/>
</root>
This configuration logs everything but those outputs. In the server.log are logged as follows:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](rest of the query)
Configure the logger for the following categories:
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
The first one will log the SQL-Statement (with ? for parameter values), the second one will print those parameter values if you need them.
So something like this should do the trick in your case:
<category name="org.hibernate.SQL">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="DEBUG"/>
</category>
<category name="org.hibernate.type">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="TRACE"/>
</category>
Solved this way; Anyway it also extracts the values extracted from the query
log = ${jboss.server.log.dir}/log/
#Loggers
log4j.rootLogger = INFO, FILE
log4j.logger.org.hibernate.SQL = DEBUG, HIBERNATE
log4j.logger.org.hibernate.type = TRACE, HIBERNATE
#Appenders
# Main appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${log}/main.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c{1}:%L %m %n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=true
log4j.appender.FILE.DatePattern=dd-MM-yyyy
# Hibernate queries appender
log4j.appender.HIBERNATE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HIBERNATE.File=${log}/hib-queries.log
log4j.appender.HIBERNATE.layout=org.apache.log4j.PatternLayout
log4j.appender.HIBERNATE.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.HIBERNATE.ImmediateFlush=true
log4j.appender.HIBERNATE.Threshold=debug
log4j.appender.HIBERNATE.Append=true
log4j.appender.HIBERNATE.DatePattern=dd-MM-yyyy

How to configure log4j to write DEBUG messages into a file and INFO to console

How can I configure log4j properties file to print DEBUG messages into a file and only INFO messages to console?
This is my properties file:
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
You can use log4j.properties like below:
# Root logger option
log4j.rootLogger=INFO, stdout, file
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout..Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
# Direct log messages to file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.File=/var/log/logfile.log
log4j.appender.file.Append=true
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{ISO8601}] %-5p %x%m%n
By this properties file, log4j create new logfile for each day and save previews logfile with date.
Alternative way with log4j xml configuration. Because I prefer to use xml for log4j config. Perhaps this may help you from some way.
eg.
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE_LOG" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/debug.log" />
<param name="Threshold" value="DEBUG" />
<param name="maxBackupIndex" value="5" />
<param name="maxFileSize" value="1500MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} |%5p| [%c] | %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="CONSOLE_LOG" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>
So, your Java code be like:
logger.debug("your debug log message");
logger.info("your info log message");
To configure with log4j.properties, possible duplicate at How can I create 2 separate log files with one log4j config file?
Hope this help!

log4j appender priority conflict?

I have this log4j.properties:
# Global logging configuration
log4j.rootLogger=WARN, file
### Direct log messages to a log file
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File= C:\\eclipse\\servers\\apache-tomcat-6.0.39\\logs\\log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Console messages Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Appender-to-Class definition
log4j.logger.com.XXX.payplatform.test.*=INFO, console
log4j.additivity.com.XXX.payplatform.test.*=false
So, ok, the WARN level information is being registered in the log4j.log file. That's OK.
But, I want to see INFO-level messages in my eclipse console, but i am not getting any response there.
Any solutions? I think the problem is in the "WARN, file" rootLogger configuration, so i tried this:
# Global logging configuration
log4j.rootLogger=INFO, file
But no changes resulted...
Thank you!
Read more Apache log4j 1.2 - Short introduction to log4j and find the samples as well.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
It should be like this. Add console here and set default root log level to INFO.
log4j.rootLogger=INFO, file, console
OR
Try with log4j.xml instead of log4j.properties. Read more Configuring Log4j 2
Sample log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/log4j.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<category name="com.XXX.payplatform.test">
<priority value="INFO" />
<appender-ref ref="STDOUT"/>
</category>
<root>
<priority value ="ERROR" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>

log4j debug messages not showing in console despite isDebugEnabled being true

I am using the following code within my project to log debug messages with log4j
private static final Logger LOG = Logger.getLogger(MyClass.class)
// ...
if(LOG.isDebugEnabled()) {
LOG.debug("my log message");
}
I can confirm that my log4j configuration is correct by adding a break point at the line where the debug message is written, i.e. LOG.isDebugEnabled() does return true.
Interestingly, my debug message does not show up in the console of my IDE (IntelliJ), however when changing LOG.debug() to LOG.info(), the info message is logged as expected.
Any ideas what I should be looking for in order to find out what's going wrong here?
EDIT: here's my log4j.properties file
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.Stdout.threshold=info
log4j.appender.StandaloneFile=org.apache.log4j.RollingFileAppender
log4j.appender.StandaloneFile.File=logs/standalone.log
log4j.appender.StandaloneFile.MaxFileSize=5MB
log4j.appender.StandaloneFile.MaxBackupIndex=20
log4j.appender.StandaloneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.StandaloneFile.layout.ConversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.StandaloneFile.threshold=info
log4j.rootLogger=info, Stdout, StandaloneFile
log4j.logger.com.myPacke.package1=info, Stdout, StandaloneFile
log4j.logger.com.myPacke.package2=DEBUG
log4j.appender.Stdout.threshold=info
Should be:
log4j.appender.Stdout.threshold=debug
You just set the console threshold to be info, so you're not getting debug level logs.
Be aware you also set the RollingFileAppender threshold to info as #Stephen C commented.
Make sure your configuration has below appender...We have used log4j.xml so i am adding from xml
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="info" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/dcm_migration.log" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>

Categories

Resources