problem comes using SizeBasedTriggeringPolicy with log4j? - java

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

Related

Trouble with log4j configuration

I have been trying to change the logging from console to both console as well as file. Looking at the large documentation on log4j I was able to add the appender as shown in the code below. However I don't seem to be getting logs in the specified file.
Any ideas what might be wrong? Here is my log4j.xml (already located at src/)
<?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">
<param name="Threshold" value="DEBUG"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="file" value="/data/MFlogs/log.log"/>
<param name="immediateFlush" value="true"/>
<param name="threshold" value="debug"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
change file path tag. try it with double slash.
value="//data//MFlogs//log.log"
Try adding full path to path location or add ${catalina.home}/logs/myApp.log to it.
Read this question, it tells how to use relative path. It is easy if you use properties file instead of xml file.

How to set value in log4j.xml using property file?

I would like to know if there is possibility to set an attribute in log4j.xml by using property file. For example the log4j.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %l - %m%n "/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="\D:\ReadText_File\log4jlogss.txt"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}]- %l - %m%n"/>
</layout>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
I wan to specify the path for the file using Properties file
Any idea how can we do it ????
about log4j, this type of configuration that XML configuration which embed the property file is not supported in current version.
for details, you can view the log4j's source code . the XML parse details is in class
DOMConfigurator.configure
You can use Log4j Lookups with PropertySubstitution:
See http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution and http://logging.apache.org/log4j/2.x/manual/lookups.html
Here is an example on StackOverflow: Log4J2 property substitution - default
(to make it short, you can use, as far as I know, the System Property using -Dkey=value)
You can also try to use the ${bundle:com.package.Messages:MyKey} syntax to use a properties file (using log4j 2.0-rc1 or greater), see https://stackoverflow.com/a/19303208/1149528.

hibernate logging can't stop it

I've tried I think all the info I've found in the Internet, with no results. I want to switch off the Hibernate Logging.
My log4j.properties:
log4j.logger.org.hibernate = INFO
log4j.logger.org.hibernate.SQL=INFO, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false
log4j.rootLogger=INFO
My log4.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<logger name="org.hibernate">
<level value="info"/>
</logger>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="out/learning.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
Inside my hibernate.cfg.xml:
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
At this moment I don't know what else can I do to stop those disturbing messages on my console.
Thanks in advance for your help.
try to use OFF level for each appender might help
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
I would encourage you to read the SLF4J Manual, you should only have 1 Binding jar on your classpath.
Remove slf4-simple-1.4.2.jar from your classpath, it may be stopping log4j being used.
slf4j-simple-1.7.5.jar
Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and
higher are printed. This binding may be useful in the context of small
applications.

Log4j, disable appenders dependent on environment

I would like to have one log4j.xml config and be able to log to console while developing my application. Once deployed to an environment I want to only log to a file appender and not the console. How can I achieve this?
This is my current config:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>
One of the solutions could be separation of log4j configuration files for development and production environments e.g.:
log4j-development.xml - for development environment
log4j-production.xml - for production environment
Then your application startup command could have parrameter specifying log4j configuration file e.g. java -Dlog4jconfig=log4j-development.xml -jar Application.jar
You can configure log4j by getting value of log4jconfig property in your code e.g. System.getProperty("log4jconfig").
Pros of that solution are as follows:
You can specify loggers independently (ConsoleAppender and LogFileAppender in development and only LogFileAppender in production)
You can specify your logging level per environment (e.g. error in production and debug in development)
You can configure file logger independently e.g. keep logs for X days in production (for audit purposes etc.) and have only one log file in development etc.
That pattern is used in many application servers where you have multiple environments (Development, UAT, Staging, Production etc.)
Example of log4j-development.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4j:configuration>
Example of log4j-production.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="error" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>
It is easy to do using appender's Threshold parameter and JVM system property. E.g.
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="${my.console.level}" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n" />
</layout>
</appender>
Then when starting application on prod use -Dmy.console.level=OFF
At the same time when starting locally use -Dmy.console.level=ALL
Both OFF and ALL are valid log4j levels.
With Log4j2 you could alternatively switch the appender reference with a Java system property like so:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<!-- Log file location uses Tomcat system variable, change for other web servers -->
<RollingFile name="rolling-file"/>
<Console name="console"/>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="${sys:log4j.loggers.root.appender:-rolling-file}"/>
</Root>
</Loggers>
</Configuration>
(the minus - in front of the default value in the sys prop variable is required for some reason)
To switch from the default "rolling-file" to the console"
-Dlog4j.loggers.root.appender=console

Configuration logging in Play Framework at start up auto-test

I configured the logging in my Play Framework application. When I run application in prod or dev mode or run test by comand (play test) - everything works fine, but test can not executes when I run their 'play auto-test'. Please help!
in application.conf:
application.log=INFO
application.log.path=/log4j.xml
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${application.path}/logs/application.log"/>
<param name="MaxFileSize" value="1MB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p ~ %m %n"/>
</layout>
</appender>
<logger name="play">
<level value="error"/>
</logger>
<root>
<priority value="error"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
When I use the following log4j.xml file:
<?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">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
tests running the command 'play auto-test' successfully executed. Please tell how configure logging in Play (output to file) that run and executes tests in 'play auto-test'!
Thi is pretty much a Play! bug. It's not caused by the fact that you've configured log4j with a separate file, though doing that will hide another error message: "play.tmp is null when it should be play.tmp=none" (something to this liking - you'll get this if you remove your custon log4j.xml file and re-do "play auto-test"). Problem is that even if you set that to play.tmp=none it will still not work. Besides, if you compare their online documentation to Play!'s console output you'll already realize that something's wrong:
The docs say:
"The 'auto-test' command do the same than the 'test' command, but it automatically launch a browser, run all the tests, and stop."
Console sais (when you do "play auto-test"):
ATTENTION: You're running Play! in DEV mode
~
~ Go to http://localhost:9000/#tests to run the tests
~
The answer was very simple
in application.conf:
%test.application.log=INFO
%test.application.log.path=/log4j.xml
application.log=INFO
application.log.path=/log4j.properties
application.log.system.out=off
log4j.xml:
<?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">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
log4j.properties:
log4j.rootLogger=ERROR, Rolling
log4j.logger.play=INFO
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=${application.path}/logs/application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n

Categories

Resources