How to disable time and date info in console logs Spring - spring

How to disable time and date for console logs ? Is there any way ? Screen is not wide enough and this takes unnecessary place . Really annoys me

If you are using spring-boot, then by default it is using logback as it's logging mechanism. My suggestion is for you to do changes via logback.xml file in-order to change the log pattern.
It is a easy fix and create logback.xml file in your project's resources directory.
I will mention below a sample xml for that. You can use my example and it will solve your issue.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Appender for logging within the console -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- Pattern of the console logging -->
<!-- %d - Date and time -->
<!-- %thread - Thread name -->
<!-- %level - Log level (%p also works) -->
<!-- %logger - Logger name -->
<!-- %msg - Log message (%m also works) -->
<!-- %n - Line separator -->
<!-- %d{yyyy-MMM-dd HH:mm:ss} - Custom Date/Time format -->
<!-- %-5level - Left justification flag - Use spaces for right padding if characters < 5 -->
<!-- %logger{36} - Abbreviate -->
<!-- [Date and time] [Thread] LoggingLevel : LoggerName - Logging message -->
<pattern>[%thread] %-5level: %logger - %msg%n</pattern>
</encoder>
</appender>
<!-- Logging levels are, in order of precedence -> TRACE, DEBUG, INFO, WARN and ERROR -->
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Above logback.xml contains more comments for better understanding. You can control the pattern of your logs as you wish via modifying the <pattern>.
Hope this solves the issue.

Related

Spring sleuth with SpringBoot (log correlation) - Traceid & SpanId Not displayed

I have an existing Spring boot (2.2.0) application and trying to integrate Spring cloud sleuth to get automatic trace and span id.
The logback file is as follows -
<configuration>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 100 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at specified level level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
This config for logback does not log/display traceId as expected.
As far as i know, nothing else is required except update to pom.xml which is as follows -
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
and
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Surprisingly enough, if I include following in the logback config -
<include resource="org/springframework/boot/logging/logback/base.xml"/>
The traceid, spanid and application name is displayed on console . Probably from the base.xml config.
Any idea what might be wrong with my logback file or any other config?
Is there anything missing in the config?
Any help is appreciated.
Well sleuth gets traceId and spanId into logs by overwriting/extending the log-level "field" of the log-pattern (inside org.springframework.cloud.sleuth.autoconfig.TraceEnvironmentPostProcessor)
so in defaults.xml (imported in the base.xml) spring defines the pattern to be:
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-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}}"/>
as you can see if variable LOG_LEVEL_PATTERN is defined it's the one taken (and then overwritten/extended in TraceEnvironmentPostProcessor, if not, it defaults to the default level pattern %5p
so if you do not include base.xml sleuth will not be able to "adjust" the log-level pattern as LOG_LEVEL_PATTERN then just does not exist in the pattern.
As documentation states (https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-logging) you always should include base.xml in your custom logback.xml, and you're fine.
not recommended:
try to define your log level pattern as ${LOG_LEVEL_PATTERN:-%5p} instead of just %5p or %-5level (but solution of including base.xml is the correct solution, if you'd ask me.
also:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-custom-log-configuration lists application.(properties|yml) fields to tweak spring-boot logging from properties.
e.g. I have custom BaggageFields for propagating downstream and my application.properties to see them in logfiles I define application properties as follows: (I do not define a custom logback.xml!)
for sleuth 2.0+
logging:
pattern:
level: "%5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] [%X{ddd:-},%X{bp:-},%X{bpids:-}] [%X{op:-},%X{chunk:-},%X{i:-}]"
for sleuth 3.0+
logging:
pattern:
level: "%5p [${spring.zipkin.service.name:${spring.application.name:}},%X{traceId:-},%X{spanId:-}] [%X{ddd:-},%X{bp:-},%X{bpids:-}] [%X{op:-},%X{chunk:-},%X{i:-}]"
In case anyone out there looking for olution I was able to get it using following changes -
Add the following to you logback-spring.xml
<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" />
Then use the following variable in your log pattern -
%clr(${LOG_LEVEL_PATTERN:-%5p})
E.g.: -
<Pattern>%d %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(%-40.40logger{39}) %clr([%20.20t]) %clr(:) %m%n</Pattern>
Hope this helps someone.

Add datetime to log file name

I have some logs that I want to write to a file in a spring-boot microservice.
In my "application.yml" I have the below line which is working fine. But I also want to add date and time to the file name. How do I do that?
logging:
file: logs/${spring.application.name}.log
Ideally, I want the filename to be something like this:
logs/spring-boot-application_YYYY_MM_DD_HH_MM
A similar question has been answered here: How to include date in log file's name with Spring Boot / slf4j?
Basically if you want more control over the logging setup, you should have your own logback.xml under src/main/resources and configure the naming format similar to:
<FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
If you are using spring-boot , you could easily specify it in application.yml or application.properties and configuring logback xml to enable Rolling File Appender as :
spring:
logging:
file: logs/dev_app.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
org.springframework.web: DEBUG
guru.springframework.controllers: DEBUG
org.hibernate: DEBUG
and specify the time based rolling policy for the file in :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ROLLIN" />
</root>
<logger name="org.springframework.web" level="INFO"/>
</configuration>
Doc
Official Spring Doc

How to create logbak.properties for TimeBasedRollingPolicy

I want generate logs on day basis, so here is my logbak.xml which works fine.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern> %d{dd-MM-yyyy HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/administrator/Desktop/app/logs/log_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</configuration>
However, I want to achieve the same thing by using logbak.properties file. But I could find any example for TimeBasedRollingPolicy. I could not find attribute for rolling policy in common-application-properties
logging.path= //file path
logging.file= //file name
logging.pattern.file= //file logging pattern
**What is for TimeBasedRollingPolicy ??**
In short how to give rolling policy in .properties file??
Slf4j is a logging abstraction that you can include in your project by just including the api jar. You are free to use any logging implementation like log4j or logback.
So you can use Log4J by simple providing the dependency in maven:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-beta0</version>
</dependency>
And then create a config file log4j.properties and put following contents :
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.DailyRollingFileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=d:/logs/example
log4j.appender.loggerId.DatePattern='-'yyyyMMdd'.log'
Hope this will work.

Spring boot logging into mysql database

I have to put all the log data (i.e., debug, info, error) into mysql database instead of to file/console.
I read the spring boot documentation but I didn't see any configuration related to database for logging.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
Also tried the following link but its also not working.
https://www.tutorialspoint.com/log4j/log4j_logging_database.htm
Can anyone help me to do this.
Thanks.
I read the spring boot documentation but I didn't see any
configuration related to database for logging.
Because spring boot hands off that functionality to logging framework (slf4j + logback/log4j etc). So you need to configure your logging framework accordingly using it's configuration file (eg: logback.xml, logback-spring.xml, logback.groovy etc). Default logging frameworks in Spring boot is slf4j+logback. So checkout how you can use DBAppender.
For Logback
https://logback.qos.ch/manual/appenders.html#DBAppender
http://learningviacode.blogspot.com/2014/01/writing-logs-to-database.html
Log to database with LogBack
https://medium.com/#chakrar27/storing-log-data-in-postgresql-using-logback-db-appender-292891a9918
1. create logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type 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="db" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://localhost:5432/simple</url>
<user>postgres</user>
<password>root</password> <!-- no password -->
</connectionSource>
</appender>
<!-- the level of the root level is set to DEBUG by default. -->
<root level="TRACE">
<appender-ref ref="stdout" />
<appender-ref ref="db" />
</root>
</configuration>
2. Create the 3 tables
logging_event
logging_event_property
logging_event_exception
They must exist before DBAppender can be used
For Log4J
https://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender
For Log4J2
http://smasue.github.io/log4j2-spring-database-appender

Spring - Exceptions do not log to a file

I'm currently using SLF4J API for logging.
Whenever an exception is thrown during runtime, the full error stack trace does not log to file, it is only printed to the console. I'm using eclipse.
Here is my code for logback.xml (currently located in classes folder under WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Is there something missing/wrong with the above file??
Is it possible to log (to a file) all the text that will be printed to console?
How does spring(or the project itself) read the logback.xml file? What if I rename it and place it in another folder?
How to create one root containing all the levels (INFO, DEBUG, ERROR, WARN, etc..) ?
To answer your questions:
Nothing looks obviously wrong to me in the file you've posted, although I didn't try actually running it.
Just the way you did it is fine, with two appenders, one that goes to the file and the other to go to the console.
Logback by default looks in the classpath for the logback.xml file. Refer to the configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it in src/main/resources. But if it ends up in WEB-INF/classes when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try putting debug="true" in the <configuration> element, to see if there's an error that it's picking up that's causing it to not use the appender the way you're expecting.
Specifying the "DEBUG" level of logging, as you've done, will also get all higher levels as well.
If the issue is that logging from Spring isn't going where you want, while your application's logging is working fine, you may need to redirect Spring (which uses Apache Commons Logging) to use SLF4J instead. To do that, remove the commons-logging dependency and add the jcl-over-slf4j library. This will emulate the commons-logging calls and have them point to SLF4J instead. See the "Using SLF4J" section of this blog post for more details.
Your configuration looks OK, but you can try use your root level INFO instead DEBUG and if you have frameworks like spring, hibernate etc. Logback allow you uses another levels to them something like:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />

Resources