log4j FileAppender using FileDatePatternConverter - date

Trying to use a pattern for filename in config such as:
<appender name="FileAppender" class="org.apache.log4j.FileAppender">
<param name="File" class="org.apache.log4j.pattern.FileDatePatternConverter" value="logs/App-%d{yyyy-MM-dd_HH-mm-ss}.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p(%c{1}) %d{yyyy-MM-dd HH:mm:ss.SSS}: %m%n" />
</layout>
</appender>
This does not what work. What is correct syntax?

Related

Stop log4net (or services) using a file when errors are not being placed in it

I have the following log4net config
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<appender name ="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\temp\Generator.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maximumFileSize value="500KB" />
<maxSizeRollBackups value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{DATE} %level %thread %logger - %message%newline" />
</layout>
</appender>
<appender name ="GenerationErrors" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="c:\temp\GenerationErrors.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{DATE} %level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFile" />
<level value="INFO" />
</root>
<logger name ="GPA.Module.Generate">
<level value="INFO" />
<appender-ref ref="GenerationErrors" />
</logger>
<logger name="PmuManagement">
<level value="INFO" />
<appender-ref ref="GenerationErrors" />
</logger>
</log4net>
The first appender logs all errors for all of my processes and the second only logs a certain group of errors, as required.
I need to be able to delete the contents of the second log file on the fly however this isn't currently possible as the programme needs constant access to it to run (if you try and make any changes you get the "this file is in use by another programme" error.
Is there a way for log4net to only need access to the file when errors need to be written to it?
Or is there a more generic way to stop services/programmes needing access to file temporarily and allowing access again shortly after?
Thanks
I found this question here Intermittent log4net RollingFileAppender locked file issue and the answer is what I needed. I simply added <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> to my second appender.

extras RollingFileAppender not creating a file

I have reviewed other related questions but have not found a answer for this. It seems the javadocs at https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html are incorrect and/or incomplete. At least the element looks to have a spurious double quote mark in it.
I am starting up in debug mode and it all looks OK. Yet, I cannot find the log file. We have other custom appenders that are working fine but I am trying to replace them with the OTB solution.
Here is my config:
<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="threshold" value="debug" />
<rollingPolicy name="file" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="logs/My_Server.%d{yyyy-MM-dd-hh}.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %r [%t] %-5p %c{1} %x - %m%n"/>
</layout>
</appender>
<appender name="ASYNCfile" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="500"/>
<param name="blocking" value="false"/>
<appender-ref ref="file"/>
</appender>
<root>
<priority value="INFO"/>
<appender-ref ref="ASYNCfile"/>
<appender-ref ref="console"/>
</root>
And the debug console:
log4j: Attaching appender named [file] to appender named [ASYNCfile].
log4j: Class name: [org.apache.log4j.rolling.RollingFileAppender]
log4j: Setting property [threshold] to [DEBUG].
log4j: Setting property [fileNamePattern] to [logs/My_Server.%d{yyyy-MM-dd-hh}.log].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ISO8601} %r [%t] %-5p %c{1} %x - %m%n].
log4j: setFile called: logs/My_Server.2017-07-17-05.log, true
log4j: setFile ended
I don't see any errors related to this in the console ouput - there is just no file to be found.

log4j writes blank lines in console

I have the following log4j configuration:
<appender name="MYCONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="INFO"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}|%-5p|MYAPP|%t|%C{1}.%M(%L)|%m%n"/>
</layout>
</appender>
<appender name="MYFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${jboss.server.log.dir}/myapp.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}|%-5p|MYAPP|%t|%C{1}.%M(%L)|%m%n"/>
</layout>
</appender>
In the Eclipse console, every log line produced by this configuration is followed by a completely blank line:
12:46:27,289 INFO [stdout] 2017-01-25 12:46:27,289|INFO|MYAPP|something
12:46:27,289 INFO [stdout] 2017-01-25 12:46:27,289|INFO|MYAPP|something else
This happens only with logs generated by MYCONSOLE, all other logs (such as the internal JBoss's ones) don't have blank lines.
Also, in the MYFILE log file, the logs are the same but the blank lines are not there.
What could be the reason?
I was able to get rid of the extra lines by using \n instead of %n.
The reason of the strange behaviour of %n is still not clear to me.
Getting same to me when I use consoleAppader for audit logger
log4j.appender.audit=org.apache.log4j.ConsoleAppender
log4j.appender.audit.layout=org.apache.log4j.PatternLayout
log4j.appender.audit.Target=System.out
log4j.appender.audit.ConversionPattern=%d{ISO8601}|%t|%m%n
The logs dumping on console are empty/blank lines.
If I remove I can get rid of empty lines, I am missing actual data too.
Would appreciate any pointers

Will this log4j configuration file create application.log files appended with the timestamp?

I have a log4j configuration file that looks like:
<?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="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d
%-5p [%c{1}] %m %n" />
</layout>
</appender>
<appender name="fileAppender"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="File" value="/var/output/logs/application.log"/>
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"/>
</layout>
</appender>
<logger name="connect.testJava" additivity="false" >
<level value="DEBUG" />
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</logger>
</log4j:configuration>
I wanted to basically create logs with the format application.log like application2013-06-12 12:12:00.log or something like that. The above configuration does not seem to be doing that. Can someone please help me out and tell me what I might be doing wrong? I am just about a couple of hours into log4j, so sorry if this question sounds too naive.
Yes it does. Just wait till midnight and a new file will be created. The day's file will remain application.log

Perf4J #Profiled annotation + log4j is not working

I have got the following problem: I want to monitor the execution time of some low-performance methods by using perf4j #Profiled annotation and log4j.
The problem is the monitoring log file is empty and looks like this:
Performance Statistics 2013-07-17 18:07:50 - 2013-07-17 18:08:00
Tag Avg(ms) Min Max Std Dev Count
Performance Statistics 2013-07-17 18:08:00 - 2013-07-17 18:08:10
Tag Avg(ms) Min Max Std Dev Count
The log4j.xml is contained in projectRoot/conf/exp directory and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!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="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./logs/exp-log.txt"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %d{HH:mm:ss.SSS} %c %m%n"/>
</layout>
</appender>
<appender name="CoalescingStatistics"
class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="10000"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="graphExecutionTimes"/>
<appender-ref ref="graphExecutionTPS"/>
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/perfStats.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<appender name="graphExecutionTimes"
class="org.perf4j.log4j.GraphingStatisticsAppender">
<!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
<param name="GraphType" value="Mean"/>
<!-- The tags of the timed execution blocks to graph are specified here -->
<param name="TagNamesToGraph" value="firstBlock,secondBlock"/>
<appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="graphExecutionTPS"
class="org.perf4j.log4j.GraphingStatisticsAppender">
<param name="GraphType" value="TPS"/>
<param name="TagNamesToGraph" value="firstBlock,secondBlock"/>
<appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="graphsFileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/perfGraphs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="org.perf4j.TimingLogger" additivity="false">
<level value="INFO"/>
<appender-ref ref="CoalescingStatistics"/>
</logger>
<logger name="sui">
<level value="debug"/>
</logger>
<root>
<priority value="debug"/>
<appender-ref ref="logfile"/>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
I have got the following annotations in my sources:
#Profiled(tag = "exportRecsForAccount")
protected void exportRecsForAccount(NetSuiteAcctInfo acctInfo) {
#Profiled(tag = "exportRecsForEachAccount")
protected boolean exportRecsForEachAccount() throws QueueManagerException, CriticalDBConnectionFailed {
Besides that, I have got the aop.xml located in projectRoot/recources/META-INF. Its content is the following:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<aspects>
<aspect name="org.perf4j.log4j.aop.TimingAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="comp.sui.ExportService"/>
</weaver>
</aspectj>
where comp.sui.ExportService is a link to the source code class.
So the question is what am I doing wrong? When I use StopWatches stamps everything is all right, but with #Profiled annotations seems nothing works.
Try:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<aspects>
<aspect name="org.perf4j.log4j.aop.TimingAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="comp.sui.ExportService"/>
<!-- Weave in perf4j aspects for Aspectj 1.6.7+ -->
<include within="org.perf4j.slf4j.aop.*"/>
<include within="org.perf4j.aop.*"/>
</weaver>
</aspectj>