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.
Related
I am using Logback for logging. Now all logs are writing into one file, and it becomes very large. How to organize logging in such a way that a new file is created at the beginning of a new day?
I use such logback.xml
<configuration>
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n" />
<property name="APP_LOG_ROOT" value="admin-logs"/>
<timestamp key="year" datePattern="yyyy" />
<timestamp key="month" datePattern="MM" />
<timestamp key="date" datePattern="dd" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger level="INFO" name="my.name">
<appender-ref ref="STDOUT" />
</logger>
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>
all logs are writing into one file
The shown config is going to the STDOUT, not a file.
If you want to write to a daily file, use logrotate/cron process, or have logback do this itself.
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_DIR}/bim.%d{yyyy-MM-dd}.log</fileNamePattern>
...
</appender>
...
<logger level="INFO" name="my.name">
<appender-ref ref="file" />
</logger>
<root level="ERROR">
<appender-ref ref="file" />
</root>
I'm having a strange issue where log4j is correctly displaying all my log messages the console, but is only sending log messages to my graylog server that I am not writing (e.g. a library I'm using which writes to log.info will show up in graylog, but not any of the messages I write with log.info). What am I missing
Here's my logback-spring.xml
<configuration>
<contextName>test</contextName>
<jmxConfigurator/>
<springProfile name="dev, test">
<appender name="gelf" class="biz.paluch.logging.gelf.logback.GelfLogbackAppender">
<host>udp:localhost</host>
<port>5555</port>
<version>1.1</version>
<extractStackTrace>true</extractStackTrace>
<filterStackTrace>true</filterStackTrace>
<mdcProfiling>true</mdcProfiling>
<timestampPattern>yyyy-MM-dd HH:mm:ss,SSS</timestampPattern>
<maximumMessageSize>8192</maximumMessageSize>
<includeFullMdc>true</includeFullMdc>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
</springProfile>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="com.test" level="info" additivity="false">
<appender-ref ref="stdout" />
</logger>
<root level="INFO">
<appender-ref ref="gelf" />
<appender-ref ref="stdout" />
</root>
</configuration>
I'm instantiating my log by doing:
private static final Logger logger = LogManager.getLogger(MyClass.class);
And writing to my log:
logger.info("WHY CAN'T I SEE THIS IN MY GRAYLOG SERVER");
Edit:
Your are specifying that the loggers of the package com.test should only write to the stdout appender.
<logger name="com.test" level="info" additivity="false">
<appender-ref ref="stdout" />
</logger>
You should change it to:
<logger name="com.test" level="info" />
Make sure that you are running your app with the matching profile for your gelf appender.
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"/>
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
I am having following xml config for log4j with 3 appenders. And want to configure logger the following way:
All messages more than INFO are written to STDOUT and global log FILE appenders
But for some class Oauth I want to have extra log file OAUTHFILE
Some classes are filtered and write messages to LOG only on error level this can be achieved by
<logger name="application" level="ERROR" />
some classes are filtered and write messages to its own log file. This is also solved with additivity equal false flag.
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILE" />
</logger>
But how can I solve the 2nd problem. The folowing configuration produces debug and error messages in STDOUT and! in OAUTHFILE
<configuration>
...
<appender name="STDOUT" ...></appender>
<appender name="FILE" ...>
</appender>
<appender name="OAUTHFILE" ...>
</appender>
...
<logger name="controllers.OAuth" level="DEBUG" additivity="false">
<appender-ref ref="OAUTHFILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Actually I have found the solution for second task. I should make 2 STDOUT and FILE appenders - one for general logging and the second one with filtering
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
<appender name="STDOUTERR" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
And use the second as extra appender-ref for classes that should produce error log to general stdout like here:
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILEAPPENDER" />
<appender-ref ref="STDOUTERR" />
</logger>