Configuring Spring Boot Logback - java

I am trying to write the application.yml file with the logging configurations for spring boot. Thanks to Bal from my previous question I was able to take a look at the sample file on spring boot's website. Since I only want to configure the logging information for now here is what my yml file looks like right now:
# ===================================================================
# SPRING BOOT PROPERTIES
# ===================================================================
# LOGGING
logging:
config: /logback.xml # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.level.org.springFramework=DEBUG # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
path: # Location of the log file. For instance `/var/log`
pattern:
console: # Appender pattern for output to the console. Only supported with the default logback setup.
file: # Appender pattern for output to the file. Only supported with the default logback setup.
level: # Appender pattern for log level (default %5p). Only supported with the default logback setup.
But I have a few questions:
How much logging configuration should I do here in the yml file versus the logback.xml file? Should any logging configuration be put here at all versus putting it all in logback.xml?
Is the logging level configuration line written correctly?
Can I use another library's logging level, like logback? For example, could I put this: ch.qos.logback.classic.Level=DEBUG

I would not combine logback.xml with application.yml. Use either simplified configuration via application.yml if it is enough for you or use "fullfeatured" logback.xml.
To be honest I am not sure what will happen when you will have contradicting configuration in those two files (e.g. different patterns or log levels).
2 + 3. My guess is you have extra logging part. Correct example would be e.g.
logging:
level:
ch.qos.logback.classic: DEBUG
org.hibernate: INFO

Related

Logback Spring - Change log levels during runtime using an external yaml file outside of the jar

Inside of my logback-spring.xml file, I am able to change the log levels, and it's able to autodetect changes when I include auto scan. Looks like this:
<configuration debug="true" scan="true" scanPeriod="10 seconds">
However, I want to keep all of my log levels in an external yaml configuration file and I don't want to have to restart the server to detect those changes. Is there a way to use an external yaml file to define log levels and use logback autoscan? Or should I be looking at a completely different way of going about it?
You can do that in application.yml, try adding the following settings
spring:
logging:
level:
org.springframework.web: DEBUG
com.company.package: DEBUG
You can also specify pattern if you like.

Springboot how to rotate log files on the server restart

Springboot how to rotate log files on the server restart.
I have the below entries
# LOGGING
logging.level.org.springframework.web=WARN
logging.level.org.hibernate=WARN
logging.file=/var/log/apps/myapp.log
I can't find any details here:
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/common-application-properties.html
As per the following link in application.properties regarding log files, the following are the configurations.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup.
Apart from the above, you can also check below the configuration based upon the server configured.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.undertow.accesslog.rotate=true # Whether to enable access log rotation.
I would suggest to use Slf4j along with logback. You need to configure logback.xml and you can configure rolling file appender.
You can add the custom log configuration with logback-spring.xml.
The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config.
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration
I got a rotation policy with setting a limit to the log file size and the same for the backup.
logging.file.name=/path/log/file
logging.file.max-size=10MB
logging.file.total-size-cap=10MB
IMPORTANT: it will keep just a backup file.
I dont know your exact scenario but you can do this by passing env_variable when you start a server.. like your build version number and use that in your property file
spring-boot:run -Dbuild.number=1.x
And you may use this like
# LOGGING
logging.level.org.springframework.web=WARN
logging.level.org.hibernate=WARN
logging.file=/var/log/apps/myapp-${build.number}.log
Dont forgot to increment number everytime.

Logback: How to remove class names and log level from Log file?

I am using Logback in my Spring boot application.
In my log file I currently get the sample output:
16:09:43.299 [pool-2-thread-1] INFO c.b.r.h.k.s.myClassName - Log message
How can I change my log settings so that it only looks like the following:
16:09:43.299 Log message
I.e removing the "[pool-2-thread-1] INFO" from the log statement.
If you're using Spring Boot default console and file logs, i.e. haven't any logback.xml in your classpath, you can use logging.pattern.console and logging.pattern.file properties. For example, adding this to your application.yml file will do the trick for you:
logging:
pattern:
file: '%d{HH:mm:ss.SSS} %msg%n'
Otherwise, add this pattern to your corresponding file appender in your logback.xml:
<pattern>%d{HH:mm:ss.SSS} %msg%n</pattern>

How to make slf4j print its own configuration?

log4j has a system property called log4j.debug, that when set by adding -Dlog4j.debug=true to your command line, prints out the information about how log4j configures itself (for example, the location of the con file it found and loaded).
I am looking for a similar capability for slf4j. Can we tell slf4j to print out how it is set and configured?
slf4j is wrapper for other loggin systems (the f in the name stands for facade), it does not have its own configuration.
With slf4j you can even use log4j as real logging library. If you are using logback as logging library along with slf4j there is an attribute debug for the main configuration tag (if you are using an xml file for configuring logback).
<configuration debug="true">
....your conf here
</configuration>
the logback mnanual can be found here

Log4j doesn't log INFO Level

I have the following log4j.properties file, for an application deployed in WebSphere Portal:
log4j.rootLogger=DEBUG, InfoAppender, DebugAppender
log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/info.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/debug.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - %m%n
When I code, I define the logger at class level:
private static Logger logger = Logger.getLogger(IWannaLogThis.class);
And I log INFO messages with this:
logger.info(theObjectToLog);
When I deploy my application, the debug.log file gets everything I log with logger.debug() but ignores everything I write with logger.info(). On the other side, the info.log file keeps empty.
The weirdest thing is that in debug.log and info.log appears some INFO and DEBUG messages made by some JARS (like Hibernate Validator) I had in the classpath, but just ignores everything I try to log in my code.
Any ideas?
This is most likely a classloading-related problem. WebSphere Portal uses Log4J internally, so I'm guessing that you end up using WebSphere Portal's provided Log4J JAR file as well as its own Log4J properties.
You can verify that by adding the following to the JVM arguments of the server instance:
-Dlog4j.debug=true
And then inspect the SystemOut.log file. Log4J will spit out lots of tracing information about the configuration file(s) it reads.
The best way to avoid this is to do the following:
Bundle the Log4J JAR file with your application.
Associate a Shared Library with the server. In that Shared Library, place your Log4J configuration file.
As an alternative to step 2, you can bundle your Log4J configuration file with the application itself, however that would carry its own drawbacks (for example, having to repackage your application whenever you perform a Log4J configuration change).
Another common problem is that the JARs you have in your classpath also use log4j and also have their own appenders set. So depending on the settings that they use, and the packages that your classes reside in, this may lead to the problem you describe.
So:
Make sure that your package names are unique and not used by any of the third party libraries.
Check the log4j settings in all libraries in your classpath. They should not contain general settings which override yours.
Make sure your loggers use your log4j.properties (you can be sure if changes you make in your file affect your loggers as expected).
If you can, make sure that your log4j stuff loads last, in case any of the third party libs reset the configuration. They shouldn't, but who can stop them.
Normally, it should be one of these things. Post more explicit example if it doesn't work.
Good luck!
What I have done in the past is set specific logs for the classes I want to log. It sounds like you can try setting your root logger to INFO and see if that gets you the messages you want. Here's a little bit of my log4j property file. I set a logger for each class and assign it to my "data" appender, which defines the log layout. In the loggers I specify specific classes I want to log and set their Log level individually. Any class that logs that is not defined in the Loggers I have use the default log level for the rootCategory.
log4j.rootCategory=INFO, rollingFile, stdout
#GetData Loggers
log4j.logger.com.myapp.data=INFO, data
log4j.logger.com.myapp.data.SybaseConnection=DEBUG, data
log4j.logger.com.myapp.data.GetData=ERROR, data
# data appender
log4j.appender.data=org.apache.log4j.RollingFileAppender
log4j.appender.data.layout=org.apache.log4j.PatternLayout
log4j.appender.data.File=c\:\\Program Files\\MyApp\\logs\\MyApp-data.log
log4j.appender.data.Append=true
log4j.appender.data.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
you root logger opens the log properties in the debug mode,
use INFO instead of DEbug in the first line of your properties file.

Categories