springboot的web项目中自带了日志组件:

我们看一下,springboot中找到日志组件。

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

进入starter-web:找到spring-boot-starter

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.1.RELEASE</version>
<scope>compile</scope>
</dependency>

进入spring-boot-starter 找到:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.1.RELEASE</version>
<scope>compile</scope>
</dependency>

进入spring-boot-starter-logging,找到:

  <dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
</dependencies>

所以我们看到其实它的日志底层用的是log4j slf4j 和logback这们是什么判断呢:

slf4j是一套日志接口,就是我们用的jdbc一样,可以连接不同的日志组件,只要符合接口原则就可以。

log4j是java最经典的日志组件了。

log4j已经很多年了,作者不甘寂寞,所以又写了日志组件,就是logback了,logback是基于log4j修改而来,可以说是升级版。

logback的官方地址:https://logback.qos.ch/

官方介绍:

Logback旨在作为流行的log4j项目的后续版本,从而恢复log4j离开的位置

Logback的体系结构足够通用,以便在不同情况下应用。目前,logback分为三个模块:logback-core,logback-classic和logback-access。

logback-core模块为其他两个模块奠定了基础。logback-classic模块可以被同化为log4j的显着改进版本。此外,logback-classic本身实现了SLF4J API,因此您可以在logback和其他日志框架(如log4j或java.util.logging(JUL))之间来回切换。

logback-access模块​​与Servlet容器(如Tomcat和Jetty)集成,以提供HTTP访问日志功能。请注意,您可以在logback-core之上轻松构建自己的模块。

在springboot中使用logback非常简单:

直接在类中定义一个logger

@SpringBootApplicationpublic class MedicareApplication {

    //定义一个全局的记录器,通过LoggerFactory获取
private final static Logger logger = LoggerFactory.getLogger(MedicareApplication.class); public static void main(String[] args) { logger.info("项目启动成功!!!!!!!!"); SpringApplication.run(MedicareApplication.class, args);
} }

运行项目,就可以在控制台看到日志了。开发环境下,真的是可以说做到0配置了。

在每个类中都定义 logger的静态字段,很烦人,可以引用一个组件解决这个问题:lombok

        <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>

然后就可以修改成:引用注解Slf4j (还需要安装lombok插件)

@SpringBootApplication
@Slf4j
public class MedicareApplication {
public static void main(String[] args) { log.info("项目启动成功!!!!!!!!"); SpringApplication.run(MedicareApplication.class, args);
} }

代码干净了很多 。

有了logback, 还有更好的选择就是log4j2.

官网地址:http://logging.apache.org/log4j/2.x/

官方介绍:Apache Log4j 2是对Log4j的升级,它比其前身Log4j 1.x提供了重大改进,并提供了Logback中可用的许多改进,同时修复了Logback架构中的一些固有问题。

上面的代码如果想切换到log4j2需要大的改动吗,答案是不需要,只需要改一处:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

排除自带的loggin,引入log4j2的starter就可以了。其它不变。

如果我们要把日志输出到指定目录的文件,调整日志输出格式,可以添加配置文件以log4j2的配置文件为例:在resources目录下创建log4j2-spring.xml

<Configuration status="INFO" monitorInterval="30">
<Properties>
<Property name="logpath">/home/logs/log/dev</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
</Console>
<RollingFile name="debug" fileName="${logpath}/debug/erp_debug.log"
filePattern="${logpath}/debug/erp_debug_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/debug" maxDepth="1">
<IfFileName glob="erp_debug_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="info" fileName="${logpath}/info/erp_info.log"
filePattern="${logpath}/info/erp_info_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/info" maxDepth="1">
<IfFileName glob="erp_info_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="warn" fileName="${logpath}/warn/erp_warn.log"
filePattern="${logpath}/warn/erp_warn_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/warn" maxDepth="1">
<IfFileName glob="erp_warn_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="error" fileName="${logpath}/error/erp_error.log"
filePattern="${logpath}/error/erp_error_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/error" maxDepth="1">
<IfFileName glob="erp_error_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile> </Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</Root>
</Loggers>
</Configuration>

该配置文件摘自地址:http://syllabus.lianmengtu.top/  一份史诗级配置

重新配置一下文件地址,就可以运行,并看到自己的日志了。

为了调试方便,可以给控制台增加彩色的输出,

先安装插件:Grep Console

最新文章

  1. Python Thread related
  2. Mybatis——oracle 的模糊查询 和 日期处理
  3. MFC 框架技术简单研讨
  4. join()、implode()函数
  5. NOIP2012 借教室
  6. android实操--练习1
  7. intel的网卡故障
  8. C++与Lua交互(四)
  9. [XJOI NOI02015训练题7] B 线线线 【二分】
  10. fsl的feat软件分包使用笔记
  11. Yii 生成表单下拉选框及查询下拉选框
  12. Best Financing(HD4833)
  13. IOS 使用程序外地图(IOS Map and google Map)
  14. SQLServer 存储过程嵌套事务处理
  15. VS2013各版本激活密钥
  16. 【Java】JDK/JVM相关工具
  17. Python3学习之路~5.10 PyYAML模块
  18. C++Primer第五版——习题答案详解(十一)
  19. MFC设置单文档保存格式以及标题
  20. Java:内省(Introspector)

热门文章

  1. ios 屏幕旋转的问题
  2. Tomcat 启动出现警告问题Setting property &#39;minSpar eThreads&#39; to &#39;25&#39; did not find a matching property
  3. jenkins之构建maven项目
  4. poj2411 Mondriaan&#39;s Dream【状压DP】
  5. Eclipse Tomcat插件的配置, 及 Tomcat 的配置
  6. TFIDF练习
  7. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)
  8. Supermarket---poj456(贪心并查集优化)
  9. 三个小时学会wordpress模板制作
  10. DBA-常用到的动态视图分析语句