英文:
How to log to console or file for different profiles with jar execution?
问题
我正在使用Spring Boot 2.3.1
版本,结合Lombok日志记录 - @Slf4j 注解。
我有两个配置文件:dev
和 default
。使用相应的 application.yml
和 application-dev.yml
配置文件。
最终,该项目使用Maven构建,并在AWS的 EC-2 Windows实例 上运行 最终的jar 文件。
目前,没有配置任何日志文件,因此jar执行看起来像这样:
java -jar -DmyProp=myVal ... myJar.jar > log_file.log &
它在后台启动程序执行,所有输出都重定向到日志文件。
这不如预期的那么好和可配置。
因此,现在想要添加不同的日志行为:
dev
配置 -> 仅记录到控制台default
配置 -> 使用滚动文件 appender 记录到文件
创建了以下 logback-spring.xml
文件并将其放置在资源文件夹中:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/>
<!-- 使用Spring默认值 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/my_log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH-mm-ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${LOGS}/archived/my_log-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>300MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="dev">
<root level="info">
<appender-ref ref="Console"/>
</root>
<logger name="com.siemens" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
</logger>
</springProfile>
<springProfile name="!dev">
<root level="info">
<appender-ref ref="RollingFile"/>
</root>
<logger name="com.demo" level="DEBUG" additivity="false">
<appender-ref ref="RollingFile"/>
</logger>
</springProfile>
</configuration>
当我使用先前提到的Maven命令运行jar文件时,我没有任何记录到文件的日志。尽管我以后台模式启动它,但所有输出都记录到控制台。
我尝试在 application.yml
中使用附加配置:
logging:
level:
name: my_log.log
path: C:\Users\user\IdeaProjects\my-service\logs
然而,结果还是一样的。
如何为不同的配置创建不同的日志策略,并在jar启动时使用它?
英文:
I am using Spring Boot 2.3.1
with Lombok logging - @Slf4j annotation.
I have two profiles: dev
and default
. With appropriate application.yml
& application-dev.yml
config files.
Finally, the project is built with maven and the final jar file is launched in AWS EC-2 Windows instance.
Currently, there aren't any log files configured, so jar execution looks like:
>java -jar -DmyProp=myVal ... myJar.jar > log_file.log &
It starts program execution at the background and all output is redirected to log file.
It isn't so good and configurable as it should be.
So, for now, want to add different log behaviours:
- dev profile -> log to console only
- default -> log to file with Rolling file appender
Created following logback-spring.xml
and put it to resources folder:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/>
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/my_log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH-mm-ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${LOGS}/archived/my_log-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>300MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="dev">
<root level="info">
<appender-ref ref="Console"/>
</root>
<logger name="com.siemens" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
</logger>
</springProfile>
<springProfile name="!dev">
<root level="info">
<appender-ref ref="RollingFile"/>
</root>
<logger name="com.demo" level="DEBUG" additivity="false">
<appender-ref ref="RollingFile"/>
</logger>
</springProfile>
</configuration>
When I run the jar file with previously mentioned Maven command I don't have any logging to file. Everything is logged to console despite the fact that I launched it at background mode.
I tried to use additional configuration for application.yml
:
logging:
level:
name: my_log.log
path: C:\Users\user\IdeaProjects\my-service\logs
However, the result is the same.
How to create different log strategies for different profiles and use it with jar launching?
答案1
得分: 1
在玩了一会儿后找到了一个解决方案。
更新 application.yml
:
spring:
main:
banner_mode: OFF
删除类似以下的配置:
logging:
level:
name: my_log.log
path: C:\Users\user\IdeaProjects\my-service\logs
Logback 配置文件是正确的,应该保持不变。
现在,当你启动应用程序时:
java -jar -DmyProp=myVal ... myJar.jar
就是这样。它会启动并且所有的输出都会被记录到日志文件中。
如果你想要再次启用横幅 -> 它将会被打印到控制台输出,并与其一起挂起。所以它必须被禁用。
英文:
After play around for a while found a solution.
Update application.yml
:
spring:
main:
banner_mode: OFF
Eliminate configs like:
logging:
level:
name: my_log.log
path: C:\Users\user\IdeaProjects\my-service\logs
Logback config file is correct and should be the same.
And now when you could launch your app:
>java -jar -DmyProp=myVal ... myJar.jar &
That's it. It will launch and all output will be logged to the log file only.
If you will enable banner again -> it will be printed at the console output and hang up with it.
So it must be disabled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论