• logback 中打印自定义参数 (ip 服务名)


    打印 application.properties 配置文件中的参数

    首先需要引入文件

    <property resource="application.properties"/>

    然后在使用

    <springProperty scope="context" name="serverName" source="spring.application.name"/>
    spring.application.name 为 application.properties 文件中的属性

    将ip打印到日志

    首先编写类 IPConverterConfig

    复制代码
    package com.wxx.converter;
    
    import ch.qos.logback.classic.pattern.ClassicConverter;
    import ch.qos.logback.classic.spi.ILoggingEvent;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class IPConverterConfig extends ClassicConverter {
    
        @Override
        public String convert(ILoggingEvent iLoggingEvent) {
            try {
                return InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    复制代码

    然后在 logback.xml 引用

     logback.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <!--    引入配置文件,获取服务名-->
        <property resource="application.properties"/>
        <springProperty scope="context" name="serverName" source="spring.application.name"/>
    
        <!--配置规则类的位置-->
        <conversionRule conversionWord="ip" converterClass="com.wxx.converter.IPConverterConfig" />
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <!-- encoder的默认实现类是ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
            <encoder>
                <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%ip] [${serverName}] [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <!--logback输出日志到文件(滚动)RollingFileAppender继承了FileAppender-->
        <appender name="cusmanage" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../logs/crm/crm.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>../logs/crm/crm.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>5MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
    
        <root level="DEBUG">
            <appender-ref ref="STDOUT" />
        </root>
        <!--ibatis打印SQL-->
    <!--    <logger name="com.ibatis" level="debug" />-->
    <!--    <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="debug" />-->
    <!--    <logger name="com.ibatis.common.jdbc.ScriptRunner" level="debug" />-->
    <!--    <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="debug" />-->
    <!--    <logger name="java.sql.Connection" level="debug" />-->
    <!--    <logger name="java.sql.Statement" level="debug" />-->
    <!--    <logger name="java.sql.PreparedStatement" level="debug" />-->
    
    </configuration>
    复制代码
  • 相关阅读:
    vscode 在linux中,切换语言模式快捷键
    转载大神的一篇文章----【如何选择开源许可证?】
    Kibana6.x.x——源码发布
    Kibana6.x.x——【Running "run:optimizeBuild" (run) task】出现警告信息
    linux系统为文件添加执行权限
    Kibana6.x.x——执行yarn build出现的警告信息记录
    Kibana6.x.x——导航权限控制入门
    poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
    poj 2386:Lake Counting(简单DFS深搜)
    蓝桥杯 第三届C/C++预赛真题(10) 取球游戏(博弈)
  • 原文地址:https://www.cnblogs.com/exmyth/p/15871547.html
Copyright © 2020-2023  润新知