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.
Related
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>
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.
Looked at quite a few examples on this and have seen some confusing stuff. Here is the log4j.xml file
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<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>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1000KB" />
<param name="maxBackupIndex" value="5" />
<!-- For Tomcat -->
<param name="file" value="${catalina.home}/logs/demandManage.log" />
<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>
<logger name="com.xxx.demand.domain.data" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
</logger>
<logger name="com.xxx.rita.serviceserver.controller.AbstractRestController" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
<appender-ref ref="console" />
</logger>
<logger name="com.xxx.dao.AbstractDAOImpl" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
<appender-ref ref="console" />
</logger>
<logger name="org.hibernate" >
<level value="warn" />
<appender-ref ref="file"/>
</logger>
<root>
<priority value ="warn" />
<appender-ref ref="console" />
<appender-ref ref="file"/>
</root>
</log4j:configuration>
In my java class:
package com.xxx.dao;
public abstract class AbstractDAOImpl<T, ID extends Serializable> implements AbstractDAO<T,ID>{
private Logger logger = LoggerFactory.getLogger(com.xxx.dao.AbstractDAOImpl.class);
#SuppressWarnings("unchecked")
public T read(ID id){
T response = (T) getCurrentSession().get(clazz, id);
if(response==null){
logger.error(RiskTaker.name.get()+ " tried to load " + clazz + " of " +id);
throw new RuntimeException("hibernate object did not exist");
}
return response;
}
}
So when I am hitting the logger.error message with this configuration, nothing is going to the demandManage.log file or the console. I don't see what's wrong. When I had root set to debug, hibernate of course practically fills up that log file so it does work.
Is there a difference between using level vs priority? Like a slfj4 vs appache logger? The root sets priority but the child loggers are using level.
I am starting tomcat in debug mode and know I am hitting those logger.error statements but the file remains empty. May be I need to flush to it right away?
I previously had the logger in the xml file as fully qualified and in the java class as just class name only but fixing that did nothing. I am wondering what I am missing. Thank you in advance.
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>
I am using iBatis with spring framework. I want to log the sql that iBatis executes when I say something like
Employee e = (Employee) getSqlMapClientTemplate().queryForObject("emp_sql", emp);
The above line will look for "emp_sql" id in the ibatis sql file that I have. And then run the query corresponding to "emp_sql". I want to log this query.
I have the following log4j xml properties file.
<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/disk1/logs/sql.log"/>
<param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
</filter>
</appender>
<logger name="log4j.logger.com.ibatis">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
<logger name="log4j.logger.java.sql.Connection">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
<logger name="log4j.logger.java.sql.PreparedStatement">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
I still cannot get the sql that the ibatis executed.
Is there something wrong with the configuration?
Should I just say
<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/disk1/logs/sql.log"/>
<param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
</filter>
</appender>
<logger name="log4j.logger.java.sql">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
Do I have to use p6spy or something else? Or is there something that I can do in the log4j configuration to get the iBatis sql logs?
Add the following to your log4j configuration (uncomment what you want to see).
# SqlMap logging configuration.
#log4j.logger.com.ibatis=DEBUG
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
#log4j.logger.java.sql=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
#log4j.logger.java.sql.Statement=DEBUG
#log4j.logger.java.sql.PreparedStatement=DEBUG
#log4j.logger.java.sql.ResultSet=DEBUG
Add this in your log4j.xml
<logger name="com.ibatis" additivity="false">
<level value="debug"/>
<appender-ref ref="APPENDER"/>
</logger>
If you are using Log4j as your logging framework you need to set mybatis to use log4j as its default logging tool. You can do this by setting it in the mybatis-config.xml like this,
<setting name="logImpl" value="LOG4J"/>
Or if you are not using mybatis-config.xml and just annotations, then you want to use
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
before invoking any other mybatis methods to set the default logging implementation. Read More...
Use this configuration in your log4j.properties,
# Global logging configuration
log4j.rootLogger=INFO, stdout
# MyBatis mapper interfaces logging configuration...
log4j.logger.com.sample.mappers=DEBUG
# SqlMap logging configuration.
log4j.logger.org.mybatis.spring=DEBUG
log4j.logger.org.apache.ibatis=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%p] %c - %m%n
If you are using log4j.xml configuration try this equivalent of the above,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "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 [%p] %c{1} - %m%n"/>
</layout>
</appender>
<logger name="org.mybatis.spring" additivity="false">
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</logger>
<logger name="com.sample.mappers">
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</logger>
<!-- Other custom 3rd party logger configs -->
<root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
Either use properties file or xml file to configure log4j as above and place it in your classpath for this to work correctly.
Add this in your log4j
<logger name="java.sql" additivity="false">
<level value="debug" />
<appender-ref ref="console" /> </logger>
This will print out the sql as well as the output results