Allow environment variable to be logged via pattern
Logging Framework for C++
Brought to you by:
wilx
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.
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 theMDC
facility instead.Set up MDC key and its value:
And add
%X{env.ID}
into yourPatternLayout
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
Diff:
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.
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.
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.
I have created a wiki page related to this. If you have any more questions, please let me know.
I have committed your patch with only a minor tweak to both trunk and 1.1.x branches.