开发者

Logging additional information with java.util.logging.Logger

On my team we have a logging standard, and for the most part the

Logger.log(Level, String, [Throwable]) 

methods works fine (it gives us automatic timestamp, class name, method name, message, and throwable information in the log). However, we have an additional identifier we log, and it's part of our standard (we use it to help monitor the logs).

My problem is that I want to be able to easily enforce this standard in a fairly painless way. At first we created a wrapper logger class, but the problem is you lose the benefit of Logger knowing which method you are in.

void myLoggerMethod(Level level, String msg, String identifier) { 
    logger.log(level, identifier + " " + msg); 
}

will log things mostly correct, but the开发者_运维知识库n it logs the wrong method name. You can use logp to get around this, but then you have to send in the method name as a String and it becomes pretty gross.

Is there a good way to allow people to enter an additional piece of data to log? Is there any extension mechanism that allows this, or something in the API i'm missing?


I don't fully understand your usecase, but you can try to implement a custom Formatter (or Handler) Or you can extend an existing one, like SimpleFormatter / FileHandler.


You could look at it another way and set up code templates in your IDE.

For instance I have my IDE set up to change logdebug, logerror, loginfo to code snippets for my logging.

In my IDE, I type logdebug, hit ctrl+space and it converts it to

if(logger.isDebugEnabled()){
logger.debug("xxx");}

I can then just fill in the message I want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜