2012年1月13日星期五

java logging



  link
  日志操作会影响程序性能,在一些电子商务和大规模低延时的股票交易系统中使用日志必须得很谨慎.
1.为什么需要记录日志?
  System.out.println()输出日志没有Log4j或java.util.logging强大,比如你先记录日志到文件中,sysout对此无能为力。在java服务应用中,记录日志文件是必选项。
2.不同的日志级别是啥?
  基本的级别有:DEBUG, INFO, WARN, ERROR.
DEBUG is the lowest restricted java logging level and we should write everything we need to debug an application, this java logging mode should only be used on Development and Testing environment and must not be used in production environment.


INFO is more restricted than DEBUG java logging level and we should log messages which are informative purpose like Server has been started, Incoming messages, outgoing messages etc inINFO level logging in java.


WARN is more restricted than INFO java logging level and used to log warning sort of messages e.g. Connection lost between client and server. Database connection lost, Socket reaching to its limit. These messages and java logging level are almost important because you can setup alert on these logging messages in java and let your support team monitor health of your java application and react on this warning messages. In Summary WARN level is used to log warning message for logging in Java.


ERROR is the more restricted java logging level than WARN andused to log Errors and Exception, you can also setup alert on this java logging level and alert monitoring team to react on this messages. ERROR is serious for logging in Java and you should always print it.


FATAL java logging level designates very severe error events that will presumably lead the application to abort. After this mostly your application crashes and stopped.


OFF java logging level has the highest possible rank and is intended to turn off logging in Java.


These java logging levels are based on log4j logging level and little bit different than java.util.logging API which provides some more logging level like SEVERE, FINER, FINEST, FATAL etc as name suggest based upon criticality of your logging message you can choose any of this level for logging in Java.
3.用log4j或java.util.logging API记录日志
  log4j优于java.util.logging,因为log4j更易于掌握和使用,此外log4j的日志级别定义清晰可以动态修改而不用重启应用,log4j还是线程安全的。
4.日志为什么影响性能
  日志操作越多记录日志文件IO越多,这会拖慢系统速度。这也是为什么对每条信息选择合适的日志级别如此重要。在isDebugEnabled记录debug信息

if(logger.isDebugEnabled()){
logger.debug("java logging level is DEBUG Enabled");
}

在生产环境中使用WARN ERROR FINER FINEST等级别,永远不要用DEBUG
5.logging十个技巧
1) Use isDebugEnabled() for putting debug log in Java , it willsave lot of string concatenation activity if your code run in production environment with production logging level instead of DEBUG logging level.
DEBUG日志前,用isDebugEnabled()判断
2) Carefully choose which kind of message should go to which level for logging in Java, It become extremely important if you are writing server application in core java and only way to see what happing is java logs. If you log too much information your performance will be affected and same time if you don't log important information like incoming messages and outgoing messages in java logs then it would become extremely difficult to identify what happened in case of any issue or error because nothing would be in java logs.
记录日志的量要适当,将输入、输出信息记录
3) Use either log4j or java.util.logging for logging in Java, I would recommend log4j because I have used it a lot and found it very flexible. It allows changing logging level in java without restarting your application which is very important in production or controlled environment. To do this you can have log4jwatchdog which continuously look for log4j.xml in a particular directory and if founds loads it and reset logging in java.
用log4j记录日志
4) By using log4j.xml you can have different logger configuration for different java classes as well. You can have some classes in INFO mode, some in WARN mode or ERROR mode. It’s quite flexible to do this to customize java logging.
用log4j.xml文件,你可以针对不同的class定义不同的配置
5) Another important point to remember is format of java logging, this you specify in logger. Properties file in case of java.util.logging API for logging to use which java logging Formatter. Don’t forget to include Thread Name and fully qualified java class Name while printing logs because it would be impossible to find sequence of events if your code is executed by multiple threads without having thread name on it. In my opinion this is the most important tips you consider for logging in Java.
配置日志输出时不要完了输出线程名和类的全称
6) By carefully choosing format of  java logging at logger level and format of writing log you can have generate reports from your java log files. Be consistent while logging messages, be informative while logging message, print data with message whereever required.

7)while writing message for logging in Java try to use some kind of prefix to indicate which part of your code is printing log e.g. client side , Database site or session side, later you can use this prefix to do a "grep" or "find" in Unix and have related logs at once place. Believe me I have used this technique and it helped a lot while debugging or investigating any issues and your log file is quite large. For example you can put all Database level log with a prefix "DB_LOG:" and put all session level log with prefix "SESSION_LOG:” 
在输出日志时用些前缀记录特定的信息,方便需要时grep查找
8) If a given logger is not assigned a level, then it inherits one from its closest ancestor. That’s why we always assign log level to root logger in configuration file log4j.rootLogger=DEBUG.

9) Both no logging and excessive logging is bad so carefully planned what to log and on which level you log that messages so that you can run fast in production environment and at same time able to identify any issue in QA and TEST environment.
无日志和过多日志都是不好的做法
10) I found that for improving logging its important you look through your log and monitor your log by yourself and tune it wherever necessary. It’s also important to log in simple English and it should make sense and human readable since support team may wan to put alert on some logging message and they may want to monitory your application in production.

11) if you are using SLF4J for logging in java use parametrized version of various log methods they are faster as compared to normal method. 

logger.debug("No of Orders " + noOfOrder +  
            " for client : " + client); // slower 
logger.debug("No of Executions {} for clients:{}"
            , noOfOrder , client); // faster

These tips and examples on logging in java is based on my experience and how I use logging in Java and by no means complete, I would love to hear some more tips from you guys and how you guys are using and customizing java logging. I would recommend reading detailed and official documentation for both java.util.logging and log4j to get complete and detailed information on java logging as well. 

nowdays slf4j+logback would be a better solution

没有评论:

发表评论