English 中文(简体)
Creating daily logs with Log4j?
原标题:

What configuration values are needed to setup Log4j to use the following pattern?
MyApp-Mon.log
MyApp-Tue.log
MyApp-Wed.log
Etc

With each file containing the days log.

This sounds easy enough to do with Log4j s DailyRollingFileAppender but I am having trouble.
Here is my current config;

<appender name="daily-file" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="Threshold" value="info"/>
    <param name="DatePattern" value=" - EE .log "/>
    <param name="file" value="MyApp"/>
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{EEE MMM dd hh:mm:ss zzz yyyy} %-5p %l - %m%n"/>
    </layout>
</appender>

I based this config on this blog post, but it is not behaving in the way he describes. The log is being created as MyApp with no extension.

Can anyone help me out here?

最佳回答

I based this config on this blog post, but it is not behaving in the way he describes. The log is being created as MyApp with no extension.

This is what you told Log4J to do with this line:

<param name="file" value="MyApp"/>

Log4J uses the value of this option as log file name and the pattern is only applied when the roll over occurs, as documented:

For example, if the File option is set to /foo/bar.log and the DatePattern set to . yyyy-MM-dd, on 2001-02-16 at midnight, the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log until it rolls over the next day.

So everything looks normal to me.

To obtain the desired behavior, you could write your own Appender. It looks like James Stauffer did something very similar (see this answer) but he extends FileAppender. It should be easy to adapt his work for a DailyRollingFileAppender subclass though.

问题回答

the current logfile is named MyApp, then tomorrow it will be renamed by Myapp-yesterday and so on

but the current log has always the default name.

Looking at the JavaDocs for the DailyRollingFileAppender, all the examples they set out have specific, numeric date patterns, not day-name patterns. Have you tried to see if using one of their examples works? If so, and yours doesn t, then I d assume that the DailyRollingFileAppender doesn t support the use of a date pattern containing E (the day of the week).

In response to your comment to Pascal s answer:

Would a ln -s MyApp.log MyApp-NOW.log work? Your legacy log viewer would show MyApp-NOW.log via the symbolic link and log4j can roll-over MyApp.log to MyApp-TUE.log at midnight.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签