写文章

Spring Boot基础教程5-日志配置-logback和log4j2

2018-11-29 17:59:30

35635 | 4 | 0

Spring Boot基础教程1-Spring Tool Suite工具的安装

Spring Boot基础教程2-RESTful API简单项目的快速搭建

Spring Boot基础教程3-配置文件详解:Properties和YAML

Spring Boot基础教程4-配置文件-多环境配置

Spring Boot基础教程5-日志配置-logback和log4j2


视频教程:http://www.roncoo.com/course/view/c99516ea604d4053908c1768d6deee3d

源码地址:https://github.com/roncoo/spring-boot-demo


支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback

配置方式:默认配置文件配置和引用外部配置文件配置

一、 默认配置文件配置(不建议使用:不够灵活,对log4j2等不够友好)

# 日志文件名,比如:roncoo.log,或者是 /var/log/roncoo.log

logging.file=roncoo.log

# 日志级别配置,比如: logging.level.org.springframework=DEBUG

logging.level.*=info

logging.level.org.springframework=DEBUG


二、 引用外部配置文件

2.1 logback配置方式:

spring boot默认会加载classpath:logback-spring.xml或者classpath:logback-spring.groovy


使用自定义配置文件,配置方式为:

logging.config=classpath:logback-roncoo.xml

注意:不要使用logback这个来命名,否则spring boot将不能完全实例化


1.使用基于spring boot的配置

见附件

2.自定义配置

见附件

2.2 log4j配置

2.2.1去除logback的依赖包,添加log4j2的依赖包

2.2.2 在classpath添加log4j2.xml或者log4j2-spring.xml(spring boot 默认加载)

2.3 自定义配置文件

见附件


三.比较

性能比较:Log4J2 和 Logback 都优于 log4j(不推荐使用)

配置方式:Logback最简洁,spring boot默认,推荐使用


项目源码地址

0

收藏
分享
全部评论4

761****@qq.com 2017-02-23 17:53:00

讲的跟屎一样,看了4分钟 。发现好多错误呀 。

lij****@163.com 2017-01-05 16:50:51

Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ demo --- [INFO] Building jar: D:\document\workspace_02\demo\target\demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ demo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.232 s [INFO] Finished at: 2017-01-05T16:49:40+08:00 [INFO] Final Memory: 21M/288M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) on project demo: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.4.3.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.uztek.yunlian100.DemoApplication, com.uztek.yunlian100.Controller.TestController] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

lij****@163.com 2017-01-05 16:31:36

加入了devtools 工具,无法自动重启

lij****@163.com 2017-01-05 16:20:40

用logback方式配置报错 配置如下: <property name="TEST_FILE_PATH" value="D:/document/workspace_02/logs" /> <!-- pro文件路径 --> <!--<property name="PRO_FILE_PATH" value="/opt/roncoo/logs" />--> <property name="PRO_FILE_PATH" value="D:/document/workspace_02/logs/" /> <!-- 开发环境 --> <springProfile name="dev"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${PATTERN}</pattern> </encoder> </appender> <logger name="com.uztek.example" level="debug"/> <root level="info"> <appender-ref ref="CONSOLE" /> </root> </springProfile> <!-- 测试环境 --> <springProfile name="test"> <!-- 每天产生一个文件 --> <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件路径 --> <file>${TEST_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件名称 --> <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 文件最大保存历史数量 --> <MaxHistory>100</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="info"> <appender-ref ref="TEST-FILE" /> </root> </springProfile> 报错信息: ERROR in ch.qos.logback.core.rolling.RollingFileAppender[TEST-FILE] - openFile(D:/document/workspace_02/logs,true) call failed. java.io.FileNotFoundException: D:\document\workspace_02\logs (拒绝访问。) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:161) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:57) at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:47) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:114) at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:304) at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:272) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:235) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:208) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:68)
没有更多了