logback.配置

1.Logger rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
可以直接使用Root_LOGGER_ANME

2.If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. More formally:

The effective level for a given logger L, is equal to the first non-null level in its hierarchy, starting at L itself and proceeding upwards in the hierarchy towards the root logger.

To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level. By default, this level is DEBUG.

如果一个Logger没有指定级别,那么它将继承自最近的父类的级别,而root的存在是为了确保所有的Logger有一个确定的级别。



3.// 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 level
ch.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 logger
logger.setLevel(Level. INFO);
可以使用Logger名创建

4.

Logger NameAttached AppendersAdditivity FlagOutput TargetsComment
rootA1not applicableA1Since the root logger stands at the top of the logger hierarchy, the additivity flag does not apply to it.
xA-x1, A-x2trueA1, A-x1, A-x2Appenders of "x" and of root.
x.ynonetrueA1, A-x1, A-x2Appenders of "x" and of root.
x.y.zA-xyz1trueA1, A-x1, A-x2, A-xyz1Appenders of "x.y.z", "x" and of root.
securityA-sec falseA-secNo appender accumulation since the additivity flag is set to false. Only appender A-sec will be used.
security.accessnonetrueA-secOnly appenders of "security" because the additivity flag in "security" is set to false.
Additivity的作用



5.if(logger.isDebugEnabled()) { 
  logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
In practice, this overhead is insignificant because evaluating a logger takes less than 1% of the time it takes to actually log a request.

Better alternative
logger.debug("The entry is {}.", entry);  //这种方式更好

. In other words, this form does not incur the cost of parameter construction when the log statement is disabled.


6.

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoder的默认实现类是ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line] %logger{20} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="fileAppender" class="ch.qos.logback.core.FileAppender">
        <file>granularity.log</file>
        <encoder><!-- 必须指定,否则不会往文件输出内容 -->
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line]  %logger{20} - %msg%n</pattern>
        </encoder>
        <append>true</append>
        <prudent>false</prudent>
    </appender>

    //不同级别打印到不同文件中,使用filter
    <appender name="errorAppender" class="ch.qos.logback.core.FileAppender">
        <file>log/error.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line]  %logger{20} - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    //默认配置级别
    <root level="debug">
        <appender-ref ref="errorAppender" />
        <appender-ref ref="STDOUT" />
        <appender-ref ref="fileAppender" />
    </root>

    //指定配置级别
    <logger name="com2" level="INFO"></logger>


</configuration>


如果logback.xml配置无效,可以按照以下步骤进行排查和解决问题。 首先,可以将debug模式设置为true,这样可以查看logback.xml的读取日志记录,包括其中的各种策略逻辑。具体的日志查看方式可能因程序而异,但通常在程序启动时就会有相应的日志输出。通过查看日志,可以发现是否是自己设置的logback.xml起作用了。确保依赖的jar包中包含了正确的logback.xml文件。 如果在生产环境中发现设置的logback.xml配置不起作用,可以尝试在启动时通过命令行参数指定logback配置文件,例如使用java -jar启动时加上-Dlogging.config=./conf/logback-spring.xml。这样可以确保使用指定的logback.xml配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [logback.xml不生效的原因](https://blog.csdn.net/puzzledboylhc/article/details/119207271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [logback.xml配置无效](https://blog.csdn.net/FaceFullofConfused/article/details/103690112)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值