zoukankan      html  css  js  c++  java
  • 【SSM】日志框架 logback

    logback.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration scan="false" scanPeriod="60000" debug="false">
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
            </layout>
        </appender>
    
        <appender name="eventFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!--<File>${catalina.base}/logs/BigeyeCenter/BigeyeCenter.log</File>-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 -->
                <FileNamePattern>${catalina.home}/logback-logs/logback.%d{yyyy-MM-dd}.log</FileNamePattern>
                <!--<FileNamePattern>D:/AssessmentLogs/logback.%d{yyyy-MM-dd}.log</FileNamePattern>-->
                <!-- keep 30 days' worth of history -->
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
                <charset>UTF-8</charset> <!-- 此处设置字符集 -->
            </encoder>
        </appender>
    
        <root level="info">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="eventFile"/>
        </root>
    
    </configuration>

    webTest

    package com.kikyo.admin.controller;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Date;
    
    @Controller
    @RequestMapping("login")
    public class LoginController {
    
        private Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @RequestMapping("hello")
        public String hello() {
            logger.error("error " + new Date());
            return "admin/login/hello";
        }
    }
    Test
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class Test {
        static Logger logger = LoggerFactory.getLogger(Object.class);
    
        public static void main(String[] args) {
    
            logger.trace("=====trace=====");
            logger.debug("=====debug=====");
            logger.info("=====info=====");
            logger.warn("=====warn=====");
            logger.error("=====error=====");
        }
    }

     Pom.xml

            <!-- logback+slf4j -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.0</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>0.9.28</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>0.9.28</version>
                <type>jar</type>
            </dependency>


  • 相关阅读:
    软件测试之po设计模式
    Python_标识符及命名规范
    python基础(第一个python程序)
    计算机基础知识
    注册界面测试案例
    vscode使用-添加格式化插件
    工作-提交review失败
    工作-大首页走测试环境
    jquery--选择器 第几
    a标签href无值,点击刷新页面解决办法
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12207655.html
Copyright © 2011-2022 走看看