首先说下对日志框架的感受,很多人slf4j-api slf4j-simple
logback-core logbak-classic log4j logj42很多的日志框架,控制台各种输出的时候日志框架warn error,有时候还不能输出日志。究其原因,大家都觉得日志框架不太重要,反正能输出就行了,感觉和sout也差不多,我也是这样的想法…出了问题百度下,复制两个log4j.properties ,log4j.xml就好了,下次有问题继续cv。不想这样了,我要好好学习下日志框架,希望大家也能好好学习。
官网
https://logback.qos.ch/manual/introduction.html
官网永远是学习的最好地方。
引入maven
requirements,说明这里是必备的。 他说logback-classic模块需要slf4j-api.jar 和logback-core.jar还有logback-classic.jar。那么我们需要引入这3个jar吗?
看到下面一段,说的是slf4j-api.jar其实就在logback-*jar里有。所以我们只需要引入2个jar。(我感觉slf4j-api是接口,logback是实现类)
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency>
此时通过idea查看我们具体的引入
是不是意味着我只引入logback-classic就好了?后面测试。
HelloWorld1
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld1 {public static void main(String[] args) {Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1");logger.debug("Hello world.");}
}
此时我还没配置xml,properties就可以自己打印日志了。
经过测试只有classic包也是可以的。当然你要注意版本,万一高版本和低版本可能没有core包。
看下官网文档给这个demo的说明
HelloWorld1 class is defined in the chapters.introduction package. It starts by importing the Logger and LoggerFactory classes defined in the SLF4J API, specifically within the org.slf4j package.
这里说了logger的由来,通过引入slf4j的api,注意这里并没有引入logback的api,
这里就是我之前提到的slf4j是接口,logback是实现类。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
On the first line of the main() method, the variable named logger is assigned a Logger instance retrieved by invoking the static getLogger method from the LoggerFactory class. This logger is named “chapters.introduction.HelloWorld1”. The main method proceeds to call the debug method of this logger passing “Hello World” as an argument. We say that the main method contains a logging statement of level DEBUG with the message “Hello world”.
这段话是说 Logger logger = LoggerFactory.getLogger(“chapters.introduction.HelloWorld1”);的由来,是通过静态方法来的,通过debug 输出hello world
Note that the above example does not reference any logback classes. In most cases, as far as logging is concerned, your classes will only need to import SLF4J classes. Thus, the vast majority, if not all, of your classes will use the SLF4J API and will be oblivious to the existence of logback.
还是我上面说的slf4j是接口,logback的实现被隐藏了。
此时如果是简单的输出日志,目前已经满足基本需求了。
但是需求千奇百怪,八股文一套一套,框架底层刨根究底,还得继续学。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;public class HelloWorld2 {public static void main(String[] args) {Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");logger.debug("Hello world.");// print internal stateLoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();StatusPrinter.print(lc);}
}
Logback explains that having failed to find the logback-test.xml and logback.xml configuration files (discussed later), it configured itself using its default policy, which is a basic ConsoleAppender. An Appender is a class that can be seen as an output destination. Appenders exist for many different destinations including the console, files, Syslog, TCP Sockets, JMS and many more. Users can also easily create their own Appenders as appropriate for their specific situation.
通过打印日志可以看到,没有找到logback-test.xml,logback.groovy,logback.xml 然后采用了默认的配置ConsoleAppender。
Appender可以看作
Note that in case of errors, logback will automatically print its internal state on the console.
The previous examples are rather simple. Actual logging in a larger application would not be that different. The general pattern for logging statements would not change. Only the configuration process would be different. However, you would probably want to customize or configure logback according to your needs. Logback configuration will be covered in subsequent chapters.
Note that in the above example we have instructed logback to print its internal state by invoking the StatusPrinter.print() method. Logback’s internal status information can be very useful in diagnosing logback-related problems.
Here is a list of the three required steps in order to enable logging in your application.
1.Configure the logback environment. You can do so in several more or less sophisticated ways. More on this later.
2.In every class where you wish to perform logging, retrieve a Logger instance by invoking the org.slf4j.LoggerFactory class’ getLogger() method, passing the current class name or the class itself as a parameter.
3.Use this logger instance by invoking its printing methods, namely the debug(), info(), warn() and error() methods. This will produce logging output on the configured appenders.
说了3点:
1.配置logback环境也就是引入jar,写xml
2.org.slf4j.LoggerFactory class’ getLogger() method
3.log.debug log.info 去打印
这个是最最简单的。继续学习。