我使用Spring Boot,希望将其日志输出到文件中。
根据文档,只需设置:
logging.file=filename.log
虽然控制台输出正常,但filename.log
未被创建。另外,如果我手动创建文件,则不会向其中写入任何内容。我错过了什么?我使用Spring Boot,希望将其日志输出到文件中。
根据文档,只需设置:
logging.file=filename.log
虽然控制台输出正常,但filename.log
未被创建。另外,如果我手动创建文件,则不会向其中写入任何内容。我错过了什么?请使用logging.file.name
代替logging.file
在spring-boot-parent(版本2.2.0及以上)
中,属性logging.file
已被废弃。
我找到了一个解决方案,但我并不是很满意,因为它仍然没有回答我的原始问题,即为什么logging.file
属性不被尊重。
我在与application.properties
同一目录下创建了logback-spring.xml
,内容来自于George的答案。根据文档,Spring Boot将从该位置选择它。然而,在我的情况下,这显然没有发生。
为了让Spring正确识别,我需要额外添加logging.config=classpath:logback-spring.xml
。现在,我application.properties
文件的相关部分如下:
logging.config=classpath:logback-spring.xml
logging.file=logs/logfile.log
(我手动创建了logs
目录。)
logback.xml
文件? - niaomingjianlogging.file
is respected for sure, depends on the fact how you specify the path, file will be created there. As a sample, in my project I have following configuration of logging in the application.yml - logging:
file: logs/dictoro-livescore-api.log
level: INFO
and it is creating log file in file system (I'm using Linux Ubuntu) in the folder /logs
- BigGinDaHouselogging.config=classpath:logback-spring.xml
。但如果您想为不同的配置文件(prod vs dev)使用不同的logback配置,则可能会有用。此外,您无需创建文件夹。 - ACV在我的情况下,我在应用程序属性文件中使用了以下内容:
logging.file
但实际上我需要使用以下内容:
logging.file.name
这样,我就可以将日志记录到指定的路径文件中了。
我遇到了同样的问题。很可能是文件系统上的文件权限导致的。我将应用程序文件夹的所有者设置为root,但"./logs"的所有者为进程所有者。因此,以下操作无法正常工作:
logging.file=my.log
但是这确实做到了
logging.file=/opt/myapp/logs/my.log
Spring Boot: 版本 2.4.3
在application.properties
文件中,应使用以下任一项:logging.file.name
或logging.file.path
例如:
logging.file.name=logs/myapp.log
logging.file.path=logs
logs
目录,它会在类路径中自动创建。~/.m2/repository/org/springframework/boot/spring-boot/2.4.3/spring-boot-2.4.3.jar!/org/springframework/boot/logging/LoggingSystemProperties.class
。logging.file.path=../tmp
- likejudo<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
logging.file=myapplication.log
application.properties
文件,使唯一与日志相关的行是logging.file=logfile.log
,这将禁用控制台日志记录。 然而,没有任何日志文件被写入。(是的,我正在使用Spring Boot 1.3.6,是的,logback配置文件位于“资源”下)。 - Christoph Möbiuslogging.config=classpath:logback-spring.xml
做到了这个。 - Christoph Möbius检查Springboot父级的版本。
如果版本是2.3.x+,则属性应为logging.file.name=yourapplog.log。
我不知道这是否对你有帮助,但我也在我的Spring-Boot
项目中使用Logback,结构如下:
文件:logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="logback.xsd">
<property resource="\application.properties"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${app.logPathPrefix}/myproject.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${app.logPathPrefix}/myproject.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO" />
<logger name="com.mycompany" level="INFO" />
<logger name="org.hibernate" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
文件名:application.properties
app.logPathPrefix=/var/log/myproject
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# log4j.appender.springlog.Threshold=INFO
log4j.appender.springlog.layout=org.apache.log4j.PatternLayout
log4j.appender.springlog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/example/filename.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
声明一个日志变量,可以像这样:
final static Logger logger = Logger.getLogger(TheClassYourIn.class);
然后在类中像这样使用:
logger.info("Well hello world then ");
这是我的做法。希望我的回答能对你有所帮助。祝你好运!
PS: log4j.appender.file.File='directory' 是指定日志存储位置的方法。如果你没有指定目录,只是以文件名.log命名,那么这个文件会自动创建在项目目录中。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="com.example.demo"
level="DEBUG" >
</logger>
</configuration>
2. 还需在 application.properties 文件中进行以下添加。
logging.config=classpath:logback-spring.xml
logging.file.name=F:/Springbootlogs/filename.log
filename.log
文件放在哪里了? - Ali Dehghani