See the RegexReplacementConverter. It does something similar.

Ralph


> On May 18, 2015, at 9:10 AM, Chathura Priyankara <priyankara...@gmail.com> 
> wrote:
> 
> Hi Ralph,
> 
> Thanks for the response.
> It is conditionally including that pattern. (This application uses log4j via 
> commons API so that I cannot use thread context maps)
> Using a converter for P how could I get the part within the curly braces and 
> convert it ? 
> Does the pattern parser comes inside the Converter ?
> 
> On Mon, May 18, 2015 at 9:15 PM, Ralph Goers <ralph.go...@dslextreme.com 
> <mailto:ralph.go...@dslextreme.com>> wrote:
> I am a bit confused when you say if the conversion type is %P then the log 
> information of CustomPattern will print. Are you saying it would be as if the 
> pattern was
> 
> [%d] %U%@%D[%T]%5p {%c} - %x %m%n
> 
> ?
> 
> If so, why isn’t the pattern just set to that?  If you mean you want to 
> conditionally include that pattern than you would want to do something like:
> 
> [%d] %P{%U%@%D[%T]}%5p {%c} - %x %m%n
> 
> The pattern converter for %P should be passed the pattern within the curly 
> braces and would then need to create its own PatternParser instance for that 
> pattern and then call the formatters in the format method. 
> 
> Ralph
> 
> 
> 
>> On May 18, 2015, at 7:40 AM, Chathura Priyankara <priyankara...@gmail.com 
>> <mailto:priyankara...@gmail.com>> wrote:
>> 
>> 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