How to log jasper report logs to separate log file? - java

I am new to JasperReport and Log4j, Now I have to Log all log messages related to jasper report to a new log file. As of now it writes to log file and console bit I want it to log only on separate log file. How can achive this.
Here is my log4j.xml 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">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p :%L - %m%n" />
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="console" />
<appender-ref ref="file" />
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.home}/logs/ITS_Server.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p:%L - %m%n" />
</layout>
</appender>
<category name="org.hibernate">
<priority value="DEBUG" />
</category>
<category name="java.sql">
<priority value="debug" />
</category>
<root>
<level value="DEBUG" />
<appender-ref ref="file" />
</root> -->
</log4j:configuration>
Please Anyone can help me in this please.

you have to define a new appender:
<appender name="jasper" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.home}/logs/jasperReports.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p:%L - %m%n" />
</layout>
</appender>
And a new category with have the apender as reference:
<category additivity="false" name="net.sf.jasperreports">
<priority value="debug" />
<appender-ref ref="jasper"/>
</category>

Related

Log4j level not working for the root logger

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.

i was create two log files using RollingFileAppender in log4j.xml

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>

log4j: RollingFileAppender is not working when file reaches max size, but rolling when restart

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"/>

Stop Hibernate SQL logs coming on catalina.out

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

How to solve the warnings in log4j???Continuable parsing error...The content of element type "log4j:configuration" must match [duplicate]

This question already has answers here:
“log4j:configuration” must match “(renderer….loggerFactory)?)”
(2 answers)
Closed 2 years ago.
Warnings are shown ...
log4j:WARN Continuable parsing error 28 and column 23
log4j:WARN The content of element type "log4j:configuration" must match
"(renderer*,appender*,(category|logger)*,root?,categoryFactory?)".
when execute the java files
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="\n[%-6r] - %m" />
</layout>
</appender>
<category name="se.jayway.ddsteps">
<priority value="info"></priority>
</category>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="file"
value="D:/workspace_Testing/IdMUnitTests/logs/IdMUnitlog.log" />
<param name="immediateFlush" value="true" />
<param name="threshold" value="debug" />
<param name="append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<root>
<priority value="INFO"></priority>
<appender-ref ref="console" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
According to the error message, the DTD expects all elements in an order specified.
(renderer*,appender*,(category|logger)*,root?,categoryFactory?)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="\n[%-6r] - %m" />
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="file"
value="D:/workspace_Testing/IdMUnitTests/logs/IdMUnitlog.log" />
<param name="immediateFlush" value="true" />
<param name="threshold" value="debug" />
<param name="append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<category name="se.jayway.ddsteps">
<priority value="info"></priority>
</category>
<root>
<priority value="INFO"></priority>
<appender-ref ref="console" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
XML Configuration Order
So remember to write xml in that order
<renderer></renderer>
<appender></appender>
<plugin></plugin>
<logger></logger>
<category></category>
<root></root>
<loggerfactory></loggerfactory>
<categoryfactory></categoryfactory>

Categories