Menu

#277 Allow environment variable to be logged via pattern

v1.1.2
closed
5
2013-10-13
2013-10-10
Ray Logel
No

We have encountered a need to show the value of an environment in our log message. We have a certain variable which changes based on which customer request is being processed and it would be helpful if this was something which we could turn include via our appender configuration.

The attached patch (based 1.1.1 source) adds a new pattern %E{...} which will use the given argument to lookup the associated environment variable value.

1 Attachments

Discussion

  • Václav Haisman

    Václav Haisman - 2013-10-10

    I like the idea of the change. However, I would like to see the converter for the environment variable in its own class and doing the getenv() lookup each time it is invoked. Otherwise you can be using the MDC facility instead.

    Set up MDC key and its value:

    log4cplus::getMDC ().put (LOG4CPLUS_TEXT ("env.ID"),
        LOG4CPLUS_C_STR_TO_TSTRING (std::getenv ("ID")));
    

    And add %X{env.ID} into your PatternLayout pattern.

    This feature is supported at least since 1.1.0. It appears to be undocumented, this is a documentation bug.

    The only drawback of this approach is that MDC is per-thread, you will have to set the MDC key value in each thread that needs to log it.

     

    Last edit: Václav Haisman 2013-10-10
  • Václav Haisman

    Václav Haisman - 2013-10-10
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,3 +1,3 @@
     We have encountered a need to show the value of an environment in our log message.  We have a certain variable which changes based on which customer request is being processed and it would be helpful if this was something which we could turn include via our appender configuration.
    
    -The attached patch (based 1.1.1 source) adds a new pattern %E{...} which will use the given argument to lookup the associated environment variable value.
    +The attached patch (based 1.1.1 source) adds a new pattern `%E{...}` which will use the given argument to lookup the associated environment variable value.
    
    • assigned_to: Václav Zeman
     
  • Ray Logel

    Ray Logel - 2013-10-11

    Good catch, when I tested it I was using a variable that wasn't changing. Attached is an updated patch which will log the value of the variable at log time not at pattern creation time.

     
  • Ray Logel

    Ray Logel - 2013-10-11

    I would be willing to submit the change but I wasn't able to find any documentation on what the contribution policies and procedures are.

     
    • Václav Haisman

      Václav Haisman - 2013-10-11

      What kind of contribution policies and procedures do you have in mind?

      I have never felt the need to formalize contributions processing much. There are not that many of those. But now that you mention it, I guess it is not a bad idea to summarize it somehow.

       
      • Václav Haisman

        Václav Haisman - 2013-10-13

        I have created a wiki page related to this. If you have any more questions, please let me know.

         
  • Václav Haisman

    Václav Haisman - 2013-10-13

    I have committed your patch with only a minor tweak to both trunk and 1.1.x branches.

     
  • Václav Haisman

    Václav Haisman - 2013-10-13
    • labels: --> v1.1.2, patch
    • status: open --> closed
    • Group: v1.1.x --> v1.1.2
     

Log in to post a comment.