1, logback配置
springboot 默认支持logback, 自动加载classpath:logback-spring.xml
<!-- logback多文件输出 --> <property name="log.root.level" value="DEBUG" /> <!-- 日志级别 --> <property name="log.other.level" value="DEBUG" /> <!-- 其他日志级别 --> <property name="log.moduleName" value="elasticSearch5.2" /> <!-- 模块名称, 影响日志配置名,日志文件名 --> <!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 --> <property name="log.base" value="${log.dir:-logs}/${log.moduleName}" /> <property name="log.max.size" value="100MB" /> <!-- 日志文件大小,超过这个大小将被压缩 --> <!--控制台输出 --> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern>logback-%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n</Pattern> </encoder> </appender> <!-- 用来保存输出所有级别的日志 --> <appender name="file.all" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${log.base}/${log.moduleName}.log</File><!-- 设置日志不超过${log.max.size}时的保存路径,注意如果 是web项目会保存到Tomcat的bin目录 下 --> <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${log.base}/archive/${log.moduleName}_all_%d{yyyy-MM-dd}.%i.log.zip </FileNamePattern> <!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份) --> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.max.size}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <!-- 日志输出的文件的格式 --> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n</pattern> </layout> </appender> <!-- 这也是用来保存输出所有级别的日志 --> <appender name="file.all.other" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${log.base}/${log.moduleName}_other.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${log.base}/archive/${log.moduleName}_other_%d{yyyy-MM-dd}.%i.log.zip </FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.max.size}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method:%L -%msg%n</pattern> </layout> </appender> <!-- 只用保存输出error级别的日志 --> <appender name="file.error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${log.base}/${log.moduleName}_err.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${log.base}/archive/${log.moduleName}_err_%d{yyyy-MM-dd}.%i.log.zip </FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.max.size}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method:%L - %msg%n</pattern> </layout> <!-- 下面为配置只输出error级别的日志 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> <!-- 添加附加的appender,最多只能添加一个 --> <appender name="file.async" class="ch.qos.logback.classic.AsyncAppender"> <discardingThreshold>0</discardingThreshold> <queueSize>256</queueSize> <includeCallerData>true</includeCallerData> <appender-ref ref="file.all" /> </appender> <!-- 使用异步记录信息 --> <appender name="file.async.other" class="ch.qos.logback.classic.AsyncAppender"> <discardingThreshold>0</discardingThreshold> <queueSize>256</queueSize> <includeCallerData>true</includeCallerData> <appender-ref ref="file.all.other" /> </appender> <!-- 为某个包下的所有类的指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName --> <logger name="com.lin" additivity="false"> <level value="${log.root.level}" /> <appender-ref ref="stdout" /> <appender-ref ref="file.async" /><!-- 即com.lin包下级别为 ${log.root.level}的才会使用file.async来打印 --> <appender-ref ref="file.error" /> </logger> <!-- root将级别为${log.root.level}及大于${log.root.level}的日志信息交给已经配置好的名为“Console”的appender处理,“Console”appender将信息打印到Console,其它同理 --> <root level="${log.root.level}"> <appender-ref ref="stdout" /> <!-- 标识这个appender将会添加到这个logger --> <appender-ref ref="file.async.other" /> <appender-ref ref="file.error" /> </root>
配置logback.xml的多环境日志出书
<include resource="org/springframework/boot/logging/logback/base.xml" /> <logger name="org.springframework.web" level="INFO"/> <logger name="org.springboot.sample" level="TRACE" /> <!-- 测试环境+开发环境. 多个使用逗号隔开. --> <springProfile name="test,dev"> <logger name="org.springframework.web" level="INFO"/> <logger name="org.springboot.sample" level="INFO" /> <logger name="com.kfit" level="info" /> </springProfile> <!-- 生产环境. --> <springProfile name="prod"> <logger name="org.springframework.web" level="ERROR"/> <logger name="org.springboot.sample" level="ERROR" /> <logger name="com.kfit" level="ERROR" /> </springProfile>
2, log4j2 配置
嗯, 刚刚继承了logback, 又需要换成log4j2
百度了下, log4j2和logback和log4j出自一个人的手, 但log4j2的同步和异步日志记录性能更为优异
2.1) 在maven上引入依赖
<!-- log related --> <dependency> <!-- exclude掉spring-boot的默认log配置 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <!-- 引入log4j2依赖 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <!-- 加上这个才能辨认到log4j2.yml文件 --> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency> <!-- end of log related -->
2.2) log4j2.yml, 放置在resources文件夹下即可, springboot可自动读取
Configuration: status: warn Properties: # 定义全局变量 Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下: #测试:-Dlog.level.console=warn -Dlog.level.xjj=trace #生产:-Dlog.level.console=warn -Dlog.level.xjj=info - name: log.level.console value: trace - name: log.level.xjj value: trace - name: log.path value: /opt/logs - name: project.name value: my-spring-boot Appenders: Console: #输出到控制台 name: CONSOLE target: SYSTEM_OUT ThresholdFilter: level: ${sys:log.level.console} # “sys:”表示:如果VM参数中没指定这个变量值,则使用本文件中定义的缺省全局变量值 onMatch: ACCEPT onMismatch: DENY PatternLayout: pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" RollingFile: # 输出到文件,超过128MB归档 - name: ROLLING_FILE ignoreExceptions: false fileName: ${log.path}/${project.name}.log filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz" PatternLayout: pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" Policies: SizeBasedTriggeringPolicy: size: "128 MB" DefaultRolloverStrategy: max: 1000 Loggers: Root: level: info AppenderRef: - ref: CONSOLE - ref: ROLLING_FILE Logger: # 为com.xjj包配置特殊的Log级别,方便调试 - name: com.xjj additivity: false level: ${sys:log.level.xjj} AppenderRef: - ref: CONSOLE - ref: ROLLING_FILE
3, 通过配置level, 既可以实现不同配置的输出了
4, 在项目中使用就可以了
protected final Logger logger = LoggerFactory.getLogger(this.getClass());