Kyle Colantonio
Kyle Colantonio

Reputation: 454

How to setup LogBack Configuration

I'm completely new to LogBack, and I'd like to use it. I notice that you can use a Configuration as an XML, but I have no idea how to implement that XML and have Logback use that instead of the default one.

My programs packages are like me.iarekylew00t.a.b and so on. It'll also be compiled into a runnable JAR at the end. How should I go about adding a logback.xml file, without the enduser needing to have it?

Sorry if this is a noob question, but I've been trying to look this up for hours and I can't find anything that clearly tells someone how to go about adding a configuration - most assume you already know how... Thanks. (please be as detailed as possible)

Upvotes: 2

Views: 2186

Answers (2)

Ivaylo Novakov
Ivaylo Novakov

Reputation: 905

I had the same issue and resolved it by copying logback.xml to src/main/java - this was the only way I found to make the executable JAR find it and use it.

Since I didn't want to have two (possibly different) config files, I kept the original logback.xml in my src/resources folder (where Eclipse would find it and use it during development) and replaced the one under src/main/java with a symlink. Works well for me.

Upvotes: 0

user180100
user180100

Reputation:

Add a logback.xml in the root of your classpath containing (tune to your needs):

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </encoder>
     </appender>

     <logger name="some.logger.name" level="INFO"/>

     <root level="DEBUG">
         <appender-ref ref="STDOUT" />
     </root>
</configuration>

and that's all.

EDIT: usual directory structure:

src/main/java/
    some/package/
                 someClass.java
    some/other/package/
                 someOtherClass.java
src/main/resources/
    logback.xml

in the jar:

some/package/
          someClass.class
some/other/package/
          someOtherClass.class
logback.xml

Upvotes: 3

Related Questions