开发者

java.logging to String?

Hiho,

i'm trying to log errors with the help of java.logging

i want my logger to log ev开发者_开发知识库erything to a log file and to a string or something(for html output(my program is a servlet))

I haven't found something like a StringHandler. Is there a possibility to do this?

greetings


The rationale behind a logging famework is to decouple your application code from logging - it sounds like you want to re-couple the two together. The biggest issue you will probably need to overcome is that all your log messages will be consolidated together, so you will see messages from separate requests in the same file.

If you want to be able to display to the user all the messages that your servlet has logged during the lifetime of their request you'll need to add an Handler as the first thing in your servlet, remove it in a finally block and then handle the messages its accumulated.

I'm not aware of any way in which you could reliably capture all relevent logging per request, as your servlet container will be executing code before you get to the point where you can intercept it, but as that sort of logging will probably deal with errors which would prevent you ever reporting anything back to the user, its probably a non-issue.

As some of the other answers intimate, logging though java.util.logging is rather basic, hence a number of other projects which provide logging, Logback being one of the best.


I think you can use the usual logger like log4j and a StringBuilder. That should do the trick.


You could try creating your own appender called StringAppender, as a subclass of WriterAppender with a StringWriter to log to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜