1. Select the appropriate log level
2. Output clear hint text and adequate field information
3. Output content line up, do not wrap
4. Other
1. Select the appropriate log level
You need to follow some common specifications when choosing a log level, and you cannot define it arbitrarily
Log4j log level, from low to High: All trace Debug info warn error fatal off
Where all off is used only to turn all logs on or off in the log4j configuration file, trace fatal generally does not
For developers, just focus on debug info warn error
Debug
Normally do not need output, only when the problem of the output of the log information, because the production environment can not be single-step debugging, the debug level of the log could be imagined as you in the build Environment debug
Info
You may want to focus on or only the more important information needed to output, such as: User login, exit, background job execution time, etc.
Warn
There are some potential dangers when the output logs, such as: The request parameter contains the attack injection script
Error
Example: SQL exception when requesting a database
Of course, the best way is to refer to good open source code
2. Output clear hint text and adequate field information
Points:
1) Clear hint text, see this paragraph hint text can know what happened, do not need to go to grilled La Yuan code
2) Adequate field information, such as: User information, the parameter value that throws the exception, exception stack information, etc.
Example:
Log.warn ("Unknown value for includeparams parameter to URL tag:" + includeparams);
Log.warn ("Unable to put request parameters (" + extractquerystring () + ") into parameter map.", e);
Log.warn ("Could not find token mapped to token name" + tokenname);
3. A log line is done
This is to make it easier to track and analyze logs, and not only to see part of a log when using the grep command
4. Other
4.1. Try to use a set of log interface, highly recommended SLF4J
Two main reasons:
1) Use the {} placeholder to avoid string concatenation
Take just three logs for example
Log.warn ("Unknown value for includeparams parameter to URL tag:" + includeparams);
Log.warn ("Unable to put request parameters (" + extractquerystring () + ") into parameter map.", e);
Log.warn ("Could not find token mapped to token name" + tokenname);
If you use SLF4J, the following is the wording:
Log.warn ("Unknown value for includeparams parameter to URL tag: {}", includeparams);
Log.warn ("Unable to put request parameters ({}) into parameter map.", Extractquerystring (), E);
Log.warn ("Could not find token mapped to token name {}", tokenname);
2) Force check Log to open before performing actual log output
When combined with log4j+slf4j, the Warn method that is executed is actually the case:
public void warn (String format, Object Arg) {
if (Logger.isenabledfor (Level.warn)) {
Formattingtuple ft = messageformatter.format (format, ARG);
Logger.log (FQCN, Level.warn, Ft.getmessage (), ft.getthrowable ());
}
}
4.2. Do not use SYSTEM.OUT.PRINTLN ()
This is not much to say, the log saw a inexplicable hello,world you will think, how to check???
4.3. Do not use E.printstacktrace ()
This type of printing can only be output to catalina.out, unable to separate the output destination file, and can cause log output confusion
4.4.SLF4J Printing exception stack information
Two examples:
Try { if (true) { thrownew runtimeexception ("I ' m OK" ); Catch (Exception e) { log.error ("error. param:{}, param2:{}, param3:{}", param, param2, param3, E);}
Will print:
Error 2015-01-17 15:11:51,426 error. param:0, Param2:2, Param3:false [Cn.xxt.log.test.Slf4jTest.main (slf4jtest.java:36)]
Java.lang.runtimeexception:i ' m OK
At Cn.xxt.log.test.Slf4jTest.main (slf4jtest.java:33)
Try { if (true) { thrownew runtimeexception ("I ' m sorry" ); Catch (Exception e) { log.error ("error. param:{}, param2:{}, param3:{}, {}", param, param2, param3, E);}
Will print:
Error 2015-01-17 15:11:51,429 error. param:0, Param2:2, Param3:false, java.lang.runtimeexception:i ' m Sorry [Cn.xxt.log.test.Slf4jTest.main ( SLF4JTEST.JAVA:44)]
Difference: The former outputs the exception stack information, which does not
Cause: The latter uses the {} placeholder to print the exception object E, which causes the exception stack information to not be output
Reference documents
Why use SLF4J instead of log4j
Http://www.importnew.com/7450.html
Java Log design & Practice (3)-Development