http://fw-geekycoder.blogspot.kr/2010/07/creating-log4j-custom-patternlayout.html


Creating Log4J Custom PatternLayout

Suppose we need to add a username information into our logging, with Log4J this can be implemented easily. Log4J allows us to create our own PatternLayout.

PatternLayout has a method createPatternParser() that will basically delegate the task to PatternParser. Here, we can create our own MyPatternLayout by extending it from PatternLayout and delegating the task to our own PatternParser as shown below.

MyPatternLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
package myproject;
 
import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.PatternParser;
 
public class MyPatternLayout extends PatternLayout {
 
    @Override
    protected PatternParser createPatternParser(String pattern) {
        return new MyPatternParser(pattern);
    }
}

Since we normally just want to add a new character in the pattern instead of redefining all the characters, we need to make sure that we call super.finalizeConverter(c) as shown below.

PatternParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package myproject;
 
import org.apache.log4j.helpers.PatternParser;
 
public class MyPatternParser extends PatternParser {
    private static final char USERNAME_CHAR = 'u';
     
    public MyPatternParser(String pattern) {
        super(pattern);
    }
     
    @Override
    protected void finalizeConverter(char c) {
        switch (c) {
        case USERNAME_CHAR:
            currentLiteral.setLength(0);
            addConverter(new MyPatternConverter());
            break;
        default:
            super.finalizeConverter(c);
        }
    }
}

MyPatternConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package myproject;
 
import org.apache.log4j.helpers.PatternConverter;
import org.apache.log4j.spi.LoggingEvent;
 
public class MyPatternConverter extends PatternConverter {
 
    @Override
    protected String convert(LoggingEvent evt) {
        // For simplicity, assume this information is retrieved from somewhere.
        return "User1";
    }
}

Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package myproject;
 
import org.apache.log4j.Logger;
 
public class Main {
 
    private static final Logger logger = Logger.getLogger(Main.class);
     
    public static void main(String[] args) {
        logger.info("Hello World");
        logger.debug("Hello World");
        logger.warn("Hello World");
        logger.error("Hello World");
    }
}


Make sure to change the layout to our own custom PatternLayout in the log4j.properties.
log4j.properties
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender

log4j.appender.A1.layout=myproject.MyPatternLayout 
log4j.appender.A1.layout.ConversionPattern=[%-5p] <%u> [%c.%t] - %m%n


'컴퓨팅' 카테고리의 다른 글

[MAC] VMWare Fusion 6에서 Static IP 설정하기  (200) 2014.09.16
Java Log4j Custom PatternLayout  (0) 2013.06.14
모든 것을 문서화 하라  (0) 2012.02.15
Posted by 지누스

댓글을 달아 주세요



티스토리 툴바