sitelink1 | |
---|---|
sitelink2 | |
sitelink3 | http://1 |
extra_vars4 | ko |
extra_vars5 | http://www.javaworld.com/javaworld/jw-12-2004/jw-1220-toolbox.html?page=3 |
extra_vars6 | sitelink1 |
- log4j.appender.REMOTE=com.holub.log4j.RemoteAppender
- log4j.appender.REMOTE.Port=1234
- log4j.appender.REMOTE.layout=org.apache.log4j.PatternLayout
- log4j.appender.REMOTE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
Using a remote appender
One of log4j's major strengths is that the tool is easy to extend. My RemoteAppender
SocketAppender
Log4j also comes with an elaborate standalone GUI called Chainsaw that you can use to view messages from a SocketAppender
Listing 3 shows a simple viewer application for my RemoteAppender
1234
) is passed to the server via the configuration file in Listing 2. Here's the relevant line:
- log4j.appender.REMOTE.Port=1234
java Client
Listing 3. Client.java: A client for viewing logging messages
Note, by the way, that the client in Listing 3 is a great example of when not to use Java's NIO (new input/output) classes. There's no need for asynchronous reading here, and NIO would complicate the application considerably.
The remote appender
All that's left is the appender itself, which manages the server-side socket and writes the output to the clients that connect to it. (Several clients can receive logging messages from the same appender simultaneously.) The code is in Listing 4.
Starting with the basic structure, the RemoteAppender
AppenderSkeleton
getXxx()
setXxx()
Xxx
Port
Note that both the getter and setter methods are private
getPort()
setPort()
private
private
The second order of business is to override a few methods from the AppenderSkeleton
superclass.
After log4j has parsed the configuration file and called any associated setters, the activateOptions()
activeOptions()
activateOptions()
accept()
accept()
close()
listenerSocket
SocketException
Once the connection is established, the thread wraps the output stream for the client socket in a Writer
Writer
Collection
clients
Writer
Collection
The synchronization is worth mentioning. I put objects into clients
The code that does the actual appending is in the append()
Writer
That iteration is tricky. I could have wrapped the clients Collection
Collections.synchronizedCollection()
add()
Since clients aren't added often, I've solved the problem simply by locking the clients Collection
The only appender method that remains is an override of close()
listenerSocket
accept()
Listing 4. RemoteAppender.java: A custom log4j appender
Conclusion
That's all there is to building an appender. You override a few methods of AppenderSkeleton
and add getter/setter methods for the parameters. Most of the nastiness here is socket-and-thread related. The actual log4j code was simplicity itself (extend a class and overwrite a few methods). Writing your own appenders for things like logging to a database, for example, is equally simple.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
14 | Apache Log4j™ 2 사용하기 | 황제낙엽 | 2023.04.28 | 3 |
13 | Migrating from Log4j 1.x to 2.x | 황제낙엽 | 2023.04.28 | 0 |
12 | SLF4J 를 사용해야 하는 이유 | 황제낙엽 | 2022.09.17 | 0 |
11 | Apache Log4j 2 Configuration 파일 설정 | 황제낙엽 | 2020.04.01 | 150 |
10 | 각 레벨별 출력파일 설정 예제 | 황제낙엽 | 2018.09.18 | 147 |
9 | log4j-1.2.15.jar 와 log4j.properties 예제 | 황제낙엽 | 2017.08.04 | 187 |
8 | Commons Logging과 Log4J | 황제낙엽 | 2013.03.07 | 102 |
7 | Library & Properties 파일 | 황제낙엽 | 2011.12.23 | 313 |
6 | WebSphere 에서 Log4j 사용하기 | 황제낙엽 | 2011.04.15 | 52 |
5 | 접속 클라이언트의 아이피별로 로그 화일 기록하기 | 황제낙엽 | 2009.06.01 | 183 |
4 | 영문 메뉴얼 (PDF) | 황제낙엽 | 2009.05.29 | 19 |
» | 사용자 정의 Appender 정의하여 Log4j 확장하기 | 황제낙엽 | 2009.05.28 | 220 |
2 | log4j에서 여러파일에 로그남기기 | 황제낙엽 | 2007.09.04 | 16 |
1 | Log4j 웹에서 사용하기 | 황제낙엽 | 2007.05.13 | 43 |