Log4j debug to file - Info to console - java

I'm using log4j for logs.
I have a class that i want to print to the console only log.info level,
And print to some file log.info + log.debug levels (The same class).
I tried to setup the log4j.xml as follows :
<!--appender to parser file-->
<appender name="parserFile" class="org.apache.log4j.FileAppender">
<param name="File" value="/var/log/java/parser/parser.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{E MMM dd HH:mm:ss} %c : %m%n" />
</layout>
</appender>
<!--appender to the stdout-->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<logger name="parser.ParserMainJava">
<level value="debug" />
<appender-ref ref="parserFile" />
</logger>
<logger name="parser.ParserMainJava">
<level value="info" />
<appender-ref ref="console" />
</logger>
This is my configuration but I can't use 2 loggers with the same package name.
Any suggestions ?
Thanks,
Or.

Just define one logger for "parser.ParserMainJava" with debug level, and add the following parameter to your console appender definition:
<param name="Threshold" value="INFO"/>

In my case I needed to adjust my AppenderRef. Adding the param element to the ConsoleAppender did not work. This element inside Loggers is good.
<Root level="debug">
<AppenderRef ref="FileAppender" />
<AppenderRef ref="ConsoleAppender" level="info"/>
</Root>

Related

log4j level trace and debug didn't show any log

In log4j there are level from trace at the lowest, debug, info, warn, error, and fatal.
I used level info to log my web application with this code
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<logger name="com.myapp">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
<logger name="com.myapp.controller">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
</log4j:configuration>
It'd produce abc.log contained log from level info to fatal.
Then I tried to move the error log only to another file, error.log.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<logger name="com.myapp">
<level value="error" />
<appender-ref ref="errorlog"/>
</logger>
<logger name="com.myapp.controller">
<level value="error" />
<appender-ref ref="errorlog"/>
</logger>
<logger name="com.myapp">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
<logger name="com.myapp.controller">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
</log4j:configuration>
But didn't work, all logger still in abc.log. I found that the logger outputs all those messages equal to that level and also all greater levels than it, that's why the log still in abc.log.
When I deleted abclog and log level info, it worked. It'd show logger from level error (and maybe fatal error if any) on error.log.
But why when I tried to lower the log info to trace / debug without level info or error, it didn't come up with anything, no logger created.
When I put the lowest log4j level, it should show all the log from its level and above like it supposed to do at info level (show log from info to fatal).
Why could it show up with nothing when the level change into trace / debug instead of level info?
Does it really no other way to separate error log to another file from other log?
Try to use the category with additivity set to false. I have not tested, but something like the following should work:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="ERROR"/>
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<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="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<!-- all error from com.myapp to errorlog only -->
<category name="com.myapp" additivity="false">
<priority value="error" />
<appender-ref ref="errorlog"/>
</category>
<!-- all other log from com.myapp to abclog only -->
<category name="com.myapp" additivity="false">
<priority value="info" />
<appender-ref ref="abclog"/>
</category>
<root>
<!-- all other log to console -->
<appender-ref ref="console"/>
</root>
</log4j:configuration>
If you want to separate com.myapp and com.myapp.controller, you can declare more categories. But you should declare com.myapp.controller before com.myapp.
UPDATE : you can try with filter perhaps is what you need, after that, I have no more idea:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="ERROR"/>
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO" />
<param name="levelMax" value="WARN" />
</filter>
</appender>
<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="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<!-- all log from com.myapp to special log file -->
<category name="com.myapp" additivity="false">
<appender-ref ref="errorlog"/>
<appender-ref ref="abclog"/>
</category>
<root>
<appender-ref ref="console"/>
</root>
</log4j:configuration>

log4j - show logs both in file and console

I have two appenders, one for file and another for console.
I want Eclipse console to show both file and console logs, but in the same time file to show only its own logs.
How do I do that?
Here is conf.
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<param name="File" value="${jboss.server.log.dir}/file.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %m%n"/>
</layout>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
</layout>
Also, file log is showing only logs from specific package.
<category name="com.project.example" additivity="false">
<appender-ref ref="FILE"/>
<priority value="INFO"/>
</category>
And root:
<root>
<priority value="${jboss.server.log.threshold}"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
you have to configure your appender in logger tag as below.
<logger name="com.project.example">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="CONSOLE" />
</root>

Log4j: specific logging

I have a service class that import and parse files from an ftp server.
This task can be executed from a Spring controller when the user choose a specific action.
The same service is used by a scheduled task (TimerTask) that everyday at a specific time do the same.
I would separate logging for user / scheduled execution into different files:
logs/scheduledImport.log
logs/userImport.log
Is it possible with Log4j?
Of course. First you must define 2 different file appenders.
<appender name="default.file" class="org.apache.log4j.FileAppender">
<param name="file" value="/log/userImport.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="another.file" class="org.apache.log4j.FileAppender">
<param name="file" value="/log/scheduledImport.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
Then you define your loggers:
<logger name="com.foo.UserService" additivity="false">
<level value="debug" />
<appender-ref ref="default.file" />
</logger>
<logger name="com.foo.ScheduledService" additivity="false">
<level value="debug" />
<appender-ref ref="another.file" />
</logger>
You can use different loggger instances. In this case you can apply different settings (output file) for each logger.

Log4j: Log File correct, but Stdout only showing a subset

I have a problem with the configuration of my logging with log4j. All the log messages are shown as expected in the root.log file, but the stdout, however, does only show a subset of the root.log although I configured it to be the same.
EDIT: Precisely I am missing all messages from a.b.
EDIT 2: I am not absolutely sure, but I think that some messages from a.b. (INFO and DEBUG) get loggedd on stdout, others don't. Might that be a problem related to threads? E.g. some threads doing logging, some not?
The following is my log4j.xml, is there eventually a mistake?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] (%C:%L) %-5p - %m%n" />
</layout>
</appender>
<appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.home}/logs/root.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
</layout>
</appender>
<logger name="a.b.master">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE_ALL" />
</logger>
<logger name="a.b.master.orm.support.HibernateSessionFilter">
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE_ALL" />
</logger>
<logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
<level value="info" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE_ALL" />
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_ALL"/>
</root>
</log4j:configuration>
I am using Commons Logging as implementation.
Thanks for the help!
Your configuration is very similar to mine where my logs are dumped into the console and to some remote host. Try this to see if it works for you:-
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
...
</appender>
<appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
...
</appender>
<logger name="a.b.master">
<level value="debug" />
</logger>
<logger name="a.b.master.orm.support.HibernateSessionFilter">
<level value="info" />
</logger>
<logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
<level value="info" />
</logger>
<root>
<priority value="info"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_ALL"/>
</root>
</log4j:configuration>
I don't think you need to define appender-ref in every a.b logger since it should pick up the appender from root. Also, I use priority instead of logger for root. Otherwise, your configuration looks identical to mine and I'm able to get mine working properly. If this doesn't work for you for some reason, I would suggest you to remove your custom a.b loggers from the configuration to see if you are able to get the a.b messages in your console.
Also, just FYI, I'm sure you already know this, but if your root is set to use "info", then there's really no need to set your custom a.b logger to "info" too. I usually leave my root to "warn" and have my custom loggers to override that with either "debug" or "info".
I found the solution even if not the problem directly. I added a parameter to my ConsoleAppender that everything is printed to the stderr. This now works. I guess its a problem of eclipse or log4j because if i am starting the app from the terminal (console) I can see all log messages properly.
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.err" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
</layout>
</appender>

Log4j not printing 3rd party logs(Spring.Hibernate etc) to RollingFileAppender

I have been on log4j for sometime and read quite a few articles and all look pretty straight forward but when I implement it, it doesnt seem to work.
I have the below requirement :
1-> Need to have 2 files created for 2 different packages :
So I created as per log4j specs and it creates 2 files based on 2 different packages but only the logs logged from our application gets logged into those files.
The logs generated from Spring or Hibernate and Ibatis do not get logged to the file at all, but they get and all other logs do get logged to the console.
I am placing the log4j.xml that I am using, any help would be appreciated :
Also I have tried lots of combinations like from root appender removed the references already mentioned in the logger, adding them back to the root, add addivity to loggers but nothing worked:
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} DSL Service(CONSOLE) %-5p %c [%t]: %m%n%n"/>
</layout>
</appender>
<appender name="FEPOCServiceLog"
class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="true"/>
<param name="maxFileSize" value="100KB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="C:\\tmp\\DSLService_1.0.log" />
<param name="threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ISO8601} DService(CONSOLE) %-5p %c [%t]: %m%n%n" />
</layout>
</appender>
<appender name="iServiceLog"
class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="C:\\tmp\\iService_1.0.log" />
<param name="threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ISO8601} iService(CONSOLE) %-5p %c [%t]: %m%n%n" />
</layout>
</appender>
Hope this helps :
<logger name="com.fepoc.services.dataservice">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
<logger name="com.fepoc.mpbit">
<level value="DEBUG"/>
<appender-ref ref="iServiceLog"/>
</logger>
<logger name="net.sf.hibernate">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
<logger name="org.hibernate">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
<logger name="com.mchange.v2">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
<logger name="com.ibatis.sqlmap">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
<logger name="org.springframework">
<level value="DEBUG"/>
<appender-ref ref="FEPOCServiceLog"/>
</logger>
...
<root>
<priority value ="debug" />
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="FEPOCServiceLog"/>
<appender-ref ref="iServiceLog"/>
Thanks,
NK
The problem is that Spring and Hibernate don't use log4j.
Hibernate internally uses slf4j, so you need to add slf4j-log4j binding to your classpath in order to redirect its output to log4j, see SLF4J user manual.
Spring uses Commons Logging, you can redirect its output to log4j via slf4j by adding jcl-slf4j bridge and slf4j-log4j binding, see Logging Dependencies in Spring.

Categories