Spring Batch: Logs not written in log file in AfterJob Method - java

I'm facing a problem with the logs when I'm launching the Job on Unix, actually the log file contains all the informations except those that I write in the log in a JobExecutionListener and exactly in the AfterJob Method.
When I'm using Eclipse I can see the report lines in the console.
Here is the configuration of log4j
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appenderConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-6p [%-25c{1}] %m%n" />
</layout>
</appender>
<appender name="traceDefaultAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="log.tmp"/>
<param name="encoding" value="ISO-8859-1" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-6p [%-25c{1}] %m%n" />
</layout>
</appender>
<appender name="reportDefaultAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="log.tmp"/>
<param name="encoding" value="ISO-8859-1" />
<param name="threshold" value="REPORT#com.cegedimactiv.fwb.core.business.log.LevelReport"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<logger name="defaultLogger">
<level value="INFO" />
<appender-ref ref="traceDefaultAppender" />
<appender-ref ref="reportDefaultAppender" />
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="appenderConsole" />
</root>
</log4j:configuration>
Any Idea?

Related

Log file with date not generated

Trying to generate a log file with date.
Below is my xml configuration for log4j. From below configuration its generated only app.log but I am expecting app.log.2020-3-1.
Can someone please help me what is the issue in my 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="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<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="file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG" />
<param name="append" value="true" />
<param name="File" value="logs/app.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<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="consoleAppender" />
<appender-ref ref="file" />
</root>
</log4j:configuration>

How to separate records in log file by blank line? Log4j

I try to make my log file more readable so I want to separate records in my log file by a blank line after every application run. I use log4j.
I understand that the easiest way is to append %m%n to a single record in code. But I'm interested in doing it via configuration file.
So the question is what I have to add to configuration file log4j.xml?
Here is the context of my log4j.xml configuration file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
>
<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="logs/logs.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>
<root>
<level value="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
You could use another Logger defined in your configuration that uses the same settings except its ConversionPattern which will contain only %n. Here is an example:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">
<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="logs/logs.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>
<appender name="file2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n"/>
</layout>
</appender>
<logger name="com.company.MyClass">
<level value="DEBUG"/>
<appender-ref ref="file2"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
Then, when your app starts:
Logger logger = Logger.getLogger(com.company.MyClass.class);
logger.info(" ");
I'm not sure to understand what you want to achieve and why, but if you want an empty line between each log entry, you just have to add an extra %n at the end of your ConversionPattern:
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n%n"/>
(%n means "new line")

How to maintain Multiple log4j.xml configuration file

How to maintain multiple log4j.xml configuration files in one webapplication.
Eg:- In Servlet:-
public void init(ServletConfig arg0) throws ServletException {
PropertyConfigurator.configure("/home/madhavib/Desktop/Nar/Raju/log4j.properties");
}
In Jsp:-
public void jspInit() {
DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml");
}
In another Jsp:-
public void jspInit() {
DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml");
}
**log4j configuration files (loaded in jsp 1):**
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/infojsp333.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<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="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<!--<root>
<appender-ref ref="FileAppender"/>
</root> -->
<logger name="pe1.com1.nextel1">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
**log4j confuguration (loaded in jsp 2) :-**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<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="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<logger name="pe.com.nextel">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
**log4jconfiguration file(loaded in servlet) :-**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<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="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<logger name="pe.com.nextel">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
How to maintain multiple log4j.xml configuration files in one webapplication.
i have given log4j configuration information files , please look into that and tell me the how to proper develop with multiple log4j files.
You can pass multiple log4j configuration files in some ways:
You can set System properties :
System.setProperty("log4j2.configurationFile", "log4j2-1.xml,log4j2-2.xml");
You can put configuration files as "log4j2.component.properties" under resource section and define system property as follows :
log4j2.configurationFile=log4j2-1.xml,log4j2-2.xml
Please check official documentation for system properties

log4j prints debug to file, but not info

I am able to print debug logs to a file, but I am unable to print info. However, printing info and debug to console works correctly. How do I go about getting info to print in a file as well?
Thanks
<!DOCTYPE log4j:configuration SYSTEM "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= "%-5p [%t]: %m%n"/>
</layout>
</appender>
<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="example.log"/>
<param name="MaxFileSize" value="1000000KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value= "%-5p [%t]: %m%n"/>
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="stdout" />
<appender-ref ref="R" />
</root>
</log4j:configuration>
So I was using the java logger object rather than the apache4j logger object...

How to get log4j to log my firms classes only

Below is a code of the log4j.xml file we are using at my firm. We been trying to change it to log only output from classes with made that is in org.xxxx. can someone let me know if this can be done and how to do it
<?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 [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/disater_relief.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollinglogfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="dailyrollinglogfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="datePattern" value=" '.' yyyy-MM-dd "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
</log4j:configuration>
Your root looger defines debug logging for all components. You should change it to warn (so you still see warnings) and add an extra logger for your own components:
<logger name="com.xxx.mycomponent">
<level value="trace"/>
<!-- appenders ... -->
</logger>
<root>
<priority value="warn" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
Simply turn off the logging for the package you don't want to see in your logs...
<!-- Limit JBoss categories -->
<category name="org.jboss">
<priority value="OFF"/>
</category>

Categories

Resources