如何使用Spring Boot将日志消息写入文件? [英] How to write log messages to file using Spring Boot?

查看:425
本文介绍了如何使用Spring Boot将日志消息写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将消息记录在文件中,而不是在控制台上.我正在使用Spring Boot,其配置如下:

I want to log message in a file and not on the console. I am using Spring Boot and my configuration is as follows:

application.properties:

application.properties:

logging.level: DEBUG
logging.level: ERROR
logging.file: ${HOME}/application.log

我仅在application.log文件中获得INFO的日志消息,但是我也希望获得ERROR和DEBUG消息.

I am getting log messages of INFO only in my application.log file but I want ERROR and DEBUG messages as well.

我的要求是我想要error.log文件中的ERROR消息和debug.log中的DEBUG消息,以及info.log中的INFO消息.

My requirement is that I want ERROR message in error.log file and DEBUG message in debug.log and INFO messages in info.log.

非常感谢任何帮助.

推荐答案

Spring Boot允许您使用application.properties配置日志记录系统的一些基本方面,但是有

Spring Boot allows you to configure some basic aspects of your logging system using application.properties, but there are limits:

要配置日志记录系统的更细粒度的设置,您需要使用所讨论的LoggingSystem支持的本机配置格式.

To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question.

换句话说,如果您想执行某些属性未特别支持的操作,则不会添加和编辑logback.xml文件(假设您使用的是Logback).

In other words, if you want to do something that isn't specifically supported through properties you won't get around to adding and editing a logback.xml file (assuming you're using logback).

所以,让我们来看看您的要求:

So, let's go through your requirements:

  1. 我想将消息记录在不在控制台上的文件中."

根据文档:

默认情况下,Spring Boot将仅登录到控制台,并且不会写入日志文件.如果要将日志文件另外(强调)添加到控制台输出,则需要设置logging.file或logging.path属性(例如,在application.properties中).

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition (emphasis added) to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

换句话说,无法使用属性登录到控制台.

In other words, not logging to the console cannot be done using properties.

  1. 我仅在我的application.log文件中获取信息的日志消息,但是我也希望有错误以及调试消息."

默认情况下,Spring Boot的INFO级别日志应包含ERROR,您确定不是不是使用默认设置获取ERROR日志吗?

By default Spring Boot logs on INFO level, which should include ERROR, are you sure you're not getting ERROR logs with the default setting?

此外,您仅指定要记录的最高级别,而不是每个级别,并且必须指定要设置该级别的记录器.

Also, you only specify the highest level you want to log, not each level, and you have to specify the logger you want to set the level on.

这行不通:

logging.level: DEBUG
logging.level: ERROR

这是一个示例,如何根据

This is an example how to configure custom log levels according to the docs:

logging.level.org.springframework.web:调试
logging.level.org.hibernate:错误

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

您还可以使用logging.level.*属性来设置根记录器的级别,如下所示:

You can also use the logging.level.* property to set the level of the root logger like this:

logging.level.ROOT: DEBUG

请注意,在ROOT记录器上设置DEBUG记录将生成大量日志.我刚刚在这里进行了测试,刚启动时大约有13MB的日志,没有做任何事情.

Note that setting DEBUG logging on the ROOT logger will generate a large amount of logs. I've just tested it here and got roughly 13MB of logs just on startup, without doing anything.

  1. 我想要error.log文件中的错误消息,并且debug.log中的调试消息以及info.log中的信息消息."

同样,不能仅使用属性来完成此操作. Spring Boot允许您仅配置一个logging.file属性,其中将包含所有日志.

Again, this cannot be done using properties alone. Spring Boot allows you to configure exactly one logging.file property which will contain all the logs.

有关可用的日志记录属性和示例值的完整列表,请参见

For a complete list of logging properties available and sample values see here.

这篇关于如何使用Spring Boot将日志消息写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆