You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Remko Popma (JIRA)" <ji...@apache.org> on 2015/10/18 17:19:05 UTC

[jira] [Commented] (LOG4J2-671) Property using ${date:} is evaluated multiple times across appenders

    [ https://issues.apache.org/jira/browse/LOG4J2-671?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14962435#comment-14962435 ] 

Remko Popma commented on LOG4J2-671:
------------------------------------

Apologies for the very late response.

One solution would be to create a custom lookup. It could look something like this:
{code}
@Plugin(name = "starttime", category = StrLookup.CATEGORY)
public class StartTimeLookup extends AbstractLookup {
    private long time;
    /**
     * Initializes the time field once, then returns formatted results.
     *
     * @param event The current LogEvent (ignored by this StrLookup).
     * @param timeFormat the format to use for the date-time result
     * @return The time, formatted as requested.
     */
    @Override
    public String lookup(final LogEvent event, final String timeFormat) {
        if (time == 0) {
            time = System.currentTimeMillis();
        }
        return new SimpleDateFormat(timeFormat).format(new Date(time));
    }
}
{code}
Then in your configuration you could do:
{code}
<Properties>
    <Property name="log_date">${starttime:yyyy-MM-dd'T'HHmmssSSS></Property>
    <Property name="log_path">log/${log_date}</Property>
</Properties>
{code}


> Property using ${date:} is evaluated multiple times across appenders
> --------------------------------------------------------------------
>
>                 Key: LOG4J2-671
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-671
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Configurators
>    Affects Versions: 2.0-rc1
>         Environment: OS: Ubuntu 12.04
>            Reporter: Robert Veitch
>
> At the top of my log4j2.xml config I have the following properties:
> {code}
> <Properties>
>     <Property name="log_date">${date:yyyy-MM-dd'T'HHmmssSSS></Property>
>     <Property name="log_path">log/${log_date}</Property>
> </Properties>
> {code}
> I would like to use the "log_path" property as the root log directory for all my appenders. By including a timestamp in "log_path" I can distinguish between multiple runs of my application. My appenders refer to "log_path" in the following way:
> {code}
> <RollingRandomAccessFile name="ApplicationLog" append="true"
>         fileName="${log_path}/application.log"
>         filePattern="${log_path}/application.log-$${date:yyyy-MM-dd'T'HHmmssSSS}.gz">
> ..snip..
> </RollingRandomAccessFile>
> {code}
> I have several such appenders, and I'd like them to all refer to the same "log_path" value. Unfortunately, it seems that the property gets re-evaluated for each appender, so I end up with multiple log directories on a given run of my application. Below is an example:
> ls -al log/
> total 20
> drwxr-xr-x 5 rveitch rveitch 4096 Jun 16 14:12 .
> drwxrwxr-x 6 rveitch rveitch 4096 Jun 16 13:18 ..
> drwxrwxr-x 2 rveitch rveitch 4096 Jun 16 14:12 2014-06-16T141225420
> drwxrwxr-x 2 rveitch rveitch 4096 Jun 16 14:12 2014-06-16T141225462
> drwxrwxr-x 2 rveitch rveitch 4096 Jun 16 14:12 2014-06-16T141225501
> Is there anyway to evaluate a property once, such that the value is frozen for re-use across appenders? Is there an alternative approach to generate a timestamp which I can use as a part of a common log directory path?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org