Unable to turn off logging in console in logback - java

I want the console logging to turn off and file logging to turn on. Is this possible in logback?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>OFF</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "c:/jcg.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/dev-01/Documents/jcg.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>/home/dev-01/Documents/jcg-%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>100MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
I am open to use another logging libraries if this is not possible in logback.

Delete the appender-ref STDOUT reference
<root level="INFO">
<appender-ref ref="FILE" />
</root>

I don't know logback but you might set logging level to OFF for the console logger.
Like this (removing STDOUT appender from root logger) :
<logger name="consoleLogger" level="OFF">
<appender-ref ref="STDOUT" />
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
See : Can I disable an appender in logback?
EDIT : added XML sample

Related

How to log org.springframework.web.context.ContextLoader with logback

I have little problem with logging Spring bootstrap. I cant log org.springframework.web.context.ContextLoader with logback. Can someone give a point how to log Spring boot?
Here is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- load props file -->
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.prod.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.prod.properties" /></then>
</if>
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.test.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.test.properties" /></then>
</if>
<define name="propExists" class="ch.qos.logback.core.property.ResourceExistsPropertyDefiner">
<resource>META-INF/crdb.dev.properties</resource>
</define>
<if condition='${propExists}'>
<then><property resource="META-INF/crdb.dev.properties" /></then>
</if>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%X{distribute_trace_id}] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/crdb.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] [%X{distribute_trace_id}] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${catalina.home}/logs/crdb.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
<property name="APP_VERSION" value="1.2.1"/>
<!-- max size of the log file, when the log file will be bigger, then will be split -->
<property name="LOG_FILE_MAX_SIZE" value="200MB"/>
<!-- max size of logs in ALL log files -->
<property name="LOG_TOTAL_SIZE_CAP" value="1GB"/>
<!-- count of days - files will be persisted for these days -->
<property name="LOG_FILE_MAX_HISTORY" value="3"/>
<!-- Splunk Monitoring Messages -->
<appender name="SPLUNK_MONITORING" class="ch.qos.logback.core.rolling.RollingFileAppender" additivity="false">
<file>${catalina.home}/logs/splunk_monitoring/crdb.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover inferred from the file name -->
<fileNamePattern>${catalina.home}/logs/splunk_monitoring/crdb.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>
<totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="SPLUNK_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/splunk/crdb.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>${APP_VERSION}</version>
<timestampPattern>yyyy-MM-dd'T'HH:mm:ss.SSS</timestampPattern>
<customFields>{"app_name":"CRDB", "env": "${env.key}", "hostname": "${hostname}"}</customFields>
<fieldNames>
<timestamp>time</timestamp>
<version>app_version</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<exclude>net\.sf\.cglib\..*</exclude>
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover inferred from the file name -->
<fileNamePattern>${catalina.home}/logs/splunk/crdb.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>
<totalSizeCap>${LOG_TOTAL_SIZE_CAP}</totalSizeCap>
</rollingPolicy>
</appender>
<logger name="splunk_monitoring" level="INFO" additivity="false">
<appender-ref ref="SPLUNK_MONITORING"/>
</logger>
<logger name="org.springframework.web.context.*" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
<appender-ref ref="SPLUNK_FILE"/>
</root>
</configuration>
When I try this configuration, It only write to console, that
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly. Thank you.
Tried this ?
<logger name="org.springframework.web">
<level value="info" />
<appender-ref ref="console" />
</logger>

logback - Debug Level does not work

I have a JAVA EE application that uses logback (intended as a successor to the popular log4j project)
Here the logback.xml file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>telefonica.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>telefonica.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.telefonicaā€¯ level="debug" />
<logger name="org.springframework" level="debug" />
<logger name="org.springframework.web" level="debug" />
<logger name="org.springframework.security" level="debug" />
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
In 1 of the class inside the package com.telefonica I have this code
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static final Logger log = LoggerFactory.getLogger(TS.class);
log.debug ("logging to WS " + WS_VERSION);
and I don't see anything in the console, but I see it when I put log.info ("logging to WS " + WS_VERSION);
Try adding appender to your logger like below
<logger name="org.springframework" additivity="false">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
</logger>
PLease refer this question and solution
Spring : Logging not working with log4j or logback
Change your logger level to level="DEBUG" from INFO
<logger name="org.springframework" level="DEBUG"/>

Logback can't write in console

I am using Logback in my spring boot application.
The problem is logback do not print my logger messages in 'eclipse' console for my both 2 packages dao and web.
The log file is written without any problem , and print my logger messages.
I am the root so probably I should see my logger messages in my console.
logger.info("Page X INFO");
logger.debug(" Page X Debug ");
Here is my logback.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "C:/logs/Log.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:/logs/Log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>C:/logs/Log.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
<logger name="package.dao" level="DEBUG" >
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
You need to add the console appender.
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
UPDATE: I just reread the logback config doku. Actually, those two should inherit both appenders from root. So, what you can try is to not specify any appender-ref on those and see what happens. If no output is written to the file then, neither - then there is something pretty strange. I'd expect that behavior if the additivity flag was set to false. But the default is appender accumulation.

logback does not write to file

I have an app with the following logback config
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level{5} %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME}/old/app-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>14</maxHistory>
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="EXCEPTIONS" class="com.software.nagibator.emulator.logging.ExceptionsAppender">
<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
<jndiLocation>jdbc/AppExceptions</jndiLocation>
</connectionSource>
</appender>
<logger name="FileLogger" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="ExceptionsLogger" level="DEBUG" additivity="false">
<appender-ref ref="EXCEPTIONS" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I use this config on two ubuntu servers. On the first server the config works as expected (it writes the log to file). However, on the second server logback does not write the log to file.
Is there something wrong with my config or is the error elsewhere in the project?
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
...
It's look like you miss < !-- before comment in xml.

Logback - delete the log file on startup

I want to delete the log file each time the program is started as opposed to it being appended. I've tried using the cleanHistoryOnStart property but that seems to have no effect whatsoever. I'm probably missing something here.
I am on Linux and using Eclipse if that matters.
<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>chat.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>chat.log.%d{yyyy-MM-dd}</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
<Encoding>utf-8</Encoding>
</appender>
<logger name="src" additivity="false" level="ALL">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<root level="OFF">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Include <param name="Append" value="false" /> in your appender to clean the log file on start up.
Otherwise try this one
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/

Categories