package com.study.spring_boot_log;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext; import com.study.spring_boot_log.dao.UserDao;
import com.study.spring_boot_log.service.UserService;
@SpringBootApplication(exclude=WebSocketAutoConfiguration.class)
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
context.getBean(UserDao.class).log();
System.out.println("===================");
context.getBean(UserService.class).log(); context.close();
}
}

pom.xml:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.study.springboot</groupId>
<artifactId>spring-boot-log</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>spring-boot-log</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
 package com.study.spring_boot_log.dao;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; @Component
public class UserDao {
private Logger log = LoggerFactory.getLogger(UserDao.class);
public void log() {
log.debug("user dao debug log");
log.info("user dao info log");
log.warn("user dao warn log");
log.error("user dao error log");
}
}
 package com.study.spring_boot_log.service;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; @Component
public class UserService {
private Logger log = LoggerFactory.getLogger(UserService.class);
public void log() {
log.debug("user service debug log");
log.info("user service info log");
log.warn("user service warn log");
log.error("user service error log");
}
}

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

 logging.level.com.study.spring_boot_log.dao.UserDao=off
logging.level.com.study.spring_boot_log.service.UserService=DEBUG logging.file=D:/ivy/mylog
logging.path=D:/ivy/mylogs logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n
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:

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

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

log4j2.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{yyyy-MM-dd} [%thread] %-5level %logger{80} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="DEBUG">
<appender-ref ref="console"/>
</root>
</loggers>
</configuration>

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

  1. 尚硅谷springboot学习17-SpringBoot日志

    SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> < ...

  2. SpringBoot学习(三):日志

    1.日志框架 小张:开发一个大型系统: ​ 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息: ...

  3. Springboot学习:日志

    介绍 市面上的日志框架: JUL.JCL.Jboss-logging.logback.log4j.log4j2.slf4j.... 日志门面 (日志的抽象层) 日志实现 JCL(Jakarta Com ...

  4. SpringBoot学习笔记(13):日志框架

    SpringBoot学习笔记(13):日志框架——SL4J 快速开始 说明 SpringBoot底层选用SLF4J和LogBack日志框架. SLF4J的使用 SpringBoot的底层依赖关系 1. ...

  5. SpringBoot学习- 9、Slf4j日志

    SpringBoot学习足迹 在上一篇学习中 通过画红线的注解,可以直接在下面log.debug输出日志到控制台,但是写日志文件就没那么顺利了,一直不成功,找了N种配置,以下配置方法可行 首先确保已引 ...

  6. springboot学习4使用日志:logback

    springboot学习4使用日志:logback 一.基本知识说明 SpringBoot默认使用logback作为日志框架 ,所以引入起步依赖后就可以直接使用logback,不需要其他依赖. Spr ...

  7. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  8. SpringBoot学习笔记

    SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...

  9. SpringBoot学习历程

    新年新气象,更新了一下本人所有写的关于SpringBoot的文章目录,感谢大家长期以来的支持,在接下来的日子还会不定期的进行更新. 入门 使用IntelliJ Idea新建SpringBoot项目 S ...

随机推荐

  1. 登录密码忘记修改jenkins

    find / -type f -name 'config.xml' 然后需要删除config.xml文件中的以下内容: <useSecurity>true</useSecurity& ...

  2. $NOIp$提高组做题记录

    对了我在这里必须讲一个非常重要的事情,就是前天也就是$2019.8.21$的傍晚,我决定重新做人了$!!$ 其实之前没怎么做$Noip$题,那就从现在开始叭

  3. Electron-forge应用(打包填坑)

    Electron-forge应用   一.  使用Electron-Forge做应用的缘由 最近遇到一个需求,Web应用登录时要校验用户的登录Mac地址,以确定该用户是在授权过的电脑设备上登录的.没错 ...

  4. JVM系列(三):java的垃圾回收机制

    java垃圾回收机制介绍    上一篇讲述了JVM的内存模型,了解了到了绝大部分的对象是分配在堆上面的,我们在编码的时候并没有显示的指明哪些对象需要回收,但是程序在运行的过程中是会一直创建对象的,之所 ...

  5. hadoop传递参数方法总结

    转自:http://blog.csdn.net/xichenguan/article/details/22162813 写MapReduce程序通常要传递各种各样的参数,选择合适的方式来传递参数既能提 ...

  6. Java开发中的各种乱码问题

    乱码问题 其实解决乱码问题,就是保证所有的编码格式一致,就不会出现问题. 控制台乱码 修改idea的控制台格式 修改 idea.exe.vmoptions和idea64.exe.vmoptions 在 ...

  7. config.xml写入和读取

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. Django之Session与Cookie

    目录 一.cookie Cookie的由来 什么是Cookie Cookie的原理 查看Cookie cookie与session的作用 二.Django中操作Cookie 获取Cookie 设置Co ...

  9. Scala实践4

    一.数组 在Scala中,用()来访问元素,数组声明的语法格式如下 : var z:Array[String] = new Array[String](3) 或 var z = new Array[S ...

  10. hdfs断电报错解决

    一,/home/hadoop/tmp/dfs/name/current 目录下查看文件二,1.stop hadoop所有的服务;2.重新格式化namenode即可: hadoop根目录下: hadoo ...