| 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 RemoteAppenderSocketAppender
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 RemoteAppender1234) is passed to the server via the configuration file in Listing 2. Here's the relevant line:
- log4j.appender.REMOTE.Port=1234
java ClientListing 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 RemoteAppenderAppenderSkeletongetXxx()setXxx()XxxPort
Note that both the getter and setter methods are privategetPort()setPort()privateprivate
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()listenerSocketSocketException
Once the connection is established, the thread wraps the output stream for the client socket in a WriterWriterCollectionclientsWriterCollection
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 CollectionCollections.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()listenerSocketaccept()
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
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 58 | 톰캣에서 로그 파일 저장시 ansi 로 저장되는 현상 | 황제낙엽 | 2024.10.21 | 665 |
| 57 |
Jsoup 을 이용하여 웹 크롤링(Web Crawling)
| 황제낙엽 | 2023.08.09 | 496 |
| 56 | Apache Log4j™ 2 사용하기 | 황제낙엽 | 2023.04.28 | 594 |
| 55 | Migrating from Log4j 1.x to 2.x | 황제낙엽 | 2023.04.28 | 718 |
| 54 |
SLF4J 를 사용해야 하는 이유
| 황제낙엽 | 2022.09.17 | 584 |
| 53 | Apache Log4j 2 Configuration 파일 설정 | 황제낙엽 | 2020.04.01 | 818 |
| 52 |
이클립스에서 JUnit 사용하기
| 황제낙엽 | 2019.04.02 | 728 |
| 51 | 각 레벨별 출력파일 설정 예제 | 황제낙엽 | 2018.09.18 | 775 |
| 50 |
log4j-1.2.15.jar 와 log4j.properties 예제
| 황제낙엽 | 2017.08.04 | 840 |
| 49 |
POI HSSF, XSSF, SXSSF 성능 분석
| 황제낙엽 | 2013.11.05 | 2097 |
| 48 | POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files | 황제낙엽 | 2013.11.05 | 1775 |
| 47 | Commons Logging과 Log4J | 황제낙엽 | 2013.03.07 | 733 |
| 46 |
Library & Properties 파일
| 황제낙엽 | 2011.12.23 | 981 |
| 45 |
Cell 의 wrap 설정 (텍스트 개행)
| 황제낙엽 | 2011.05.09 | 3610 |
| 44 |
XSSF Examples
| 황제낙엽 | 2011.05.04 | 816 |
| 43 | 병합된 셀의 스타일( border) 설정하기 | 황제낙엽 | 2011.05.03 | 2214 |
| 42 | 셀 크기 조정 (자동 크기 조정) | 황제낙엽 | 2011.05.03 | 8316 |
| 41 | 셀 병합 | 황제낙엽 | 2011.05.03 | 697 |
| 40 | WebSphere 에서 Log4j 사용하기 | 황제낙엽 | 2011.04.15 | 632 |
| 39 |
Map 사용 예제
| 황제낙엽 | 2010.10.17 | 622 |