logback-spring.xml Error: xml doesnt works with <rollingPolicy> - spring-boot

I added with 'rollingPolicy' to my project but i cant run my project after this. Its not give me any exception just stuck suddenly when i start to run it.
my logback-spring.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>-%d{-yyyy-MM-dd HH:mm:ss.SSS} -%5p %X{clientId}#%15.15t %-40.40logger{39} : %m%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
the debugger console look like
Hibernate: select alarmsenti0_.ID as ID1_0_, alarmsenti0_.ADDRESS as ADD....
Hibernate: select conveyorse0_.ID as ID1_9_, conveyorse0_.IS....
Hibernate: select sortersent0_.ID as ID1_35_, sortersent0_.HOST as HOST2_35_, sor...
Hibernate: select planmaster0_.ID as ID1_29_0_, planmaster0_.INSERT_DATE as ...
etc. no exceptions just stuck
can anyone fix this please, thank you?

Related

spring logback how to dinamically retrieve class and application name

I have the following logback-spring.xml configuration file:
<configuration scan="true">
<property name="LOG_PATH" value="/logs/application/"/>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<encoder>
<pattern>[%d{yyyy-MM-dd_HH:mm:ss.SSS}] %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 15 MegaBytes -->
<fileNamePattern>${catalina_home}/${LOG_PATH}/general-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>15MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ALL" />
</root>
</configuration>
Now, I have my springboot application "Application" and within the class "MyClass" I call:
LoggerFactory.getLogger(MyClass.class);
I want to configure the log file name by changing the static name "general" in "Application-MyClass" (whatever is the application name). Any tips?

How to define logstashencoder in yml file

Could someone please help me to convert this logback-spring.xml to logback-spring.yml ?
<appender class="ch.qos.logback.core.ConsoleAppender" name="consoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>time</timestamp>
<message>message</message>
<thread>thread</thread>
</fieldNames>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<exclude>org\.springframework\..*</exclude>
<inlineHash>true</inlineHash>
</throwableConverter>
</encoder>
</appender>

Logback TimeBasedRollingPolicy not saving the log with the desired name of the log file

I want to configure TimeBasedRollingPolicy in my spring boot project. I want the log file to be saved in a pattern 'mylog.%d{yyyy-MM-dd}.log'.
My logback-spring.xml file looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p{${LOG_LEVEL_PATTERN}} %15.15t - %-80.80c{1.} %X{username} %X{ipaddress} : %msg%n</pattern>
</encoder>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/mylog.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p{${LOG_LEVEL_PATTERN}} %15.15t - %-80.80c{1.} %X{username} %X{ipaddress} : %msg%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/mylog.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</configuration>
But it's not working, the log file is created but it is not titled mylog but the default 'localhost_access_log.2022-05-16'. Can someone please help me with this?
Declare timestamp separately and refer it inside your fileNamePattern
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<timestamp key="timestamp" datePattern="yyyy-MM-dd"/>
// place all your appenders
<appender name="RollingFile"
.
.
.
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/mylog.${timestamp}.log</fileNamePattern>
</rollingPolicy>
</appender>
</configuration>

How to show sql Queries in console in springboot azure (Springboot + Azure Cosmos)

i am using SpringBoot with azurecosmos db
i want to see the query for below repository code.
findByFirstNameAndLastName(String firstName,String lastName);
For me adding following configuration to application.yaml worked.
logging:
config: classpath:logback-local-spring.xml
level:
com.azure.cosmos.implementation.SqlQuerySpecLogger: debug
You can try adding the below lines inside application.properties to get all the query generated in console
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
logging.level.org.hibernate.SQL = DEBUG
logging.level.org.hibernate.type = TRACE
logging.level.org.springframework.web = DEBUG
You can use also logback. Here is one example:
<configuration>
<include resource="/org/springframework/boot/logging/logback/base.xml"/>
<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="STDOUT"/>
</root>
<logger name="com.azure.cosmos" level="error"/>
<logger name="org.springframework" level="error"/>
<logger name="io.netty" level="error"/>
<logger name="com.azure.cosmos.implementation.SqlQuerySpecLogger" level="DEBUG"/>

Spring Boot logging pattern

I have a problem with configuration on Logback in a Spring Boot application. I want my consoleAppender to look like the default Spring Boot console appender. How to inherit pattern from Spring Boot default console appender?
Below is my consoleAppender configuration
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern class="org.">
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
Once you have included the default configuration, you can use its values in your own logback-spring.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
…
</configuration>
You can find Spring Boot logback console logging pattern in defaults.xml file:
spring-boot-1.5.0.RELEASE.jar/org/springframework/boot/logging/logback/defaults.xml
Console pattern:
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
</Pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
If you are using application.yml for your config, you can set the logging pattern this way:
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} | %-5level | %logger{1.} | %msg%n"
level:
org.springframework: WARN
com.ulisesbocchio.jasyptspringboot: WARN
com.example.test: DEBUG
You can override the logging level on the command line. For example:
$ java -Dlogging.level.com.example.test=TRACE -jar my-example.jar
It's been some time since this question was asked but since I had the problem myself recently and couldn't find an answer I started digging a bit deeper and found a solution that worked for me.
I ended up using the debugger and take a look at the default appenders attached to the logger.
I found this pattern to be working as desired for me:
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p 18737 --- [%t] %-40.40logger{39} : %m%n%wEx</pattern>
EDIT: The pattern is not entirely correct, I saw that runtime some values had already been instantiated (in this case 18737 ---) i will look into the proper variable to substitute there. It does contain the format for fixed length columns though
EDIT 2: Ok, I took another look at the debugger contents. This you can also do yourself by looking at the contents of a logger instance:
Debugger(eclipse) Logger Contents
So I ended up using the pattern used in the consoleAppender:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(18971){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
As can be seen here:
Debugger: detailed contents of the encoder pattern
Logging pattern can be configured using application.properties file
Example :
# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
You can use below pattern :
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40logger{1.} : %m%n%wEx
Note that you can also customize the imported properties.
But beware that at least with spring boot 1.4.3 if you want to customize the properties imported from the defaults.xml, then the customization should be placed BEFORE the include.
For example this customizes the priority to 100 character wide:
<configuration scan="true">
<property name="LOG_LEVEL_PATTERN" value="%100p" />
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
</layout>
</appender>
<logger name="hu" level="debug" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<root level="warn">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
But this is NOT:
<configuration scan="true">
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_LEVEL_PATTERN" value="%100p" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
</layout>
</appender>
<logger name="hu" level="debug" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<root level="warn">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
For those who'd like to use Łukasz Frankowski's answer (which looks like the cleanest solution here), but in a groovy version, the "problematic" {$PID:- } part can be expanded like in the following:
logback-spring.groovy
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.core.ConsoleAppender
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
import static ch.qos.logback.classic.Level.INFO
conversionRule("clr", ColorConverter)
conversionRule("wex", WhitespaceThrowableProxyConverter)
conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter)
appender("STDOUT", ConsoleAppender) {
layout(PatternLayout) {
def PID = System.getProperty("PID") ?: ''
pattern = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
}
}
root(INFO, ["STDOUT"])
This worked for me, adding following line to resources/log4j2.properties file
appender.console.layout.pattern = %d{ISO8601} - info: %msg%n ( your custom pattern goes here )
The spring documentation has an example of the logback.xml that defines the default.
<?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="org.springframework.web" level="DEBUG"/>
</configuration>

Resources