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 bu...@apache.org on 2007/09/16 13:00:53 UTC

DO NOT REPLY [Bug 43403] New: - PatternLayout: new format modifer: prefix if non-empty

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43403>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=43403

           Summary: PatternLayout: new format modifer: prefix if non-empty
           Product: Log4j
           Version: 1.2
          Platform: Other
        OS/Version: other
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Layout
        AssignedTo: log4j-dev@logging.apache.org
        ReportedBy: yozh@mx1.ru


PatternLayout needs new format modifier: prefix to be used when parameters evaluates to the non-
empty string.

So pattern could be written as:

[%c%{x;prefix="-"}] %m

that outputs

[app.Something-7777] message

if NDC is 7777, and

[app.Something] message

if NDC is empty (no chars before square bracket).

This is must have thing for MDC: when several should be printed, and some of them could be unset:

[%c${X;key=clientId;prefix=" clientId="}%{X;key=city;prefix=" city="}] %m

should be evaluated to

[app.something clientId=777 city=Moscow] message
[app.something clientId=888] message
[app.something city=Moscow] message
[app.something] message

depending on what values are set in MDC.

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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


DO NOT REPLY [Bug 43403] - PatternLayout: new format modifer: prefix if non-empty

Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43403>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=43403





------- Additional Comments From carnold@apache.org  2007-09-16 11:34 -------
I think the use case is reasonable, but I don't think the proposed solution is desirable.  First, the 
proposed solution isn't general enough. if there is a "prefix" why shouldn't there be a "suffix" too.  If it 
is desirable for NDC and MDC, why not for other fields as well. Second, implementing the solution 
would require hacking the pattern parser to support a new language feature.

ExtendedPatternLayout (the log4j 1.3 pattern layout backported for use with log4j 1.2 and provided in 
the extras companion) does support just %X which will print out all MDC values as key=value.

XSLTLayout (also in the extras) companion should also be capable of achieving your desired layout 
goals.  I haven't benchmarked XSLTLayout, but I would assume that it would have to be substantially 
slower than PatternLayout for equivalent tasks.  But it is hugely flexible and the performance different 
may not be an issue in your application.

If that isn't desirable, then possibly adding a new "replace" pattern to ExtendedPatternLayout in apache-
log4j-extras would be a reasonable approach that would not require hacking the pattern parser, just 
adding a new pattern specifier.  replace could look like:

%replace{layout pattern}{regex}{replacement}

So your NDC prefix would look like:

%c%replace{%x}{(.+)}{-$1}



-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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