I'm facing strange issue with log4j when deployed in IBM WebSphere Portal Server. Its working pretty well when tested as standalone application link here.
Its pretty straight forward RollingFileAppender xml configuraion 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="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="file" value="E:/logs/Mylog.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
<root>
<level value="DEBUG"/>
<appender-ref ref="fileAppender"/>
</root>
All I needed is when the log size Mylog.log exceeds 10MB, then it has to create Mylog.1.log, Mylog.2.log, Mylog.3.log and so on.. where as its creating Mylog.1.log, after some time Mylog.2.log will be created and Mylog.1.log will be deleted automatically (when Mylog.3.log would have been created then Mylog.1.log, Mylog.2.log would have been deleted).
Am I doing something wrong, As its enterprise web application, log4j-config.xml will be at business layer and on top of those multiple web applications will be accessing it using singleton factory pattern(One instance of log for all web application modules).
Tried many ways, but in vain, any help is highly appreciable. Thanks.
Related
I want to change the log level dynamically based on TEST/PROD server accessing the application.
Our web app has a lot of DEBUG and INFO,error log statements. Once we deploy the .war file to the test / production servers,
the log4j.xml also gets deployed which results in a lot of DEBUG and INFO statements in the log files.
How to show only error logs in PROD server and debug and error logs in DEV/TEST server. Apache Tomcat is the server we are using to deploy the application.
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- This appender logs to the console -->
<appender name="consoleAppender" 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:%L] - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.home}/logs/myProject.log"/>
<param name="append" value="true" />
<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>
<!-- Set "debug" log level for project code -->
<logger name="com.choonchernlim.myproject">
<level value="info"/>
</logger>
<!-- Set "info" log level for Spring framework -->
<logger name="org.springframework">
<level value="info"/>
</logger>
<!-- Set "debug" log level for Hibernate framework -->
<logger name="org.hibernate">
<level value="debug"/>
</logger>
<root>
<!-- The default log level is "warn" -->
<priority value="warn"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
Included below tags in web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
One approach i could think of is to edit setenv.bat from apachetomcatserver\bin and add loglevel. But how to set loglevels dynamically in the log4j.xml file???
in tomcatserver : catalinahome\bin\setenv.bat
set loglevel=error
How to dynamically set the loglevel so that when application is deployed in TEST all logs are shown and when deployed in PROD only error log messages are shown in the log file.
I am using log4j1.2,spring,java.
You would have to create different log4j.xml-files for the corresponding environments. And then during startUp, you can simply provide the path to the log4j.xml configuration it should use.
Application parameter:
-Dlog4j.configuration=path/to/env/folder/log4j.xml
If you are using spring framework, you can use logback for logging configuration and then you can control log level based on your active spring profile.
You can get some help form following examples-
http://www.baeldung.com/spring-profiles
https://springframework.guru/using-logback-spring-boot/
I have my webapp some-app.war running under Tomcat 8.5. I use Slf4j and Logback for logging within the webapp. When I log using log.info() or similar, the output gets written to catalina.out, the same log file as Tomcat uses for its internal logging. I want the logs from my application to be written to a separate log file. But I do NOT want to use logback's FileAppender to do it. I'd prefer to leave Logback using ConsoleAppender as I may deliver the WAR to others who won't have the same logging needs. I see the location of the log file as an aspect of the deployment environment, not the code.
Is there a way that I can manage per-application log files from the configuration of the Tomcat server itself -- while still using Slf4j and Logback in my code?
You can't know about others' logging needs, so there's not a silver bullet solution for this. However I'm sure the best option is not "I'm gonna leave it configured really badly because someone might log differently".
You can configure it how you wish for yourself, using FileAppenders and document the necessary steps to reconfigure it (e.g. edit the war).
You can also use more advanced configurations such as making use of syslog. Now other users have a standardized configuration option, just as long as they use syslog.
There are other options as well, but the common thread here is that you can't know what others may want, so don't waste time trying to guess it. Just do what works well for you at least.
If you placed logging.properties at [CATALINA_HOME/conf/logging.properties] then it should use it as your root logging properties file. Put you logging.properties or log.xml file in your application at [MyApp/WEB_INF/classes/logging.properties] and configure it like blow.
<?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="stderr" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.err"/>
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out"/>
<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.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="2MB" />
<param name="maxBackupIndex" value="100" />
<param name="file" value="${catalina.base}/logs/MyApp/MyApp.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="stdout" />
<appender-ref ref="stderr" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
I have a problem with log4j under tomcat.
When Eclipse deploys new version of application to tomcat, it reloads application context. After reloading context is completed, web application level logger stops to write to console.
Only tomcat logger still works.
My configuration in log4j.xml in web appplication is following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "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="stdout" 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>
<root>
<level value="INFO" />
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
I've put log4j-1.2.16.jar to /WEB-INF/lib directory of my application. Besides that I've put another copy of log4j-1.2.16.jar to tomcat's /lib directory, according to tutorial presented on official tomcat site.
Use the follow option of org.apache.log4j.ConsoleAppender. This options determines if the appender honors reassignments of System.out or System.err made after configuration. By default, false.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Follow" value="true" />
<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>
Thanks to Paul's answer I've discovered the real reason why logger didn't log after reloading context. The problem was that on application initialization I was invoking the following code:
System.setErr( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.ERROR ), true));
System.setOut( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.INFO ), true));
This code is supposed to write all output written to console (like System.out.println) to xml log on production server, but during devlopment I'm using console so it has to be turned off.
When I added <param name="Follow" value="true" /> to my configuration, there was no output at all in any case, so thanks to this option I managed to discover the real problem.
My log4j logger does not want to use the log4j.xml file to be configured. This file is located in the src folder and looks like that:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="ROLLING" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\debug\myproject\logfile.log" />
<param name="MaxFileSize" value="10024KB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ROLLING" />
</root>
</log4j:configuration>
But logfile.log is still empty and there is no DEBUG line in the console.
NB: it is a Java EE project on JBoss 7.1.0 and using Struts2.
The following answer is not entirely mine. It was posted in the question itself by the OP, so I'm moving it into this community wiki answer.
I resolved it by explicitely configuring the logging configuration.
Note that you won't be able to change the configuration at runtime if you configure it using a file.
For more information, see: https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F
Make sure the log4j.xml file is located in the /WEB-INF/classes directory of your web application.
The problem was with the log file. If you use the absolute file name make sure to replace backslashes with slash. For example
<param name="File" value="C:/debug/myproject/logfile.log" />
I am using log4j with RollingFileAppender and also triggeringPolicy with SizeBasedTriggeringPolicy.log file is splited but one file split on size 11kb but other can grow without spliting when it reach the given size and also give wrning message "log4j:WARN Failure in post-close rollover action".i could not able to solve the problem.my lo4j.xml file consists the following 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 class="org.apache.log4j.rolling.RollingFileAppender" name="FixedWindowRollingFile">
<param name="Append" value="true"/>
<param name="ImmediateFlush" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="fileNamePattern" value="E:/BonitaMQ_workflow/RequestHandlerService/logs/log_%i.log"/>
<param name="minIndex" value="1"/>
<param name="maxIndex" value="1"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="10240"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM,dd yyyy h:mm:ss a } %p %c{1}:%L - %m%n"/>
</layout>
</appender>
<logger name="com.bonitaservice" additivity="false">
<level value="INFO"/>
<appender-ref ref="FixedWindowRollingFile"/>
</logger>
<root>
<priority value="Debug"/>
<appender-ref ref="FixedWindowRollingFile"/>
</root>
</log4j:configuration>
please help me solve this problem.
Thanks
This behavior can be caused by logging to the same logfile from multiple JVM instances. A solution would be to append the processid to the logfile name, but I have not worked out yet how to do this.
I had the same problem. I did not investigate it fully but believe it is caused by the fact that FixedWindowRollingPolicy always rotates at least once, which means there is always an index 1 and an index 2. In your case a log_1.log and a log_2.log, even if your MaxIndex is set to 1.
In my case, increasing maxIndex to 2 solved the problem.
Cheers,
ED