Slf4j little skill

Copyright: code word is not easy, please indicate the source ~~ https://blog.csdn.net/m0_37190495/article/details/88636139
Reproduced, please indicate the source:
Original starting in: http://www.zhangruibin.com
article from RebornChang's blog

Slf4j little skill

Speaking Web development, it is divided into many modules, which log module is an integral part. Statistical analysis of logs collected here do not speak of these, except to say that commonly used in the development of small skill points.
Tell me first ask two questions:

Print log of time, what the role of '{}' is?

Print log time, '+' and the difference between ',' what is?

When the printing log, abnormality information to print to the catch 'logger.error ( "error message:" + e.getMessage ())' and 'logger.error ( "error message:" + e)' is the difference between what?

As of 7 log settings, and configure what the meaning of this will not say, bloggers say is this section cheat cold little knowledge, following in that time.

First, that the '{}' role:

Logger.INFO () specific medium '{}'

In logger.info (), the '{}' is a placeholder, the placeholder SLF4J based on the position, and acquires data corresponding placeholder is filled Object.
Look at Slf4j on logger level source of info section:

 void info(String var1, Object var2);

 void info(String var1, Object var2, Object var3);

 void info(String var1, Object... var2);

 void info(String var1, Throwable var2);
 
 void info(Marker var1, String var2);

 void info(Marker var1, String var2, Object var3);

 void info(Marker var1, String var2, Object var3, Object var4);

 void info(Marker var1, String var2, Object... var3);

 void info(Marker var1, String var2, Throwable var3);

We can see from the above, info-level method, the first parameter of type String, the second parameter is the type of object (note that the variable parameters Object ... var2), so write directly to:

 logger.info("这里只写一段字符串"+"或者再拼接上一串字符串,是没有问题的")
 因为这时候走的是 void info(String var1, Object... var2);方法进行日志的输出打印。

If this is I have a Person object, the object is zhangsan, I want to print out this information Joe Smith, this time we can do this:

logger.info("这就是zhangsan的信息:{}",zhangsan.toString());

Note: If I want to print out this sentence zhangsan name, it should be how to write?

logger.info("这就是zhangsan的信息:{};zhangsan的名字是:{}。",zhangsan.toString(),zhangsan.getName.toString());

This time I understand it, in the info level '{}' is a placeholder because the info method which has a String parameter, the placeholder '{}' to take effect.

Then say, '+' and the difference between ',' What is

According to the method given in the figure Slf4j we can guess, '+' is for that type of stitching parameters Sting, '' is another parameter declarations and mass participation, that some people say, anyway, I want to print a log, and I have used toSting () and then use the '+' splice is not on the list?
Here relates to '{}' placeholder effect. Because the placeholders in addition to the first string is the parameter type filled in '{}' position, so that full use toString () and then using the '+' splicing, then '{}' it does not work.

Print exception information to the catch 'logger.error ( "error message:" + e.getMessage ())' and 'logger.error ( "error message:" + e)' What is the difference between

Let's first look at the inside of getMassage java.lang.Throwable use and interpretation:
`` `

/**
 * Returns the detail message string of this throwable.
 *
 * @return  the detail message string of this {@code Throwable} instance
 *          (which may be {@code null}).
 */
 
public String getMessage() {
    return detailMessage;
}
/**
 * Specific details about the Throwable.  For example, for
 * {@code FileNotFoundException}, this contains the name of
 * the file that could not be found.
 *
 * @serial
 */
private String detailMessage;

```

As an example, common null pointer exception:
e.getMessage (), is tell you that is a null pointer, stack information will not be printed, so when using logger.error () to print a log of us can do:

logger.error("异常:",e.getMessage(),e);

He said relatively plain, are small details, students may wish to pursue their own get to the bottom, welcome discussions and exchange correction.

Guess you like

Origin blog.csdn.net/m0_37190495/article/details/88636139