Spring boot 日志问题

_冢彧 发布于 2016/09/27 12:04
阅读 1K+
收藏 0

一、logback配置文件如下

src/main/resources/config/log/logback-spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 日志组件启动时,打印调试信息,并监控此文件变化,周期120秒 -->
<configuration scan="true" scanPeriod="120 seconds" debug="true">

    <!-- 项目名称及日志目录定义 -->
    <property name="PROJECT_NAME" value="dragon"/>
    <property name="LOG_PATH" value="/var/log/webapps/${PROJECT_NAME}"/>

    <contextName>${CONTEXT_NAME}</contextName>

    <!-- include spring boot logback base configuration -->
    <!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->

    <!-- ===================================================================== -->
    <!-- 以下是appender的定义 -->
    <!-- ===================================================================== -->

    <!-- 记录到控制台,仅用于本地开发 -->
    <!-- TODO 本地开发环境只需要控制台输出,其他Appender仅作为测试logback配置,后期可删除其他appender -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH🇲🇲ss.SSS} %-5level [%15.15thread] %logger{36}:%L - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="INFO-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/apps.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_PATH}/apps.log.%d{yyyy-MM-dd}.%i</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>1024MB</MaxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH🇲🇲ss.SSS} %-5level [%thread] %logger{0}:%L- %msg%n</pattern>
            </layout>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="WARN-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/apps-warn-30dt.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_PATH}/apps-warn-30dt.log.%d{yyyy-MM-dd}.%i</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>1024MB</MaxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH🇲🇲ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            </layout>
            <charset>UTF-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/apps-error-30dt.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_PATH}/apps-error-30dt.log.%d{yyyy-MM-dd}.%i</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>1024MB</MaxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH🇲🇲ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            </layout>
            <charset>UTF-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- ===================================================================== -->
    <!-- 以下是logger的定义 -->
    <!-- ===================================================================== -->
    <springProfile name="dev">
        <logger name="com.mzw.dragon" additivity="true">
            <level value="INFO"/>
            <appender-ref ref="STDOUT"/>
        </logger>
        <logger name="org.apache" additivity="true">
            <level value="INFO"/>
            <appender-ref ref="STDOUT"/>
        </logger>
        <logger name="org.springframework" additivity="true">
            <level value="INFO"/>
            <appender-ref ref="STDOUT"/>
        </logger>
    </springProfile>

    <springProfile name="online">
        <logger name="com.mzw.dragon" additivity="true">
            <level value="INFO"/>
        </logger>
    </springProfile>

    <!-- ===================================================================== -->
    <!-- Root logger的定义 -->
    <!-- ===================================================================== -->
    <root>
        <level value="INFO"/>
        <!--<appender-ref ref="STDOUT"/>-->
        <appender-ref ref="INFO-APPENDER"/>
        <appender-ref ref="WARN-APPENDER"/>
        <appender-ref ref="ERROR-APPENDER"/>
    </root>
</configuration>
二、application配置文件
spring.profiles.active=dev
logging.config=classpath:config/log/logback-spring.xml
#logging.path=/var/log/webapps/dragon
#logging.level.root=INFO
#logging.level.com.mzw.dragon=INFO
#logging.level.org.apache=INFO
#logging.level.root.springframework=INFO
############################### VELOCITY TEMPLATES (VelocityAutoConfiguration)
spring.velocity.charset=UTF-8
spring.velocity.properties.input.encoding=UTF-8
spring.velocity.properties.output.encoding=UTF-8
spring.velocity.properties.parser.pool.size=100
spring.velocity.properties.velocimacro.library=/velocityTemplates/macros/macros.vm
spring.velocity.properties.velocimacro.library.autoreload=false
spring.velocity.cache=false
spring.velocity.content-type=text/html;charset=UTF-8
spring.velocity.date-tool-attribute=date
spring.velocity.number-tool-attribute=number
spring.velocity.prefix=/velocityTemplates
spring.velocity.suffix=.vm
spring.velocity.resourceLoaderPath=classpath:/velocityTemplates
spring.velocity.toolbox-config-location=/config/velocity/velocity-toolbox.xml
spring.mvc.view.prefix=/velocityTemplates
spring.mvc.view.suffix=.vm
 三、启动类文件
package com.mzw.dragon;

import com.mzw.dragon.configuration.DragonConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Import;

/**
 * Created by victor.min on 2016/9/9.
 */
@SpringBootApplication
@Import({DragonConfiguration.class})
public class BootStrap extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

    private static Logger logger = LoggerFactory.getLogger(BootStrap.class);

    public static void main(String[] args) throws Exception {
//        System.setProperty("spring.profiles.active", "dev");
        logger.info("--------------------------------------------");
        logger.info("------------------application start--------------------------");
        SpringApplication.run(BootStrap.class, args);
        logger.info("-------------------application end-------------------------");
        logger.info("--------------------------------------------");
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        logger.info("--------------------------------------------");
        logger.info("------------------customize start--------------------------");
        configurableEmbeddedServletContainer.setPort(8088);
        logger.info("-------------------customize end-------------------------");
        logger.info("--------------------------------------------");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        logger.info("--------------------------------------------");
        logger.info("------------------configure start--------------------------");
        return builder.sources(BootStrap.class);
    }
}

不论在IDEA 里面直接RUN还是使用maven spring-boot:run启动,日志都可以被正确的记录,但是当打成war包放到外部tomcat下时,日志就不能被记录了……

求大大们帮下忙呀~~

看了下面的两篇文章,好像那样也不行呀~~

http://www.cnblogs.com/zb38/p/5411701.html

http://www.cnblogs.com/puroc/p/5049222.html

加载中
0
_冢彧
_冢彧

卧擦,不知道什么原因

把logback-spring.xml移动到src/main/resources文件夹下,然后修改application.properties文件中的logging.config为logging.config=classpath:logback-spring.xml日志就出来了,感觉是logging.config的加载哪里出错了,不知道为什么logging.config=classpath:config/log/logback-spring.xml就是不行、、、修改后日志记录怀疑是因为spring boot会默认去classpath下找logback.xml和logback-spring.xml、logbakc.groovy、logback-spring.groovy这四个文件…………

_冢彧
_冢彧
回复 @skhuhu : 老感觉是logging.config这个配置没有生效,以后有什么问题多多请教你呀
skhuhu
skhuhu
boot 要看源码 里面好多默认设置···坑多
0
北极天涯
北极天涯
我也遇到打成war包日志不出来这个问题了,我的logback.xml文件就在resources目录下,只不过没有在application.properties中指定‘logging.config=classpath:logback.xml’,后来加上这句话就好了。
_冢彧
_冢彧
我是放到src/main/resources/config/log/logback.xml,然后配的是logging.config=classpath:config/log/logback.xml,然后打成war记录不了日志,但是直接运行又可以记录
_冢彧
_冢彧
#logging.config=# Location of the logging configuration file. For instance `classpath:logback.xml` for Logback 如果是直接放到src/main/resources文件夹下,不用配都可以的,因为springboot的默认就会去下面找logback.xml这个文件,
0
码翼
码翼

由Spring Boot实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档.
之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of REST Client tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client

码翼
码翼
新版本地址 https://github.com/Wisdom-Projects/rest-client
返回顶部
顶部