EurekaUser-Logback-json

项目路径如下

EurekaUser-Logback-json

 

 

 

pom.xml添加

        <!-- logback -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <version>2.1.11.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

 application.properties添加

#日志存放路径
logging.paths=/home/odyun/user
#配置引用路径
logging.config=classpath:conf/logging/logback-spring.xml

  logback-spring.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <!-- 属性文件:在properties文件中找到对应的配置项 -->
    <springProperty scope="context" name="LOGGING_PATH" source="logging.paths"/>

    <contextName>EurekaUser</contextName>

    <!-- 输出控制台 -->
    <!-- ConsoleAppender:把日志添加到控制台 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!--格式化输出(配色):%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%yellow(%d{yyyy-MM-dd HH:mm:ss}) %red([%thread]) %highlight(%-5level) %cyan(%logger{50}) - %black(%msg) %n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--根据日志级别分离日志,分别输出到不同的文件-->
    <!--RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。它是FileAppender的子类-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGGING_PATH}/info.log</file>
        <encoder>
            <pattern>
                [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} -%msg%n
            </pattern>
            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--按时间保存日志 修改格式可以按小时、按天、月来保存-->
            <fileNamePattern>${LOGGING_PATH}/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>10</MaxHistory>
            <!-- 文件压缩分为多个文件 -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>30KB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--文件大小-->
            <!--<totalSizeCap>10K</totalSizeCap>-->
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>


    <!-- 日志级别 -->
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
    </root>

</configuration>

  UserAction

@RestController
@RequestMapping("/user")
public class UserAction {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${server.port}")
    String port;

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/hi")
    public String hi(@RequestParam String name) {

        logger.info("----- selectAll method test ------");
        List<User> userList = userMapper.selectList(null);
        logger.info("userList:{}", JSON.toJSON(userList));
        return "hi " + name + ",i am from port:" + port;
    }

}

  启动项目访问http://127.0.0.1:10001/user/hi?name=ok

EurekaUser-Logback-json

 

EurekaUser-Logback-json

 

EurekaUser-Logback-json

上一篇:RHEL 6.5 rpm包安装mplyer


下一篇:缓存算法