Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple configurations with logback in a single project?

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.

like image 823
maaartinus Avatar asked Jul 14 '11 20:07

maaartinus


People also ask

How do I use application properties in Logback xml?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.

What is Logback configuration?

Logback executes an async appender in a separate thread to decouple the logging overhead from the thread executing your code. Using the async appender is incredibly easy. Refer the appender that should be asynchronously invoked within an <appender> element.


2 Answers

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

like image 147
jtoberon Avatar answered Sep 28 '22 09:09

jtoberon


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>   ... 
like image 45
Agustí Sánchez Avatar answered Sep 28 '22 10:09

Agustí Sánchez