If you build your own custom pattern layout, which it doesn’t sound like you 
need to do, your configuration would look like:

<Console name=“Console” target=“SYSTEM_OUT”>
        <CustomPatternLayout pattern=“%U%D[%T]”/>
</Console>

If you add your own pattern converters, which is what I think you really want 
to do, then you need only include the pattern characters in the pattern string.

I think I have a need for some more flexible converters myself:

1. A converter that can take a boolean expression to test and, if true, will 
include the result of the associated pattern in the output. For example 
%test{$${mdc:userid} != null:$X{userid}}
2. Enhancement to %X to specify a list of keys instead of a single key - 
%X{key1, key2, key3: includeKey=true}. If key1 and key2 were present but not 
key 3 the result would be {key1=a, key2=b}. If none of the keys were present 
the result would be {}.

Ralph

> On May 18, 2015, at 8:30 AM, Chathura Priyankara <priyankara...@gmail.com> 
> wrote:
> 
> Hi Gary,
> 
> Thanks for the prompt response.
> (For my purpose cannot use Thread context maps because these log information 
> are highly user configurable)
> Could you please tell me one thing. If I have custom pattern layout plugin 
> eg: (plugin name = CustomPatternLayout).
> How would I configure this for a particular appender using log4j2.xml?
> 
> Eg: 
> 
> <Console name="CONSOLE" target="SYSTEM_OUT">
>       <PatternLayout pattern="%U%D[%T]"/>
> </Console>
> 
> Here how can I configure console appender to use my CustomPatternLayout ?
> 
> Thanks.
> 
> 
> On Mon, May 18, 2015 at 8:46 PM, Gary Gregory <garydgreg...@gmail.com 
> <mailto:garydgreg...@gmail.com>> wrote:
> Can you achieve the same result with Thread context perhap? 
> 
> Gary 
> 
> 
> -------- Original message --------
> From: Chathura Priyankara <priyankara...@gmail.com 
> <mailto:priyankara...@gmail.com>> 
> Date: 05/18/2015 07:40 (GMT-08:00) 
> To: Log4J Developers List <log4j-dev@logging.apache.org 
> <mailto:log4j-dev@logging.apache.org>> 
> Subject: Re: [Dev][log4j2] How to create custom pattern layout in log4j2 
> 
> Hi Ralph,
> 
> There are some custom information that I need to log. Please see the 
> following log4j properties.
> log4j.appender.CONSOLE.layout=com.test.custom.PatternLayoutClass
> log4j.appender.CONSOLE.layout.ConversionPattern=[%d] %P%5p {%c} - %x %m%n
> log4j.appender.CONSOLE.layout.CustomPattern=%U%@%D[%T]
> 
> Here some custom Conversion characters such as U, D etc. are used. If the 
> conversion type is %P 
> for ConversionPattern then the log information of CustomPattern will print.
> Can you please tell me how could I do the same thing with log4j2 ?
> 
> Thank you very much for the reply.
> 
> 
> On Mon, May 18, 2015 at 6:25 PM, Ralph Goers <ralph.go...@dslextreme.com 
> <mailto:ralph.go...@dslextreme.com>> wrote:
> You shouldn't need to do this.  What is different about your layout than what 
> comes with Log4j? If you simply have a custom pattern or 2 then you would 
> just implement those as converters.
> 
> Ralph
> 
> On May 18, 2015, at 5:41 AM, Chathura Priyankara <priyankara...@gmail.com 
> <mailto:priyankara...@gmail.com>> wrote:
> 
>> I have to convert a pattern layout written for log4j 1.2. to log4j2. 
>> Following code snippet shows a part of it. I have searched a lot about this 
>> but couldn't find a proper guide.
>> Can anyone please give me a brief guide about how to do this conversion ?
>> 
>> Any help is highly appreciated!
>> 
>> public class CustomPatternLayout extends PatternLayout {
>> 
>> 
>> public static final String DEFAULT_PATTERN = "[%T][%S]";
>> 
>> private static String customPattern = DEFAULT_PATTERN;
>> 
>> 
>> public CustomPatternLayout(String pattern) {
>>     super(pattern);
>> }
>> 
>> public synchronized void setCustomPattern(String customPattern) {
>>     CustomPatternLayout.customPattern = customPattern;
>> }
>> 
>> 
>> private static class customPatternParser extends PatternParser {
>> 
>>     protected void finalizeConverter(char c) {
>>         PatternConverter pc = null;
>>         switch (c) {
>>             case 'P':
>>                 pc = new UserNamePatternConverter(formattingInfo, 
>> extractPrecisionOption());
>>                 break;
>>                 .
>>                 .
>>                 .
>>             default:
>>                 super.finalizeConverter(c);
>>         }
>>         if (pc != null) {
>>             currentLiteral.setLength(0);
>>             addConverter(pc);
>>         }
>>     }
>> 
>> private abstract static class CustomNamedPatternConverter extends 
>> PatternConverter {
>> 
>>         private int precision;
>> 
>>         public CustomNamedPatternConverter(FormattingInfo formattingInfo, 
>> int precision) {
>>             super(formattingInfo);
>>             this.precision = precision;
>>         }
>> 
>>         protected abstract String getFullyQualifiedName(LoggingEvent event);
>> 
>>         public String convert(LoggingEvent event) {
>>             String n = getFullyQualifiedName(event);
>>             if (n == null) {
>>                 return "";
>>             }
>>             if (precision <= 0) {
>>                 return n;
>>             } else {
>>                 int len = n.length();
>>                 int end = len - 1;
>>                 for (int i = precision; i > 0; i--) {
>>                     end = n.lastIndexOf('.', end - 1);
>>                     if (end == -1) {
>>                         return n;
>>                     }
>>                 }
>>                 return n.substring(end + 1, len);
>>             }
>>         }
>>     }
>> 
>>     private static class MyPatternConverter extends 
>> CustomNamedPatternConverter {
>> 
>>         public MyPatternConverter(FormattingInfo formattingInfo, int 
>> precision) {
>>             super(formattingInfo, precision);
>>         }
>> 
>>         public String getFullyQualifiedName(LoggingEvent event) {
>>             int userId = //Get user Id
>>             if ( . . .) {
>>                 return new CustomPatternLayout(customPattern).format(event);
>>             }
>>             return someOther;
>>         }
>>     }
>> }
>> }
>> Thanks in advance.
>> 
>> -- 
>> Chathura Priyankara,
>> Faculty of Information Technology,
>> University of Moratuwa.
>> Blog  : www.codeoncloud.blogspot.com <http://www.codeoncloud.blogspot.com/>
> 
> 
> 
> -- 
> Chathura Priyankara,
> Faculty of Information Technology,
> University of Moratuwa.
> Blog  : www.codeoncloud.blogspot.com <http://www.codeoncloud.blogspot.com/>
> 
> 
> 
> -- 
> Chathura Priyankara,
> Faculty of Information Technology,
> University of Moratuwa.
> Blog  : www.codeoncloud.blogspot.com <http://www.codeoncloud.blogspot.com/>

Reply via email to