Spring 3 standalone application does not write output to file - java

I have a Spring 3 standalone application and I'm using log4j for logging. Log4j settings are the ones in the xml that is pasted below. I get log output writen to console but nothing is writen to log file.
Log4j is initialized in class Main by statement:
DOMConfigurator.configure("abanol-loader-log4j.xml");
I've already tried by changing loggers level values, by changing value of attribute additivity in logger file and even by removing it. I get output writen to console but nothing is writen to log file (but it is created).
I've validated XML file successfully, I've googled for a solution but I get nothing. Any idea?
Thanks in advance.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="debug" debug="false">
<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 - %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="abanol-loader.log"/>
<param name="MaxFileSize" value="25MB"/>
<param name="MaxBackupIndex" value="20"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p-%d{ISO8601}-[%t]-%C.%M:%L(%c{1}) - %m%n"/>
</layout>
</appender>
<logger name="es.pack1.mypackage" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</logger>
<logger name="org.springframework" additivity="true" >
<level value="WARN"/>
<appender-ref ref="file"/>
</logger>
<logger name="net.sf.jasperreports" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="file"/>
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>

You have configure both "The Console Appender" and "RollingFile Appender" in your log4j.xml
So, Log4j defaultly selects the first. i.e, Console Appender as the logging.
So, it was writting the log report in Console(System.out.println)
Please remove the Console Appender, then it will automatically save the log's in the File...........

I've replaced log4j XML configuration by a properties file finally. I think it's same than XML that I was using but it works while XML doesn't. I really don't know why.
log4j.rootCategory=DEBUG, S, file
log4j.logger.es.bod.freenergy.abanol=DEBUG, file
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern =%5p-%d{ISO8601}-[%t]-%C.%M:%L(%c{1}) - %m%n
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File =abanol-loader.log
log4j.appender.file.MaxFileSize = 10Mb
log4j.appender.file.MaxBackupIndex=25
log4j.appender.file.Append = false
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%5p-%d{ISO8601}-[%t]-%C.%M:%L(%c{1}) - %m%n
Thanks to who has answered my question anyway.

Related

log4j fatal level logging required

This is my log4j.xml. i want only FATAL log from package com.xyz.rest should print to /LOGS/Acb/d.log file.
But i can see Debug level logs as well in file. Why log4j is printing Debug level logs from com.xyz.rest package.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="Def" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/LOGS/Acb/d.log"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="com.xyz.rest">
<logger value="fatal"/>
<appender-ref ref="Def"/>
</logger>
</log4j:configuration>
Its because with the following code you are adding another log level to your com.xyz.rest package which is not removing the debug level which your package inherits from parents:
<logger name="com.xyz.rest">
<logger value="fatal"/>
<appender-ref ref="Def"/>
</logger>
What you need is to set the additivity to false so that rather than adding another log level, you will set the log level:
<logger name="com.xyz.rest" additivity="false">
<logger value="fatal"/>
<appender-ref ref="Def"/>
</logger>
More information can be found here.
Not sure but it might also be necessary to enclose the <logger></logger> tags in <loggers></loggers> tag

Log4j debug to file - Info to console

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>

log4j log one class [duplicate]

I want to set up log4j so that all log meessages produced from classes under package com.foo.bar go to bar.log, and all the log meessages produced from classes under package com.bar.blatz go to blatz.log.
Questions
How do I do this using log4j.xml?
I know its possible using property files, but how do I do it using the XML configuration?
This is based on my answer to a similar question:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="bar.log" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- additional fooSystem logging -->
<appender name="BlatzLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="blatz.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<logger name="com.foo.bar">
<appender-ref ref="BarLogFile"/>
</logger>
<logger name="com.bar.blatz">
<appender-ref ref="BlatzLogFile"/>
</logger>
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
</log4j:configuration>
If you add an appender-ref to the root element, it will also receive com.foo.bar etc messages. You can stop that by specifying 'additivity="false"' on the loggers.
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
We can add appenders here. It will work if the application is using root logger. for example quartz Scheduler API.

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>

iBatis, spring, how to log the sql that is executed?

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

Categories