How to use AsyncAppender in log4j in order to write log message to the web service? Should I create my own Appender which would extend AsyncAppender or just attach custom appenders to the AsyncAppender? If the second choice is correct, where should I take the AsyncAppender object? Is there any example?
相关问题
- convert logback.xml to log4j.properties
- logging static methods in a parent class
- Wrap log4j or create custom logger?
- ClassNotFoundException found in Log4j 2.0
- Undeploying a Grails App from Glassfish gets a Cla
相关文章
- Can IntelliJ create hyperlinks to the source code
- how can I disable output to log4j.rootLogger?
- Log4j2 using {} against using %d or %s
- How do I redirect a javaw.exe console output to a
- log4j: Standard way to prevent repetitive log mess
- Log4j2 not working in WebLogic 12.2.1
- Grails and Log4J : How to logs in different files
- Managing a Large Number of Log Files Distributed O
We wanted to use log4j.AsyncAppender but couldn't find any setter methods to attach other appenders at the log4j.property file. So we extended the log4j.AsyncAppender class and added a setter to add other appenders. This has helped the main program thread to be independent of log4j's logging operation. The details below.
log4j entry:
Define a logger 'com.noPath' with log4j File Appender, 'fileAppender'. Note that the logger path is immaterial and hence the name 'com.noPath'
Define the logger that business needs, 'com.business', Attach the File appender of previous step, 'fileAppender', to the log4j AsyncAppender through a custom class com.log.AsyncAppenderHelper which extends log4j AsyncAppender
The java class com.log.AsyncAppenderHelper extending log4j.AsyncAppender, have it available in the class path.
Answering my own question.
In the log4j config file(usually it's log4j.xml or log4j.properties) we should define AsyncAppender which would refer to a real appender(it may be our own defined class like in my case).
So I wrote a WebServiceAppender class which extends AppenderSkeleton and implements 3 abstract methods. The main method is "append" which connects to the web service and sends all the information to it. That's it.
Add AsyncAppender in log4j config file which will refer to a real appender. For demo: adding asyncappender to console appender in log4j.xml