SpringBoot学习(1) - 日志

 1 package com.study.spring_boot_log;
 2
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration;
 6 import org.springframework.context.ConfigurableApplicationContext;
 7
 8 import com.study.spring_boot_log.dao.UserDao;
 9 import com.study.spring_boot_log.service.UserService;
36 @SpringBootApplication(exclude=WebSocketAutoConfiguration.class)
37 public class App {
38     public static void main(String[] args) {
39         ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
40         context.getBean(UserDao.class).log();
41         System.out.println("===================");
42         context.getBean(UserService.class).log();
43
44         context.close();
45     }
46 }

pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4
 5     <groupId>com.study.springboot</groupId>
 6     <artifactId>spring-boot-log</artifactId>
 7     <version>1.0.0</version>
 8     <packaging>jar</packaging>
 9
10     <name>spring-boot-log</name>
11     <url>http://maven.apache.org</url>
12
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16
17     <dependencyManagement>
18         <dependencies>
19             <dependency>
20                 <groupId>org.springframework.boot</groupId>
21                 <artifactId>spring-boot-dependencies</artifactId>
22                 <version>1.5.3.RELEASE</version>
23                 <scope>import</scope>
24                 <type>pom</type>
25             </dependency>
26         </dependencies>
27     </dependencyManagement>
28
29     <dependencies>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter</artifactId>
33         </dependency>
34     </dependencies>
35 </project>
 1 package com.study.spring_boot_log.dao;
 2
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Component;
 6
 7 @Component
 8 public class UserDao {
 9     private Logger log = LoggerFactory.getLogger(UserDao.class);
10     public void log() {
11         log.debug("user dao debug log");
12         log.info("user dao info log");
13         log.warn("user dao warn log");
14         log.error("user dao error log");
15     }
16 }
 1 package com.study.spring_boot_log.service;
 2
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Component;
 6
 7 @Component
 8 public class UserService {
 9     private Logger log = LoggerFactory.getLogger(UserService.class);
10     public void log() {
11         log.debug("user service debug log");
12         log.info("user service info log");
13         log.warn("user service warn log");
14         log.error("user service error log");
15     }
16 }

springboot默认 的日志级别是info
可以通过logging.level.*=debug配置项设置,*可以是包,也可以是某个类。

也可以在Run Configurations中配置 --debug,或者SpringApplication.run(App.class,“--debug=true”)。这两种debug只会是springboot默认,自定义的类不会debug。

日志级别有:TRACE,DEBUG,INFO,WARN,ERROR,FATA,OFF
日志级别配置成OFF,表示关闭日志输出

logging.file 指定日志文件路径名字
logging.path 指定日志目录(此时的日志名字为spring.log)
日志文件输出,文件的大小10M之后,就会分割

logging.pattern.console 配置控制台输出日志的pattern
logging.file.console 配置日志文件输出日志的pattern

application.properties

1 logging.level.com.study.spring_boot_log.dao.UserDao=off
2 logging.level.com.study.spring_boot_log.service.UserService=DEBUG
3
4 logging.file=D:/ivy/mylog
5 logging.path=D:/ivy/mylogs
6
7 logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n
8 logging.file.console=%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n

springboot 默认支持logback
也就是说,只需要在classpath下放一个logback.xml,logback-spring.xml的文件,即可定制日志的输出

logback-spring.xml或logback.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <configuration>
 3   <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
 4     <layout class="ch.qos.logback.classic.PatternLayout">
 5       <pattern>%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n</pattern>
 6     </layout>
 7   </appender>
 8
 9
10   <root level="debug">
11     <appender-ref ref="consoleLog" />
12   </root>
13 </configuration>

使用其他的日志组件的步骤:
1.排除默认的日志组件:spring-boot-starter-logging
2.加入新的日志路径依赖
3.把相应的配置文件放在classpath下

log4j2.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <configuration>
 3     <appenders>
 4         <Console name="console" target="SYSTEM_OUT" follow="true">
 5           <PatternLayout pattern="%d{yyyy-MM-dd} [%thread] %-5level %logger{80} - %msg%n"/>
 6         </Console>
 7    </appenders>
 8    <loggers>
 9     <root level="DEBUG">
10       <appender-ref ref="console"/>
11     </root>
12   </loggers>
13 </configuration>
时间: 2024-08-27 19:24:14

SpringBoot学习(1) - 日志的相关文章

尚硅谷springboot学习17-SpringBoot日志

SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> 底层依赖关系 总结: ? 1).SpringBoot底层也是使用slf4j+logback的方式进行日志记录 ? 2).SpringBoot也把其他

SpringBoot学习笔记(1):配置Mybatis

SpringBoot学习笔记(1):配置Mybatis 参考资料: 1.AndyLizh的博客 2.xiaolyuh123的博客 快速开始 添加Mybatis依赖(其他依赖已省去) <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId

Java学习-007-Log4J 日志记录配置文件详解及实例源代码

此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-1-30 13:54:02,请知悉. 所需的 jar 包下载链接为:http://yunpan.cn/cKE56sxqtQCfP  访问密码 63d8 有关 Log4J 日志文件中日志级别及文件配置的详细情况,在 Log4J 的配置文件(xml.properties)中有详细的介绍,敬请参阅!

Springboot学习记录1--概念介绍以及环境搭建

摘要:springboot学习记录,环境搭建: 官方文档地址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/ 本机为Ubuntu 概念:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Spring Boot致力于在蓬勃发展的快速

SpringBoot学习笔记(5):处理前端JSON返回的日期的格式

SpringBoot学习笔记(4):处理前端JSON返回的日期的格式 问题描述 前端页面显示的时间为毫秒格式,不利于直观显示! 解决方法1--后端解决 public class Flow { @JsonFormat(pattern = "yyyy-MM-dd", timezone="GMT+8") private Date flow_date; ..... } 解决方法2--JS处理 function crtTimeFtt(val, row) { if (val !

Springboot学习笔记

Springboot学习笔记(一)-线程池的简化及使用 Springboot学习笔记(二)-定时任务 Springboot学习笔记(三)-常用注入组件方式 原文地址:https://www.cnblogs.com/yw0219/p/9060331.html

SpringBoot学习-SpringMVC自动配置

SpringBoot学习-SpringMVC自动配置 前言 在SpringBoot官网对于SpringMVCde 自动配置介绍 1-原文介绍如下: Spring MVC Auto-configuration Spring Boot provides auto-configuration for Spring MVC that works well with most applications. The auto-configuration adds the following features

Springboot学习05-自定义错误页面完整分析

Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1-Springboot错误页面匹配机制(以404错误为例): 1-在模板引擎下:找templates/error/404.html;如果没有,则继续匹配 2-在模板引擎下:找templates/error/4XX.html;如果没有,则继续匹配 3-在静态资源下:找static/error/404.

springboot动态修改日志级别+权限认证

1. springboot动态修改日志级别+权限认证 1.1. 需求 网上找到的动态修改日志级别的方式,基本都是没有权限验证的,或者特地关闭权限验证,但也没给出加上验证的解决方式 修改日志等级也是一个敏感操作,最好不能暴露地址直接修改,所以我研究了下,把权限验证加上了 1.2. 解决 1.2.1. pom 首先加上pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>