打印 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>