如何在使用JAR执行时针对不同配置文件将日志输出到控制台或文件中?

huangapple go评论61阅读模式
英文:

How to log to console or file for different profiles with jar execution?

问题

我正在使用Spring Boot 2.3.1 版本,结合Lombok日志记录 - @Slf4j 注解。

我有两个配置文件:devdefault。使用相应的 application.ymlapplication-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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
&lt;property name=&quot;LOGS&quot; value=&quot;./logs&quot;/&gt;
&lt;!-- use Spring default values --&gt;
&lt;include resource=&quot;org/springframework/boot/logging/logback/defaults.xml&quot;/&gt;
&lt;appender name=&quot;Console&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
&lt;encoder&gt;
&lt;pattern&gt;${CONSOLE_LOG_PATTERN}&lt;/pattern&gt;
&lt;charset&gt;UTF-8&lt;/charset&gt;
&lt;/encoder&gt;
&lt;/appender&gt;
&lt;appender name=&quot;RollingFile&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
&lt;file&gt;${LOGS}/my_log.log&lt;/file&gt;
&lt;encoder class=&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;&gt;
&lt;pattern&gt;%d{yyyy-MM-dd HH-mm-ss.SSS} [%thread] %-5level %logger{36} - %msg%n&lt;/pattern&gt;
&lt;charset&gt;UTF-8&lt;/charset&gt;
&lt;/encoder&gt;
&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;
&lt;fileNamePattern&gt;
${LOGS}/archived/my_log-%d{yyyy-MM-dd}.%i.log
&lt;/fileNamePattern&gt;
&lt;timeBasedFileNamingAndTriggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP&quot;&gt;
&lt;maxFileSize&gt;300MB&lt;/maxFileSize&gt;
&lt;/timeBasedFileNamingAndTriggeringPolicy&gt;
&lt;maxHistory&gt;30&lt;/maxHistory&gt;
&lt;totalSizeCap&gt;3GB&lt;/totalSizeCap&gt;
&lt;/rollingPolicy&gt;
&lt;/appender&gt;
&lt;springProfile name=&quot;dev&quot;&gt;
&lt;root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;Console&quot;/&gt;
&lt;/root&gt;
&lt;logger name=&quot;com.siemens&quot; level=&quot;DEBUG&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;Console&quot;/&gt;
&lt;/logger&gt;
&lt;/springProfile&gt;
&lt;springProfile name=&quot;!dev&quot;&gt;
&lt;root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;RollingFile&quot;/&gt;
&lt;/root&gt;
&lt;logger name=&quot;com.demo&quot; level=&quot;DEBUG&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;RollingFile&quot;/&gt;
&lt;/logger&gt;
&lt;/springProfile&gt;
&lt;/configuration&gt;

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.

huangapple
  • 本文由 发表于 2020年9月27日 04:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64082409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定