#Logback入门 @FDDLC
发布日期:2021-06-30 21:03:20 浏览次数:3 分类:技术文章

本文共 3345 字,大约阅读时间需要 11 分钟。

Logback is intended as a successor to the popular log4j project, .

logback is divided into three modules, logback-core, logback-classic and logback-access.

 

logback三大模块的简介:

 

log4j.properties to logback.xml Translator:

 

logback.xml to logback.groovy translator:

 

官方教程(英文版): (over 150 pages and dozens of concrete examples)推荐!!!慢慢读,慢即是快。

 

依赖关系:

 

官方示例:

package chapters.introduction;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 state    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();    StatusPrinter.print(lc);  }}

 

可以不手动配置Logback(即无相关配置文件):

By virtue of logback's default configuration policy, when no default configuration file is found, logback will add a ConsoleAppender to the root logger.

 

Appender(输出目的地):an output destination is called an appender. Currently, appenders exist for the console, files, remote socket servers, to MySQL, PostgreSQL, Oracle and other databases, JMS, and remote UNIX Syslog daemons.

More than one appender can be attached to a logger.

 

Logback中的Level:

示例:

import ch.qos.logback.classic.Level;import org.slf4j.Logger;import org.slf4j.LoggerFactory;....// get a logger instance named "com.foo". Let us further assume that the// logger is of type  ch.qos.logback.classic.Logger so that we can// set its levelch.qos.logback.classic.Logger logger =         (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.foo");//set its Level to INFO. The setLevel() method requires a logback loggerlogger.setLevel(Level. INFO);Logger barlogger = LoggerFactory.getLogger("com.foo.Bar");// This request is enabled, because WARN >= INFOlogger.warn("Low fuel level.");// This request is disabled, because DEBUG < INFO. logger.debug("Starting search for nearest gas station.");// The logger instance barlogger, named "com.foo.Bar", // will inherit its level from the logger named // "com.foo" Thus, the following request is enabled // because INFO >= INFO. barlogger.info("Located nearest gas station.");// This request is disabled, because DEBUG < INFO. barlogger.debug("Exiting gas station search");

 

同名则同物:

 

继承无序:

In fundamental contradiction to biological parenthood, where parents always precede their children, logback loggers can be created and configured in any order. In particular, a "parent" logger will find and link to its descendants even if it is instantiated after them.

 

Logger的命名策略:

This can be accomplished by instantiating a logger in each class, with the logger name equal to the fully qualified name of the class. This is a useful and straightforward method of defining loggers. As the log output bears the name of the generating logger, this naming strategy makes it easy to identify the origin of a log message. However, this is only one possible, albeit common, strategy for naming loggers. Logback does not restrict the possible set of loggers. As a developer, you are free to name loggers as you wish.

Nevertheless, naming loggers after the class where they are located seems to be the best general strategy known so far.

 

Appender的附加规则:

示例:

 

 

 

各个level的关系:

 

 

 

配置文件的结构:

 

 

极简入门:

 

logback.xml详解:

 

 

 

 

 

 

转载地址:https://liuxingchang.blog.csdn.net/article/details/115215182 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#力扣 LeetCode504. 七进制数 @FDDLC
下一篇:#SLF4J入门 @FDDLC

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月14日 18时12分22秒