标签:bug 添加 system get over evel include 滚动 []
Spring Boot集成了Logback日志系统。
Logback的核心对象主要有3个:Logger、Appender、Layout
Logger:日志的记录器 主要用于存放日志对象,也可以定义日志类型、级别。
级别:ERROR、WARE、INFO、DEBUG和TRACE。没有FATAL,归纳到了ERROR级别里。ERROR、WARN and INFO level messages are logged by default.
在Spring Boot中,最好定义为logback-spring.xml。if you ue standard configuration locations,Spring cannot completely control log initialization.(指的就是配置文件名为logback.xml)
//接口定义 public interface SendMessage { public void send(); }
//Stg环境实现类 @Component @Profile("stg") public class StgSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Stg Send()<<<<<<<<"); } }
//Dev 环境实现类 @Component @Profile("dev") public class DevSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Dev Send()<<<<<<<<"); } }
在启动类中配置,如下:
@RestController @SpringBootApplication public class SampleLogbackApplication { private static final Logger logger = LoggerFactory.getLogger(SampleLogbackApplication.class); @Autowired private SendMessage sendMessage; @PostConstruct public void logSomething() { sendMessage.send(); } public static void main(String[] args) throws Exception { SpringApplication.run(SampleLogbackApplication.class, args); } }
对不同的环境进行不同的日志配置,如下:
<springProfile name="dev"> <logger name="sample.logback.web.TestProfileCtrl" level="TRACE" /> </springProfile> <springProfile name="stg"> <logger name="sample.logback.web.TestProfileCtrl" level="WARE" /> </springProfile>
在启动程序的时候通过添加 –spring.profiles.active={profile} 来指定具体使用的配置。例如:
java -jar demo.jar –spring.profiles.active=dev
Spring Boot 会先加载默认的配置文件,然后使用具体指定的profile中的配置去覆盖默认配置。
spring.profiles.active 默认指定dev环境,如果我们在运行时指定 –spring.profiles.active=stg 那么将应用stg环境,最终 server.port 的值为8082
用于指定日志输出的目的地,目的地可以是控制台、文件、远程套接字服务器、 MySQL等数据库、 JMS和远程UNIX Syslog守护进程等。
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <!-- <logger name="sample.logback" level="DEBUG" /> --> <!-- 控制台输出 --> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 时间滚动输出 level为 DEBUG 日志 --> <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>D:/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 时间滚动输出 level为 ERROR 日志 --> <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>D:/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 特定过滤含有某字符串的日志 --> <appender name="file-str" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator> <expression>return message.contains("str");</expression> </evaluator> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>D:/logs/contains.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 数据库输出 --> <!-- <appender name="db" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource"> <driverClass>com.mysql.jdbc.Driver</driverClass> <url>jdbc:mysql://host_name:3306/log_db</url> <user>root</user> <password>123qwe</password> </connectionSource> </appender> --> <logger name="java.sql.Connection"> <level value="DEBUG" /> </logger> <logger name="java.sql.Statement"> <level value="DEBUG" /> </logger> <logger name="sample.logback.SampleLogbackApplication"> <level value="DEBUG" /> </logger> <logger name="sample.logback.web.TestCtrl"> <level value="INFO" /> </logger> <root level="DEBUG"> <appender-ref ref="stdout" /> <appender-ref ref="file—debug" /> <appender-ref ref="file—error" /> <appender-ref ref="file-str" /> <!--<appender-ref ref="db" /> --> </root> </configuration>
如上,日志有记录输出到控制台的,也有记录到文件或者数据库中的。
不过Spring Boot中的pom.xml配置文件主要依赖的包如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>2.5.16</version> </dependency> </dependencies>
负责把事件转换成字符串,格式化的日志信息的输出。
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>TRACE</level> </filter> <encoder> <Pattern>.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n</Pattern> </encoder> </appender>
<Pattern>节点中定义了输出的格式,解释如下:
<encoder>
表示对日志进行编码:
%d{HH:mm:ss.SSS}
——日志输出时间%thread
——输出日志的进程名字,这在Web应用以及异步任务处理中很有用%-5level
——日志级别,并且使用5个字符靠左对齐%logger{36}
——日志输出者的名字%msg
——日志消息%n
——平台的换行符
参考博文:
(1)Javaeye http://aub.iteye.com/blog/1101260
(2)Spring Boot Doc https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logging-format
剑指架构师系列-spring boot的logback日志记录
标签:bug 添加 system get over evel include 滚动 []
原文地址:http://www.cnblogs.com/mazhimazhi/p/7499358.html