3

I have setup Logback file appender programmatically and set log level into ALL. The reason was to set log level ALL i wanted to log file contains all log details like(INFO,DEBUG,WARN and etc...)But i want to avoid those details from the console.If anyone knows please let me know how can i disable Logback output to console programmatically.

Logger code snippet

Reference

private static Logger createLoggerFor(String string, String file) {
          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
          PatternLayoutEncoder ple = new PatternLayoutEncoder();

          ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
          ple.setContext(lc);
          ple.start();
          FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
          fileAppender.setFile(file);
          fileAppender.setEncoder(ple);
          fileAppender.setContext(lc);
          fileAppender.start();

          Logger logger = (Logger) LoggerFactory.getLogger(string);
          logger.addAppender(fileAppender);
          logger.setLevel(Level.ALL);
          logger.setAdditive(false); /* set to true if root should log too */

          return logger;
    }

Note:When i set log level WARN or DEBUG details doesnt show in the log file as well as console out put.But i want to log every details in to log file including INFO.

Community
  • 1
  • 1
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74

3 Answers3

5

From the docs:

Assuming the configuration files logback-test.xml or logback.xml are not present, logback will default to invoking BasicConfigurator which will set up a minimal configuration. This minimal configuration consists of a ConsoleAppender attached to the root logger.

If you want to disable all console output, you should try removing the default ConsoleAppender. According to the BasicConfigurator source, this appender's name is "console". So you could try (untested):

logger.detachAppender("console");

I highly recommend moving these programmatic configurations into a logback.xml file, and only changing the configuration programmatically when necessary. The config file is more explicit and easier to manage in my opinion.

Kyle McVay
  • 488
  • 3
  • 13
  • this didn't work.as a temporary solution i added a dummy `lockback.xml` file into my project and overwrite default behavior.My logback xml contails only following xml tag only `` this is not the elegant way to do but it did the trick. – gihan-maduranga Nov 07 '16 at 11:07
3

This works for me

logger.setAdditivity(false);
Albin
  • 1,929
  • 2
  • 14
  • 18
  • 2
    I'm not sure whether the above was ever correct or whether maybe it changed since the answer was given, but I ran into this today, and as of Logback version 1.2.3 at least, the correct form is `logger.setAdditive(false);` (and it worked for me as well) – Torque Apr 08 '21 at 11:22
  • This worked for me as well. `logger.setAdditive(false);` – pavan kumar Nov 05 '22 at 17:16
0

you can do it by removing the ConsoleAppenderfromyour logback configuration.

the log back config will be as follows.

<configuration>

<appender name="logFileAppender" class="ch.qos.logback.core.FileAppender">
<file>c:/logs/serverlog.log</file>
<append>true</append>
  <encoder>
    <pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern>
  </encoder>
</appender>

<root level="INFO">
 <appender-ref ref="logFileAppender" />
</root>

</configuration>
Jobin
  • 5,610
  • 5
  • 38
  • 53