1

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?

1
  • we ended up going a different route. in the project all of our exceptions extend from a particular class, which has the info we need. since this is mostly for logging exceptions, we have a custom formatter that checks the exception type, and extracts the info if it's there. Commented Oct 8, 2010 at 12:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.