tags:

views:

44

answers:

2

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 then 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?

A: 

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.

Bozho
+1  A: 

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.

Kevin D