如何在单个项目中使用带有 logback 的多个配置?

发布于 2024-11-24 02:51:22 字数 186 浏览 0 评论 0 原文

logback 的配置文件在类路径中找到,因此是特定于 Eclipse 项目的,这不是我想要的。我正在使用多个 Java 实用程序,它们全部驻留在一个项目中(共享类路径),并且我需要对其中一些实用程序使用特定的配置。

我尝试过变量替换和 Joram 配置器,但没有任何效果。这很可能是我的错,有一天我会解决它,但现在我需要一个简单的解决方案。

The configuration file for logback gets found on the classpath, and is therefore Eclipse-project-specific, which is not what I want. I'm using multiple Java utilities, all of them residing in a single project (this sharing the classpath), and I need to use a specific configuration for some of them.

I've tried variable substitution and Joram configurator, but nothing worked for me. This was most probably my fault, and I'm going to solve it one day, but for now I'd need a simple solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂人 2024-12-01 02:51:22

选项 1:使用 logback.configurationFile 系统属性指定 logback 配置文件的位置。事实上,这确实允许您每个项目拥有多个配置文件。根据 logback 文档,此属性的值可以是 URL,类路径上的资源或应用程序外部文件的路径。例如:
-Dlogback.configurationFile=/path/to/config.xml

选项 2:使用变量替换通过系统属性设置日志文件的名称。例如:

  1. 您的附加程序可以按如下方式设置文件:
    /var/tmp/${mycompany.myapplication}.log
  2. 然后您可以在启动 java 时指定该变量的值:
    -Dmycompany.myapplication=SomeUtility

选项 3:使用系统属性设置记录器级别。这将允许您记录更多/更少。例如:

  1. 将其放入您的 logback 配置文件中:

    这会导致指定的包默认以 DEBUG 级别记录。
  2. 如果您想在特定应用程序中将日志记录级别更改为 INFO,请在启动该应用程序时将以下内容传递给 java:
    -Dmycompany.logging.level=INFO

选项 4:通过将系统属性命令行参数传递给 java 来添加/删除附加程序。这将允许您登录到不同的地方。请注意,条件处理需要 janino。例如:

  1. 将其放入您要放置 的 logback 配置文件中,将 ref 值更改为您自己的 值之一。当然是appender>
    <代码>
    ;
  2. 如果您想启用此附加程序,请在启动该应用程序时将以下内容传递给 java:
    -Dmycompany.logging.console=true

关于系统属性,您将它们作为 -D 参数传递给 java,例如
java -Dmy.property=/path/to/config.xml com.mycompany.MyMain

OPTION 1: specify the location of the logback configuration file with the logback.configurationFile system property. This does in fact allow you to have multiple configuration files per project. As per the logback documentation, the value of the this property can be a URL, a resource on the class path or a path to a file external to the application. For example:
-Dlogback.configurationFile=/path/to/config.xml

OPTION 2: use variable substitution to set the name of the log file with a system property. For example:

  1. Your appender can set the file as follows:
    <file>/var/tmp/${mycompany.myapplication}.log</file>
  2. And then you can specify the value of that variable when launching java:
    -Dmycompany.myapplication=SomeUtility

OPTION 3: set the logger level with a system property. This will allow you to log more/less. For example:

  1. Put this into your logback config file:
    <logger name="com.mycompany" level="${mycompany.logging.level:-DEBUG}"/>
    This causes the specified package to log at DEBUG level by default.
  2. If you want to change the logging level to INFO in a specific application, then pass the following to java when launching that application:
    -Dmycompany.logging.level=INFO

OPTION 4: add/remove an appender by passing a system property command-line parameter to java. This will allow you to log to different places. Note that conditional processing requires janino. For example:

  1. Put this into your logback config file wherever you would put an <appender-ref>, changing the ref value to one of your own <appender>s, of course:

    <if condition="property("mycompany.logging.console").equalsIgnoreCase("true")">
    <then><appender-ref ref="STDOUT"/></then></if>
  2. If you want to enable this appender, then pass the following to java when launching that application:
    -Dmycompany.logging.console=true

Regarding system properties, you pass them to java as -D arguments, e.g.
java -Dmy.property=/path/to/config.xml com.mycompany.MyMain

他夏了夏天 2024-12-01 02:51:22

在 Spring Boot 应用程序中,您可以在 logback 配置文件中引用 Spring Profiles。

请参阅本文

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <springProfile name="dev">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>
          %d{HH:mm:ss.SSS} | %5p | %logger{25} | %m%n
        </pattern>
        <charset>utf8</charset>
      </encoder>
    </appender>
    <root level="DEBUG">
      <appender-ref ref="CONSOLE"/>
    </root>
  </springProfile>
  ...

In a Spring Boot application, you can reference Spring Profiles inside logback configuration file.

See this article.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <springProfile name="dev">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>
          %d{HH:mm:ss.SSS} | %5p | %logger{25} | %m%n
        </pattern>
        <charset>utf8</charset>
      </encoder>
    </appender>
    <root level="DEBUG">
      <appender-ref ref="CONSOLE"/>
    </root>
  </springProfile>
  ...
阳光下的泡沫是彩色的 2024-12-01 02:51:22

我使用了基于 列奥尼达博客。有两个文件:

  • 包含环境属性
  • 和自定义配置(例如logback-env-test.xml)的可选属性文件(environment.properties)。所有这些文件都必须位于类路径上。

如果属性文件存在并定义了 logEnv 属性,例如

logEnv = dev66

logback 会尝试查找并包含 logback-env-dev66.xml 中的自定义配置

<included>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</included>

,否则它将回退到默认值( 部分)配置。请注意,自定义配置文件中使用的是 标签,而不是

logback.xml 来管理上述所有内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds" debug="true">
    <!-- To skip error if property file doesn't exist -->
    <define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
        <resource>environment.properties</resource>
    </define>
    <if condition='${propExists}'>
        <then>
            <property resource="environment.properties" />
        </then>
    </if>
    <!-- If specific configuration exists, load it otherwise fallback to default (<else> section)  -->
    <define name="confExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
        <resource>logback-env-${logEnv}.xml</resource>
    </define>
    <if condition='${confExists}'>
        <then>
            <include resource="logback-env-${logEnv}.xml"/>
        </then>
        <else>
            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
            <root level="INFO">
                <appender-ref ref="STDOUT" />
            </root>
        </else>
    </if>
</configuration>

它将允许您对所有环境进行单独配置,定义自己的自定义配置(例如本地开发)而不影响其他配置。

I have used another option based on Leonidas blog. There are two files:

  • the optional property file (environment.properties) that contains the environment property
  • and custom configurations (e.g. logback-env-test.xml). All these files have to be on the classpath.

If the property file exists and defines logEnv property e.g.

logEnv = dev66

the logback tries to find and include the custom configuration from logback-env-dev66.xml

<included>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</included>

Overwise it will be falback to default (<else> section) configuration. Please, note the <included> tag are using instead of <configuration> in custom configuration files.

the logback.xml to manage all the above things:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds" debug="true">
    <!-- To skip error if property file doesn't exist -->
    <define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
        <resource>environment.properties</resource>
    </define>
    <if condition='${propExists}'>
        <then>
            <property resource="environment.properties" />
        </then>
    </if>
    <!-- If specific configuration exists, load it otherwise fallback to default (<else> section)  -->
    <define name="confExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
        <resource>logback-env-${logEnv}.xml</resource>
    </define>
    <if condition='${confExists}'>
        <then>
            <include resource="logback-env-${logEnv}.xml"/>
        </then>
        <else>
            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
            <root level="INFO">
                <appender-ref ref="STDOUT" />
            </root>
        </else>
    </if>
</configuration>

It will allow you to have separate configuration for all environments, define own custom configuration (e.g. local development) without influence on others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文