Despite all these, many project owners still want to try write their own logger service! I wondered if I were to ask and write one myself, what would it be like? So I played around and come up with this simple facade that wraps one of the logger provider (JDK logger in this case), and you can check it out here. With my logger, you can use it like this in your application:
import zemian.service.logging.*;
class MyService {
static Log LOG = LogFactory.createLog(MyService.class);
public void run() {
LOG.info(Message.msg("%s service is running now.", this));
}
}
Or you can use the Logger wrapper to avoid many imports:
import zemian.service.logging.Logger;
class MyService2 {
static Logger LOGGER = new Logger(MyService2.class);
public void run() {
LOGGER.info("%s service is running now.", this);
}
}
Some principles I followed when trying this out:
- Use simple names for different level of messages: error, warn, info, debug and trace (no crazy fine, finer and finest level names.)
- Seperate Log service from implementation so you can swap provider.
- Uses Message logging POJO as data encapsulation. It simplifies the log service interface.
- Use log parameters and lazy format binding to construct log message to speed performance.
Here are some general rules about using logger in your application that I recommend:
- Use ERROR log messages when there is reallyl a error! Try not to log an "acceptable" error message in your application. Treat an ERROR as critical problem in your application, like if it's in production, some one should be paged to take care of the problem immediately. Each message should have a full Java stacktrace! Some application might want to assign a unique Error Code to these level of messages for easier identification and troubleshoot purpose.
- Use WARN log messages if it's a problem that's ignorable during production operation, but not good idea to supress it. Likely these might point to potentially problem in your application or env. Each message should have a full Java stacktrace, if available that is!
- Use INFO log messages for admin operators or application monitors peoples to see how your application is doing. High level application status or some important and meaningful business information indicators etc. Do not litter your log with developer's messages and unessary verbose and unclear message. Each message should be written in clear sentence so operators knows it's meaningful.
- Use DEBUG log messages for developers to see and troubleshoot the application. Use this for critical application junction and operation to show objects and services states etc. Try not to add repeated loop info messages here and litter your log content.
- Use TRACE log message for developers to troubleshoot tight for loop and high traffic messages information.
- You should select a logger provider that let you configure and turn these logging levels ON or OFF (preferrable at runtime if possible as well). Each level should able to automatically suppress all levels below it. And ofcourse you want a logger provider that can handle log message output to STDOUT and/or to FILE as destination as well.
No comments:
Post a Comment