0

Scenario I have:

project --> libA --> libB

project uses libA (imported in pom), libA uses libB.

Due to other constrains, in project pom I exclude everything from libA and then I import manually libB.

Both libA and libB are maintained by me but are extracted as librarie for reusability. Project and libB have as parent the sprig-boot-parent. libA is a maven project (no parent) that contains spring libraries.

If I put logback.xml file in all of them (project, libA, libB), then I receive an warning:

13:20:26,442 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.

Additional to above log, there are other lines of log that I don't desire.

If I put logback.xml in project and in libB, then I don't receive the above warning. The logs in the project and libB is formated correctly but not the one in libA

In the end I want to have the same log format for all of them without other unwanted logs.

How can I cofigure logback so that it will apply also to the library and transitive libraries ? What are the recommendations in these types of scenarios?

Later edit:

Adding logback config that is applied in the project:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>D:%d{yyyy-MM-dd HH:mm:ss.SSS} L:%p C:%c{1} F:%F\(%L\) Fn:%M T:%thread R:%X{R} - %m%n</pattern>
        </encoder>
    </appender>
    <appender name="pandaAppender" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>L:%p %m%n</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="stdout"/>
    </root>
    <logger name="com.happypanda" level="INFO" additivity="false">
        <appender-ref ref="stdout"/>
    </logger>
    <logger name="com.happypanda.myproject" level="INFO" additivity="false">
        <appender-ref ref="pandaAppender"/>
    </logger>

    <logger name="com.happypanda.libB" level="INFO" additivity="false">
        <appender-ref ref="pandaAppender"/>
    </logger>

    <logger name="org.apache.http" level="ERROR">
        <appender-ref ref="stdout"/>
    </logger>
    <logger name="org.apache.http.wire" level="ERROR">
        <appender-ref ref="stdout"/>
    </logger>

</configuration>

Later later edit:

I've tried to simplify the logic: to remove the libA and use directly libB in project.

Now the log level in libB is debug + there are additional logs from httpclient (see this as reference Disable HttpClient logging)

Later later edit:

In my application I'm taking the logback.xml from external source (is not in resources). In a static block I have the following config:

System.setProperty("logging.config", "file:/services/config/logback.xml");

For testing purposes I've moved the config log inside the resources file of the project. Now all the logs in both project and libB are ok.

I've tried to set them as command line properties but doing so I see debug logs in libB.

CMD java -XX:MinRAMPercentage=40.0 -XX:MaxRAMPercentage=80.0 -XX:+HeapDumpOnOutOfMemoryError -Dlogging.config=/services/config/logback.xml -jar
florin
  • 719
  • 12
  • 31
  • Are you using any tool which handles the dependencies and their version? For example: Maven. – sigur Feb 16 '23 at 09:25
  • You can try putting logback.xml only on `project`. remove it from the libraries. – khakiout Feb 16 '23 at 09:29
  • @sigur yes, using maven – florin Feb 16 '23 at 09:36
  • @khakiout you suggest to merge all 3 logback files into 1 and put in in the project? if yes, this I didn't try and I will give it a show. what I tried was to configure logback just for the project(without the config for libraries). but here I had a problem with httpclient (libB uses httpclient): httpclient was logging with debug level --I could see for example http handshak. I fixed this by adding logback.xml in libB. but then I said to put it also in libA for log format consistency and I dropped in the case mentioned in the post – florin Feb 16 '23 at 09:52
  • yes, please include your logback.xml for reference too. – khakiout Feb 16 '23 at 11:19
  • @khakiout I've added the configuration. please see my later later edit: the org.apache loger is added because I have debug logs in my log and I tried to fix. – florin Feb 16 '23 at 11:42

1 Answers1

0

You should create the logback file for the Project, which is using library A and library B.

In this logback configuration files, you need to handle the log which the project requires and you need: project classes and library classes.

Since you are using logback in this project, I suggest you to specify the dependency and its version in its pom.xml, so Maven is going to use the version from the POM of Project, and not "guessing libA or libB".

sigur
  • 662
  • 6
  • 21
  • can you detail more on this sentence: "In this logback configuration files, you need to handle the log which the project requires and you need: project classes and library classes." I didn't understand what should I do – florin Feb 16 '23 at 10:03