如何在单个项目中使用多个配置和logback? [英] How to use multiple configurations with logback in a single project?

查看:228
本文介绍了如何在单个项目中使用多个配置和logback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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

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

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


  1. 您的appender可以按如下方式设置文件:

    < file> / var / tmp / $ {mycompany.myapplication} .log< / file>

  2. 然后你可以在启动java时指定该变量的值:
    $
    -Dmycompany.myapplication = SomeUtility

  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

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

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


  1. 将此信息放入您的logback配置文件中:

    < logger name =com.mycompanylevel =$ {mycompany.logging.level:-DEBUG}/>

    这会导致指定的包以DEBUG级别登录默认情况下。

  2. 如果要在特定应用程序中将日志记录级别更改为INFO,请在启动该应用程序时将以下内容传递给java:

    -Dmycompany.logging.level = INFO

  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

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

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. 将此放入您的logback配置文件,无论您将< appender-ref> <放在哪里/ code>,将 ref 值更改为您自己的< appender> 中的一个,当然:
    $

    < if condition =property(mycompany.logging.console)。equalsIgnoreCase(true)>
    < then>< appender-ref ref =STDOUT/>< / then>< / if>

  2. 如果要启用此appender,请在启动该应用程序时将以下内容传递给java:

    -Dmycompany.logging.console = true

  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

关于系统属性,你传递它们java作为 -D 参数,例如

java -Dmy.property = / path / to / config.xml com .mycompany.MyMain

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

这篇关于如何在单个项目中使用多个配置和logback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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