I was making a log4j.xml for one of my projects in netbeans .
Here is its contents :
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MyLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="INFO" />
<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>
<appender name="sqlLogs" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MySqlLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="TRACE" />
<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>
<level value ="INFO" />
<appender-ref ref="FILE" />
</root>
<logger name="org.hibernate.SQL" additivity="false" >
<level value="DEBUG" />
<appender-ref ref="sqlLogs" />
</logger>
</log4j:configuration>
Now i want sql logs to go only to MySqlLogs.log but they are appearing on both MySqlLogs.log and catalina.out .
The format of logs in catalina.out is
Hibernate : select ....
Hibernate : insert into ....
Can anyone help me out on this please .
You can stop it, on your env shared properties.
hibernate.show_sql=true
you have to add the following line in your hibernate_cfg.xml file
show_sql to false
and in your file loging.properties :
log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.type=trace
log4j.logger.org.apache=debug
Related
Okay, this is driving me nuts. I have an app that uses log4j 1.2.16, my configuration file is properly picked up and log4j writes to the file that I want, BUT it only logs error level.
Code:
import org.apache.log4j.Logger;
- (... code omitted)
private static final Logger LOGGER = Logger.getLogger(PostLoginController.class);
LOGGER.debug("TOTO debug");
LOGGER.info("TOTO Info");
LOGGER.warn("TOTO warn");
LOGGER.error("TOTO error");
XML:
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t:%c] : %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value ="debug" />
<appender-ref ref="fileAppender"/>
</root>
When my app runs myLogFile is created and I can see the error log in it, but not the other ones:
2019-06-06 19:37:31 ERROR [http-/0.0.0.0:8080-2:my.classpath.PostLoginController] : TOTO error
Any idea what's wrong here?
You have to use like this for logging debug to error.
<log4j:configuration debug="false">
<appender name="consoleAppender" 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{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="default.console" />
<appender-ref ref="default.file" />
</root>
</log4j:configuration>
Okay, figured it out. I had to add
<category name="my.classpath">
<priority value="all" />
<appender-ref ref="fileAppender" />
</category>
because somehow the rootLogger level can't be altered.
When I'm tapping on the log button corresponding to my application of spring boot admin dashboard I'm getting Error while reading logfile: - error. Does somebody faced this already?
I'm using log4j with such configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="service-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="file" />
</root>
</log4j:configuration>
My application.yml file has such logging section:
logging:
file: "service-rest.log"
level:
org:
springframework:
boot:
autoconfigure:
logging: DEBUG
Also I could get that logfile from app directly http://url/actuator/logfile
The problem was the slightly wrong format of logs that has been pushed to service-rest.log file. The right one appender for file is
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="tvpp-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
Without that brackets around time and log level.
I want to create two log file.
Because my project has two modules and log each module in separate log files.
After that ,I have to log each module logging data independently .i have Single Log4j file.when i use appender -ref inside the root then i can achive but at that time aop or java logging cant appear in that file only manualy logger msg shown there
Try Something like :
<log4j:configuration>
<appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/Testing/Project_moduleOne.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="com.sp.logger1" additivity="false">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.sp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender1" />
<appender-ref ref="fileAppender2" />
</root>
</log4j:configuration>
I have below log4j configuration. when file reaches its max size , it is not rolling but when I restart server then rolling is working.
Please let me know what I am wrong in my configuration.
<appender name="ALog" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="logs/Log.txt" />
<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 ="debug"/>
<appender-ref ref="ALog"/>
</root>
Thanks.
You have to do :
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<appender-ref ref="file"/>
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>