log4j2 RollingFile Error renaming file - java

I have this appender
<RollingFile name="Application" fileName="/home/user/log/abc.log" filePattern="/var/log/mylog/abc-%d{yyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="9999"/>
</RollingFile>
when the 5 MB are reached and there is no space on /var/log/mylog I get the error "ERROR renaming file from xx to yy If I make space afterwards, the logger does not try again to rename the file. Any ideas how I could solve that?
I use log4j2 2.2

This is not an answer. I don't have enough reputation to make a comment.
SUGGESTION: Log4j2 allows you to automatically delete files older then X days. I've been struggling with this some time ago. The issue has been resolved here:
Log4j2 - Configure RolloverStrategy to delete old log files
Maybe this will be helpful for you.

If you configure a FailoverAppender and put your RollingFileAppender within it then you can configure the FailoverAppender to retry at an interval you configure. In the meantime, the log events can be routed to a secondary appender that can do something else. See http://logging.apache.org/log4j/2.x/manual/appenders.html#FailoverAppender.

try removing ":" here {yyyy-MM-dd HH:mm:ss.SSS}
something like below
<PatternLayout>
<Pattern>%d{yyyy-MM-dd} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>

Related

How do I delete a log file while using RollingFileAppender in log4j?

Sometimes, while rolling log files, there seems to be some issue occurring like: 'Unable to delete log file', 'Unable to move log file'.
Error: java.nio.file.FileSystemException a.log -> directory\a-2011-08-09-2.log: The process cannot access the file because it is being used by another process.
Following is the log4j configuration:
<RollingFile name="a" fileName="${sys:catalina.base}/logs/a.log" append="true" createOnDemand="true" filePattern="${sys:catalina.base}/logs/a-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601}|%-5p|%i|%T|%s|%R|%t|%c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>

Sending particular INFO log line to a different file

I have a particular log line with some information, which needs be logged in to a different file for future use. I have already configured log4j.properties in my system to log necessary information.
Eg:-
[2017-07-28 20:33:07,798] INFO - 'john#doe.com' logged in at [2017-07-28 20:33:07,798+0530]
I have to filter out this and append to a org.apache.log4j.DailyRollingFileAppender
Appreciate any help.
Below is the solution for xml configuration. You can find the the details here. As an example you can specify two appenders: one for storing all the lines and one for storing your special lines. Then, for the appender storing your special lines set up the filter in the following way (example for a RollingFileAppender):
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*logged in at.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
Also check this question where you can find the clue on how to configure the same using properties.

log4j2 periodically cleaning log file

I use log4j2 to log my programs. In my xml configuration file, I have this appender:
<RollingFile name="General" fileName="log/logs/giornale.log" filePattern="log/logs/log-%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss,SSS} [%t] %-5level %logger{-1} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
What I need is a way to configure it, so that it will delete automatically all files older than n days.
I already found some questions like this, but they don't help, since they don't say how to do it via xml configuration.
To make it short, where exactly am I suppose to indicate parameters like "MaxBackupIndex" in the above snippet? Or which other parameter should I use (and where can I put it)?
Add the following tag under 'RollingFile' tag. Remove the 'policies' tag. You probably don't need it.
<DefaultRolloverStrategy>
<Delete basePath="log/logs" maxDepth="2">
<IfLastModified age="60d" />
</Delete>
</DefaultRolloverStrategy>
With this configuration, logs older than 60 days will be auto-deleted.
Refer to the log4j2 docs for more configuration information.
You can add the DefaultRolloverStrategy to your xml as -
<RollingFile name="General" fileName="log/logs/giornale.log" filePattern="log/logs/log-%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss,SSS} [%t] %-5level %logger{-1} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="<specify maximum archive count>"/>
</RollingFile>
As well you can use the combination of time and size based roll over for logs using -
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
..a sample configuration that uses a RollingFileAppender with both the
time and size based triggering policies, will create up to 7 archives
on the same day (1-7) that are stored in a directory based on the
current year and month, and will compress each archive using gzip and
will roll every 6 hours when the hour is divisible by 6

User JVM parameter in log4j2 configuration

I have an RollingFile Appender defined in my log4j2.xml.
<RollingFile name="RollingFile" fileName="/logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} - %-5p - %m - [%l]%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
What I want to do is to pass this parameter to the JVM at startup:
-Dapp_home=/home/admin/server
The documentation is pretty straight forward. And from what I understand it should work like this:
<RollingFile name="RollingFile" fileName="${jvmrunargs:app_home}/logs/app.log"
But it doesn't. I verified that it is working in general by using the absolut path.
In an other application where I use log4j (1.x) it works like this:
log4j.appender.file.File=${app_home}/logs/app.log
Take a look at the System Properties Lookup section of the documentation. If you define a variable as system property using -D like this:
-Dapp_home=/home/admin/server
use
${sys:app_home}
in your Log4j 2 configuration to access it.

Is it possible to configure log4j to create a new file with every run of the application?

For example, the first time I run an application (or immediately after I clear out the /logs directory), I want log4j to write the application's logs to a file called log.0. Then, I exit the application and restart it, I want the logs to be written to log.1. And so on.
I would like to keep this in the configuration file, although if I can't, I guess I could always do it in my application, when log4j is set up.
Is this possible? If so, how?
May be this is what you are looking for
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/
**Edit:**I got one more solution!
But no idea whether it works or not,but you can try
http://www.mail-archive.com/log4cxx-user#logging.apache.org/msg02132.html
Solution with log4j2:
<RollingFile name="RollingFile" fileName="${log-path}/GScraper.log"
filePattern="${log-path}/GScraper_%d{yyyy-MM-dd}_%i.log">
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%level\t%d{yyyy-MM-dd HH:mm:ss} %c: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="32 MB" />
<OnStartupTriggeringPolicy/>
</Policies>
</RollingFile>
Note OnStartupTriggeringPolicy and %i in filePattern. This way, Log4j will create new log file with index each time you run the App.
See logback's manual on uniquely named files (by timestamp).
Off the top of my head I don't think this is possible from with Log4j. Maybe when you shut the application down you could rename the .log file so next time you start up a new log file is created.

Categories

Resources