console pattern not recognized in logback-spring.xml - spring-boot

I already looked up on stackoverflow but I did not find something comparable.
I use spring boot with following dependencies:
spring-boot 2.2.1 with
spring-boot-starter-logging 2.2.1
logback-core 1.2.3
logback-classic 1.2.3
I want to use a custom pattern for my log output to print my mdc.variables on console. Which works via application.properties
logging.level.com.mypackage=TRACE
logging.pattern.level=%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable}] %-5level %logger{36} - %m%n
But I get double entries but I can not set the additivity property via application.properties. I have to use logback-spring.xml. Which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.mypackage" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable}] %-5level %logger{36} - %m%n
</Pattern>
</encoder>
</appender>
</configuration>
This solves the double entries. But the pattern is not use for the logoutput. I can have either double entries or no pattern because it is not possible to combine configuration via application.properties and logback-spring.xml. What am I doing wrong?

I finally found out what is wrong. I checked the defaults.xml and the ConsoleAppender.xml on github e.g.
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml
The solution was to define the constant CONSOLE_LOG_PATTERN. And to define it before importing the defaults.xml and console-appender.xml
My working config file looks now like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable] %-5level %logger{36} - %m%n"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.mypackage" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
${CONSOLE_LOG_PATTERN}
</pattern>
</encoder>
</appender>
</configuration>

Related

How do I disable logging from javers?

Currently working in a large spring-boot application, it would be preferred to disable logging if possible to reduce noise.
sample of the logs
NFO org.javers.core.JaversBuilder mappingStyle: FIELD
NFO org.javers.core.JaversBuilder loading GroovyAddOns ...
NFO org.javers.core.JaversBuilder using fake InMemoryRepository, register actual Repository implementation via JaversBuilder.registerJaversRepository()
NFO org.javers.core.JaversBuilder JaVers instance started in 116 ms
How do I disable logging from javers?
You can set log level for Javers in logback.xml like this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.javers.SQL" level="DEBUG"/>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
With this configuration you should only see DEBUG log messages displayed in pattern above.
If you want to completly disable logging you can use this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.javers.SQL" level="OFF"/>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
The instruction: level="OFF" tells Logback to disable all log output for a given logger, in your case that is javers logger.

No logging in external tomcat with spring boot application

I am trying to log the spring boot applications logs to a separate directory(apps-archive ie ${catalina.base}/logs/apps-archive/log.log) in the external tomcat. After I start the server the log file is empty, and there are some logs in catalina.2021-04-04.log file about application is deployed and so...
In my spring boot application I have logback-spring.xml in src/main/resources as
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d [%thread] %-5level %-50logger{40} : %msg%n
</pattern>
</encoder>
</appender>
<property name="LOG_PATH" value="${catalina.base}/logs/apps-archive"/>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d [%thread] %-5level %-50logger{40} : %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${LOG_PATH}/log_%d{dd-MM-yyyy}_%i.log
</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>400MB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<!-- Hibernate loggers -->
<logger level="DEBUG" name="org.hibernate"/>
<logger level="INFO" name="com.commerzbank.eudsgvo"/>
<logger level="DEBUG" name="org.hibernate.type.descriptor.sql"/>
<logger level="DEBUG" name="org.springframework"/>
</springProfile>
</configuration>
I have no configuration for logging in application.properties file. Can anyone please suggest me why logs file is empty in apps-archive directory on server.
The default profile spring boot apps run with is default.
Can you please change
<springProfile name="dev">
to
<springProfile name="default">
Regarding the issues with the RollingPolicy I don't have any ideas at the moment.

Logback I want File do not create when the appender is never used. [RollingFileAppender]

I'm creating a logback-common config file for my applis.
I'm defining a RollingFileAppender in it, that for all my applis generates the same log format in a file (if we need it).
Sometimes I want to use this appender, and sometimes not (when we test for example).
So we configure our specific logback-{profile}.xml depending what we want.
But when we do not use the FILE appender, the file gets created and I would like not.
I have:
logback-common.xml >> with all appenders definition (FILE and COMMON)
appli_one
resources/logback.xml >> call logback-common and config/logback-{profile}.xml
resources/config/logback-{profile}.xml >> specific appli/profile logback configuration.
To configure we can do in logback-{profile}.xml
<root level="WARN">
<appender-ref ref="FILE"/> <!-- For File Log when we need it -->
<appender-ref ref="CONSOLE"/>
</root>
<root level="WARN">
<!-- <appender-ref ref="FILE"/> --> <!-- in comment when we do not need if > BUT create a empty file -->
<appender-ref ref="CONSOLE"/>
</root>
logback-common.xml
<included>
<!-- The FILE and ASYNC appenders are here as examples for a production
configuration -->
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/${spring.application.name}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
log/${spring.application.name}.%d{yyyy-MM-dd}.%i.log.zip
</fileNamePattern>
<maxHistory>90</maxHistory>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>"%d{yyyy-MM-dd} [%thread] %-5level %45logger{45} - %msg%n"</pattern>
</encoder>
</appender>
</included>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" packagingData="true">
<property name="spring_profiles_active" value="${spring.profiles.active}" />
<property resource="application.properties"/>
<include resource="config/log/logback-common.xml"/>
<include resource="config/log/logback-${spring_profiles_active}.xml"/>
</configuration>
Not a core feature of logback but there are workarounds to achieve lazy file initialization.
See more here
Logback - do not create empty log files at startup

Spring boot startup logs to file

Is it anyway to send startup logs to file, right now all logs up to the statement "Started Application in...." goes to stdout, I want all logging to file.
My logback config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="logs/app${PID}.log"/>
<appender name="AI-APPENDER"
class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="AI-APPENDER"/>
</root>
</configuration>
You need to configure your logging framework. Assuming you're just using the default from spring-boot then that's LogBack. So have a look at their docs or have a search, there are many useful resources (like this one)
If you add a logback.xml file with the following content to your resources folder you should get logging to both console and file (called application.log) in the same format as you now see for just console.
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>application.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
You probably want to use a RollingFileAppender as this will allow you to create new files when the log file gets big.

Hazelcast logging doesn't work

We followed all the posts and documentation out there but can't get any DEBUG statements generated by Hazelcast.
Hazelcast v.3.4.1 Tomcat 7 application server. Slf4j v.1.7.5
Hazelcast config:
<properties>
<property name="hazelcast.logging.type">slf4j</property>
</properties>
Logback.xml
EDIT: Here is a full logback.xml
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{0} - [%X{sessionID}] - [%X{loginID}] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>2</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - [%X{sessionID}] - [%X{loginID}] - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.mybatis" level="debug" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="com.hazelcast" level="debug" additivity="false">
<appender-ref ref="FILE" />
</logger>
<root level="warn">
<appender-ref ref="FILE" />
</root>
Seems like something else is needed. No hazelcast logging is visible.
I would appreciate some help.
Do you see somethng like this in the logging?
INFO: [192.168.122.1]:5701 [dev] [3.5.1]
Members [1] {
Member [192.168.122.1]:5701 this
}
Because this is a clear indication that the Hazelcast logging is working correctly. If you don't see this, then there really is a logging problem, otherwise there probably is some kind of log level problem.

Resources