zoukankan      html  css  js  c++  java
  • slf4j日志使用

    scala中

    trait LogSupport {
    protected val log = LoggerFactory.getLogger(this.getClass) }


    需要要到的类 extends LogSupport即可
    public class BootTest {
    
        public static void main(String args[]){
            Logger log = LoggerFactory.getLogger(BootTest.class);
            log.info("BootTest"); //打印日志
        }
    
    }

     java中非静态的使用:

    private Logger log = LoggerFactory.getLogger(this.getClass());//传入的参数this.getClass() 影响打印信息%logger{35} 例如: c.z.b.v.a.l.k.test.LogTest

    logback.xml典型配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="60 seconds" debug="true">
        <!-- 定义日志文件 输入位置 -->
        <property name="log_dir" value="./log" />
        <!-- 日志最大的历史 7天 -->
        <property name="maxHistory" value="7"/>
    
        <!-- definition of appender STDOUT -->
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} +[%thread] %-5level - %msg%n</pattern>
    <!-- <charset class="java.nio.charset.Charset">UTF-8</charset> 这样?-->
           <charset>UTF-8</charset> <!-- 防止乱码,设置字符集 -->
    </encoder> </appender> <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.warn.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.info.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.debug.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.error.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <!--%d{yyyy-MM-dd HH:mm:ss.SSS}%-4relative [%thread] %-5level %logger{35} - --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <!-- appender referenced after it is defined --> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_ERROR"/> <appender-ref ref="FILE_WARN"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="FILE_DEBUG"/> </root> </configuration>

    参考:https://my.oschina.net/u/1428349/blog/341826 《logback中文乱码问题》

    https://logback.qos.ch/manual/appenders.html  logback官方文档《Chapter 4: Appenders》

  • 相关阅读:
    C#的第一个应用
    中位数和顺序统计量(第9章)
    散列表的学习和探讨(算法导论第11章)
    数据挖掘和机器学习的基本概念
    Weka数据挖掘——选择属性
    Weka数据挖掘——关联
    Weka数据挖掘——聚类
    Weka数据挖掘——分类
    WEKA-过滤器算法学习
    WEKA——数据挖掘与机器学习工具
  • 原文地址:https://www.cnblogs.com/yongwangzhiqian/p/6484430.html
Copyright © 2011-2022 走看看