I read the official document of log4j2 and get a question about RollingFileAppender.
That's what the document says:
Below is a sample configuration that uses a RollingFileAppender with both the time and size based triggering policies, will create up to 100 archives on the same day (1-100) that are stored in a directory based on the current year and month, and will compress each archive using gzip and will roll every hour. During every rollover, this configuration will delete files that match "/app-.log.gz" and are 30 days old or older, but keep the most recent 100 GB or the most recent 10 files, whichever comes first.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="baseDir">logs</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="100">
<!--
Nested conditions: the inner condition is only evaluated on files
for which the outer conditions are true.
-->
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="100 GB" />
<IfAccumulatedFileCount exceeds="10" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
I think this configuration will create up to 100 archives on the same hour of the day, not 100 archives on the same day, can anybody who give me a hand? Thanks!
Yes, it will keep up to 100 files in the hour but in reality it will never get that high since it is only keeping a maximum of 10 files overall.
Related
Helllo everyone,
we have log4j from 1.2.8 to log4j2.11, and observed half daily logs files are not generating as expected.Only i can see only AM log files are generating.
<RollingFile name="file" fileName="testAMPM.log" filePattern="testAMPM.log-%d{yyyy-MM-dd-a}" >
<PatternLayout pattern="%d %5p %-120m [%t - %c:%L] %n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
Will not mark this as duplicate because this, question here did not get answered. The answer to another duplicate question does have an example though, so please try this here first:
<RollingFile name="fileAppender" filename ="${logName} append="true" FilePattern="${logName}.$$d{yyyy-MM-dd-a}">
<CronTriggeringPolicy schedule="0 0 0,12 * * ?" />
</RollingFile>
A comment in the first linked duplicate also suggests to check out Cron Triggering Policy, which could provide a better solution to your requirement than a TimeBasedTriggeringPolicy:
Just try it out.
I used the following Log4j2 configuration to test the behavior. It rotates the file every 10 seconds and should keep only 3 files. However no file is deleted. What am I missing?
Please don't suggest to use the delete action.
<RollingFile name="File" fileName="/var/log/mylog.log" filePattern="/var/log/mylog-%d{yyyy-MM-dd-HH-mm-ss}.log.gz">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
</RollingFile>
I realise this is an old question, but I had the same problem.
I also realise the OP said "Please don't suggest to use the delete action" so this may mean there are version restrictions on using Delete.
However I found this feature request which sheds some light on this behaviour - a comment there indicates.
DefaultRolloverStrategy max attribute only applies if you have a %i in
the file pattern.
The manual, under "Default Rollover Strategy", indicates
The default rollover strategy accepts both a date/time pattern and an
integer from the filePattern attribute specified on the
RollingFileAppender itself
and
max : integer : "The maximum value of the counter. Once this values is
reached older archives will be deleted on subsequent rollovers. The
default value is 7."
A bit a mix of "counter" and "integer" - but think (now) that it means this only applies to "%i" pattern. If the manual used "integer counter" it may be clearer.
A solution I got working (using 2.10.0 and days rather than seconds) using Delete (sorry OP) was:
<RollingFile name="MyLog" fileName="${sys:dataDir}/mylog.log" append="true">
<FilePattern>./mylog-%d{yyyy-MM-dd}.log.zip</FilePattern>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level - %msg %logger{36}%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:dataDir}" maxDepth="1">
<IfFileName glob="mylog-*.log.zip" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile >
hey I was wondering if it possible to have the same output in Console as in the file output.
Here is my XML config.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="log4j2 Logs">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
<RollingFile name="file" fileName="${basePath}/ActivateMaintenancePage.logs"
filePattern="${basePath}/ActivateMaintenancePage-%d{yyyy-MM-dd}">
<PatternLayout header="LOGGING START%n%n" footer="%n%nLOGGING END"
pattern="%3sn %30d{DEFAULT} [%M] %-7level %c{30} - %m%n" />
<Policies>
<OnStartupTriggeringPolicy minSize="0"/>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout header="LOGGING START%n%n" footer="%n%nLOGGING END"
pattern="%3sn %30d{DEFAULT} [%M] %-7level %c{30} - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="console" level="error"/>
<AppenderRef ref="file" level="trace"/>
</Root>
</Loggers>
</Configuration>
Output in RollingFile
1 2017-07-25 11:16:36,762 [initializeChrome] INFO class testNG.SimSettings - Web Chrome driver is now initilized.
2 2017-07-25 11:16:36,762 [lambda$0] INFO class testNG.SimSettings - Opening... http://msrvaq11vm.technomedia.ca/sigal_60/2017sp1/sp_polymont/_sim/PROD Sp2 2016 Sim...
3 2017-07-25 11:16:47,926 [initilizeAllElements] INFO class testNG.SimSettings - All #FindBy elements have been initilized.
4 2017-07-25 11:16:48,006 [change1stLevelFrame] INFO class testNG.SimSettings - Changing target to 1st level frame.
5 2017-07-25 11:16:49,719 [enterCredentials] INFO class testNG.SimSettings - UserName & Password: Success!
and empty in Console. But now if I change
<AppenderRef ref="console" level="error"/>
to "trace"
It will be 2,4,6....in console and in my file it will be 1,3,5,7... which is easily understandable.
But my question is how can we have both the same log-level (trace) output in Console and File?
(adding tag with package name and level did not work)
Related to this question: log4j2 xml configuration - Log to file and console (with different levels)
I'm not sure if I read your question correctly but it seems that you want to render some unique value in the log output, such that the same log event has the same unique value in the Console log and the File log output.
The sequence number pattern converter will increment every time a log event is rendered. The same log event is rendered separately for each Appender, so different Appenders will never have the same sequence number.
There are a number of alternatives. One idea is to include %nano nanotime in the pattern layout. This value is captured when the application makes the logging call and will be the same for all appenders. An alternative is to create a custom Log4j2 pattern converter or lookup.
The following is my time and size based file rolling appender
<RollingFile name="fileWriter" fileName="${LOG_DIR}/file.log"
filePattern="${ARCHIVE}/file_log.%d{yyyy-MM-dd}-%i.gz">
<PatternLayout pattern="${PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
To reduce the number of log files, I would like to make it rolling over by time, so that the files generated 30 days ago is automatically removed.
Could DefaultRolloverStrategy help in my case? If it doesn't, would anyone provide some suggestions? Many Thanks.
Yes, the DefaultRolloverStrategy will do what you want. You should be able to configure:
<DefaultRolloverStrategy max="30"/>
As an alternative, you can also specify a Delete action something like:
<DefaultRolloverStrategy>
<Delete basePath="${ARCHIVE}" maxDepth="2">
<IfFileName glob="*/file_log-*.gz" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
I'm using log4j2-beta9 and want to configure it using a log4j2.xml in strict mode.
My issue is: how do I specify attributes that are not in the shipped schema file?
An Example:
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration
status="DEBUG"
strict="true"
monitorInterval="5"
name="TestingAttributes"
verbose="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Log4j-config.xsd">
<Properties>
</Properties>
<Appenders>
<Appender
type="Console"
name="SYSERR"
target="SYSTEM_ERR"> <!-- cvc-complex-type.3.2.2: Attribute 'target' is not allowed to appear in element 'Appender'. -->
<Layout Type="PatternLayout">
<Pattern>%date{dd.MM.yyyy HH:mm:ss,SSS} %5p %logger %m%n</Pattern>
</Layout>
<Filters>
<Filter
type="MarkerFilter"
marker="FLOW"
onMatch="DENY"
onMismatch="NEUTRAL" />
<Filter
type="MarkerFilter"
marker="EXCEPTION"
onMatch="DENY"
onMismatch="NEUTRAL" />
</Filters>
</Appender>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="SYSERR" />
</Root>
</Loggers>
</Configuration>
Notice that I want to set the appender to have the target SYSTEM_ERR but the attribute is not allowed in strict mode.
target="SYSTEM_ERR"> <!-- cvc-complex-type.3.2.2: Attribute 'target' is not allowed to appear in element 'Appender'. -->
I could always edit the Log4j-config.xsd and allow that attribute there but that would be kind of wrong also because not all appenders have a target attribute.
As searching the web didn't help me so far, I'm asking you:
Is there anything I'm missing in configuring Log4j2 in strict XML mode?
Do I have to "patch" the XMLConfiguration and the schema file and commit a change to log4j or is there another way besides not using strict mode?
Thanks in advance.
Can you ask this on the log4j-user mailing list? It could be a bug in the schema, but I suspect the schema can do with more improvements and your feedback would be valuable.