java log Print

Print log, should pay attention to the following 4:00.

A pre-determined level log

To DEBUG, INFO logging level, the output must be used or the conditions using a placeholder printing manner. The convention considers the operating efficiency and print the log demand program.

First look at a counter-example:

log.debug ( "input parameter information id =" + id + ", obj =" + obj);

 

If you print a configuration application WARN level in accordance with the above code prints DEBUG level log, the log will not be printed, but will perform the string concatenation operation; if obj is an object, but also the implementation of toString ( ) method, a waste of system resources.

Example code is correct as follows:

// use condition is determined in the form of 
IF (log.isDebugEnabled ()) { 
    log.debug ( "input parameter information {} id =" , ID); 
} 
// use placeholders 
log.debug ( "input parameter information id = {}, obj = {} " , id, obj);

Second, avoid invalid log Print

DEBUG log output prohibit the production environment and selectively output INFO log.
When using the INFO, WARN log level to record the behavior of business information, be sure to control the output, in order to avoid shortage of disk space, while setting a reasonable life cycle for the log files, clean the expired log.

Avoid repetitive printing, be sure to set the configuration additivity = false log file, for example:

<logger name="com.test" additivity="false">
    <level value="INFO" />
    <appender-ref ref="logfile" />
</logger>

additivity property profile:

It is whether the child inherits the parent Logger Logger output source (appender) flag, by default erupted Logger Logger will inherit the parent appender, that is sub-Logger output in the appender's parent Logger. The additivity set to false, then the sub-Logger only in their own appender in output without output appender's parent Logger.

Third, the discrimination error log

WARN, ERROR log level is related to an error, but not a print error occurs on the ERROR log, such as some business exceptions can be retried by guide will be able to restore, such as user input parameter error, in this case, the record when a user logs in order to restore the site can consult if the output level is ERROR, says the event will require human intervention, which is obviously unreasonable. So, ERROR level records only system logical errors, anomalies or violations of important business rules, other errors can be classified as WARN level.

Fourth, ensure the integrity of the contents of records

Content logging site needs to include contextual information and exception stack information, the print when needed attention to the following points:

Must abnormal output stack 1. When the recording abnormality, for example:

log.error("xxx" + e.getMessage(), e);

2. If the log output object instance, to ensure that instances of the class overrides the toString method, otherwise it will print object hashCode value has no real meaning.

 

Reprinted: https://www.cnblogs.com/haha12/p/11834088.html

Guess you like

Origin www.cnblogs.com/lwcode6/p/11839620.html