1

Spring Boot 2.3.1 web application, using a logger (default Logback), condiguration like in https://www.baeldung.com/logback , for example:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>...</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>...</file>
    <encoder>
      <pattern>...</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
  <logger name="com.me" level="INFO">
    <appender-ref ref="FILE" /> 
  </logger> 
</configuration>

Is it possible to define two or more file appender, each with different pattern, for a logger (like "com.me")? When using in code LOGGER.info(...) to be able to choose witch appender/pattern to use?

Now it is configured, for "com.me" if LOGGER.info(...) is used in code, it will write to a file using appender "FILE". But all my LOGGER.info(...) for this package (or class level) will have the same pattern, more flexibility is needed, somewhere in the code perhaps I would like to log in log level "info" just a simple message, with a different pattern.

Can this be done via logback-spring.xml configuration or programatically? For now I use:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

But the logger doesn't have any set-methods to set something.

neblaz
  • 683
  • 10
  • 32
  • Have you checked this? [Logback to log different messages to two files](https://stackoverflow.com/questions/2488558/logback-to-log-different-messages-to-two-files) – Stalin Gino Jun 19 '20 at 06:25
  • In that post, there are two different files to which is logged (logfile.log, analytics.log). Can file appenders share the same file for logging? I want the output to be logged to the same log file and also same console. Just the pattern shall be different. – neblaz Jun 19 '20 at 08:30
  • Use a single appender but add filters according to your need. [check this thread](http://logback.10977.n7.nabble.com/Multiple-FileAppenders-writing-to-single-file-no-longer-working-in-Logback-1-2-3-td16445.html) – Stalin Gino Sep 04 '20 at 05:45

0 Answers0