Home » Java » logging – Where to save logback.xml file for use of Logger slf4j in Java-Exceptionshub

logging – Where to save logback.xml file for use of Logger slf4j in Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a question regarding the location where I should save my logback.xml file so that my program can find it. I now saved it in src/main/resources, but it cannot find it. I know this since

    String URL = "logback.xml";
    System.out.println(ClassLoader.getSystemResource(URL));

returns null.

The logger does work, but prints all information to the command line

null
13:05:26.606 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 18
13:05:26.613 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 479.0
13:05:26.666 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 113
13:05:26.668 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 272.0
13:05:26.693 [main] DEBUG o.j.f.c.master.AbstractMaster - Number of columns: 118
13:05:26.694 [main] DEBUG o.j.f.c.master.AbstractMaster - Objective value: 238.0
13:05:26.721 [main] INFO  o.j.f.c.master.AbstractMaster - Best objective value found: 238.0
13:05:26.721 [main] INFO  o.j.f.c.master.AbstractMaster - (d1-s2-s2-d1) (s2-c1-s2) (s2-c3-c2- 
s2);238.0 chosen with value: 1.0 resulting in cost: 238.0

The content of my logback.xml file is

<configuration scan="true" info="true">
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <file>myApp.log</file>
  <append>false</append>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <pattern>%msg%n</pattern>
    </layout>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <logger name="algorithms.MultiLabel.cg.master" level="OFF"/>

  <root level="debug"> <!-- levels: OFF, info, debug -->
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Next I make some changes to the logback.xml file and again save it in the src/main/resources folder. If I now run my program again, I suddenly get the following output.

file:./target/classes/logback.xml
13:06:41,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:06:41,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:06:41,632 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:./target/classes/logback.xml]
13:06:41,715 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:06:41,720 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[.\target\classes\logback.xml]] every 60 seconds. 
13:06:41,720 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:06:41,731 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
13:06:41,733 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
13:06:41,783 |-WARN in ch.qos.logback.core.FileAppender[FILE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
13:06:41,783 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [myApp.log]
13:06:41,787 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:06:41,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:06:41,790 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:06:41,797 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [algorithms] to OFF
13:06:41,797 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
13:06:41,797 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
13:06:41,798 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:06:41,798 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:06:41,800 |-INFO in [email protected] - Registering current configuration as safe fallback point

Best objective value found: 238.0
(d1-s2-s2-d1) (s2-c1-s2) (s2-c3-c2-s2);238.0 chosen with value: 1.0 resulting in cost: 238.0
  • Why does it now find the file in the folder ./target (where the src folder also is located), even though I did not save it there?
  • All information that it is printing is coming from the master package, which I set to “OFF”, so how come it is still printing it?

I am looking forward to any suggestions/explanations!

How to&Answers: