logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Blaine Bergeson (bbergeson)" <bberge...@micron.com>
Subject RE: Observations about the Context Map and the Header attribute
Date Thu, 22 Jan 2015 16:29:26 GMT
I was finally able to get a solution that worked by using Routing.  It seems that maybe the
evaluation of the route is delayed so that the values of the context map are available by
the time it is evaluated.  Here is an example of my configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="MyApp" status="warn">
        <Routing name="Routing">
            <Routes pattern="$${ctx:mode}">
                    <RollingRandomAccessFile name="RollingRandomAccessFile" fileName="${ctx:logDir}/${ctx:logFilename}.log"

                            <header>${ctx:app_title} ${ctx:app_version}  ${java:version}
- ${java:vm} - ${java:os} - ${java:hw}${ctx:newline}</header>
                            <pattern>%d{yyyy-MMM-dd HH:mm:ss.SSS} %-6p [%t] %C{2} %M
                            <SizeBasedTriggeringPolicy size="1 MB"/>
                        <DefaultRolloverStrategy min="1" max="3" fileindex="max" />
        <Async name="AsyncRollingRandomFile" includeLocation="true">
            <AppenderRef ref="Routing"/>
        <Root level="error" includeLocation="true">
            <AppenderRef ref="AsyncRollingRandomFile"/>

-----Original Message-----
From: Remko Popma [mailto:remko.popma@gmail.com] 
Sent: Saturday, January 17, 2015 3:53 PM
To: Log4J Users List
Subject: Re: Observations about the Context Map and the Header attribute

On Saturday, January 17, 2015, Blaine Bergeson (bbergeson) < bbergeson@micron.com> wrote:

> Here are some things I have observed when trying to use the context 
> map and the header attribute for a pattern.  Are these bugs or are 
> they suppose to work this way?
>   1.   When I try to use the context map values in the header attribute of
> a patternLayout of a RollingRandomAccessFile appender the values are 
> not available the first time the log file is created but they do 
> become available when the log is created as part of a rollover event.  
> It seems that a rollover must occur before the context values are 
> available to the header attribute.

Yes, if you refer to a log4j2 class in your app, and this class isn't loaded yet, log4j will
initialize itself when that class is loaded. During initialization all appenders are created.
RandomAccessFileAppender creates the file and writes the first header when it is initialized.
This means that the first header has been output before your app has had a chance to add things
to the context map.
If you want to change this, please raise a Jira and ideally provide a patch. (Sounds like
a tricky one though...)

> Also, if I make the logger and/or appender async then the values also 
> don’t seem to be available even though I have the location attribute 
> set to true on the async logger and the async ref appender.

Sorry, not sure what you mean by "the values". The includeLocation attribute is a signal to
the async logger/appender that a layout is configured (like PatternLayout with %location or
%method) that needs a snapshot of the caller stack. This is not related to context map values.

>   2.  If I add items to the context map from within a specific class 
> those values will not be available to the Root logger.  I have to 
> define at least
> 1 logger that is defined with a portion of the path to the class where 
> the items were added.  For example if I have a class called 
> com.sample.MyClass then any items I add to the context map will be 
> available to a logger defined as at least com, but the Root Logger and 
> any appender used by the Root logger will not see them.

This sounds like a bug. Can you raise a Jira for this with the steps (and ideally demo app/config)
to reproduce the issue?

>   3.  It appears that the header attribute value can’t use any of the 
> formatting or substitution features, i.e. it has to be an exact string.
> This means that to enter the date, time, or just a  newline I have to 
> add it to the end of the string that is entered in the context map in 
> the application rather than just adding it in the configuration file.

As far as I know the header supports Lookups, or is that not what you mean?
(http://logging.apache.org/log4j/log4j-2.1/manual/lookups.html )

>   4.  When the appender is stdout it won’t display the header 
> attribute value.

Oops. Can you raise a Jira for this?


> Blaine
> Sent from my iPhone
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> <javascript:;>
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> <javascript:;>
View raw message