我在部署在JBoss AS 7.1.1.Final上的Spring 3.1.1.RELEASE應用程序中使用了log4j 1.2.15。我試圖將寫入log4j的輸出路由到我的響應輸出流。我已經輸出這樣寫的如何將log4j輸出重定向到我的HttpServletResponse輸出流?
private static final Logger LOG = Logger.getLogger(TrainingSessionServiceImpl.class);
…
LOG.info("Creating/updating training session associated with order #:" + order.getId());
,我想它的路線,像這樣我的輸出流...
@RequestMapping(value = "/refreshPd", method = RequestMethod.GET)
public void refreshPD(final HttpServletResponse response) throws IOException
{
...
final WriterAppender appender = new WriterAppender(new PatternLayout("%d{ISO8601} %p - %m%n"),response.getWriter());
appender.setName("CONSOLE_APPENDER");
appender.setThreshold(org.apache.log4j.Level.DEBUG);
Logger.getRootLogger().addAppender(appender);
worker.work();
Logger.getRootLogger().removeAppender("CONSOLE_APPENDER");
,但遺憾的是,沒有什麼是讓輸出到我的瀏覽器,儘管我知道(通過調試)日誌語句被調用。有誰知道我可以如何調整我的設置以使其工作?以下是我的log4j.properties文件,部署到我的wAR的WEB-INF/classes目錄中。
log4j.rootLogger=DEBUG, CA, FA
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=/usr/java/jboss/server/default/log/log4j.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = DEBUG
謝謝 - 戴夫
'worker.work()'運行在相同的還是其他線程中?問題表明它在另一個線程中運行。 – BalusC
是的,它運行在同一個線程中。 – Dave
在Tomcat上適合我。經過一番調查,它看起來像一個JBoss AS特定問題:http://stackoverflow.com/questions/6071809/adding-log4j-appenders-programmatically,隨後https://issues.jboss.org/browse/JBAS-9318 – BalusC