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

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>

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.

Spring boot logging

I am using Spring boot to build a simple REST service and I am wondering about the most appropriate way to handle the logging.
In my application.properties file I have the following:
logging.level.org.springframework.web: DEBUG
While developing the application I simply run it as such:
java -jar myapp.war
thus, I got all the nice log messages in stdout. However, I intend to deploy it, and I'm wondering what is the most appropriate way to deploy the application and still have my logs.
Sure, one can simply redirect the output
java -jar myapp.war >> somefile
but this is not very elegant, and I want to deploy my application so that it can easily be used as a service:
ln -s /my/app/xyz.war /etc/init.d/xyz
Then, doing
service xyz start|stop|restrart
to manage it. Seems like doing this prevents me from redirecting stdout ..
Any ideas or advice about this?
What you are really after is Spring Boot file logging output functionality.
Quoting the above documentation:
By default, Spring Boot will only log to the console and will not
write log files. If you want to write log files in addition to the
console output you need to set a logging.file or logging.path property
(for example in your application.properties).
Essentially adding in your application.properties:
logging.file=name.of.my.log.file.log
logging.path=/path/where/above/log/file/gets/stored
In you application.properties file you can set two attributes for your logging.file.
Like in the documentation description: (26.3 File output)
By default, Spring Boot will only log to the console and will not
write log files. If you want to write log files in addition to the
console output you need to set a...
logging.file
Writes to the specified log file. Names can be an exact location or relative to the current directory.
logging.path
Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.
After setting one of those logging properties will be written in a file.
Here you can find the complete doc
I want to give a short update in 2020:
logging.file and logging.path are now deprecated:
Instead, I suggest to use logging.file.name in your application.properties (logging.file.path is also available).
Example: logging.file.name=./logdir/spring.log
Spring Boot support almost every logging frameworks you can use as per your convenience, but I will suggest you to use slf4j logging framework and customize using logback.xml it's very easy look at
1) Create LOGGER object adding this single line of code in your class
private static final Logger LOGGER = LoggerFactory.getLogger(YourClassName.class);
2) Create logback.xml file in /resource folder and copy below code
<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
<appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MMM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>
<appender name = "FILE" class = "ch.qos.logback.core.FileAppender">
<File>/var/tmp/app.log</File> <!-- LOCATION of FILE WHERE YOU WANT TO SAVE -->
<encoder>
<pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>
<root level = "INFO">
<appender-ref ref = "FILE"/>
<appender-ref ref = "STDOUT"/>
</root>
</configuration>
Using logback.xml configuration you can customize your application logging in spring boot

How to configure log4j and logback to log into one single file properly?

I have a java web app that uses logback with slf4j for logging.
And this project has a dependency jar (which is a sub project). And this dependency jar uses org.apache.log4j.Logger for logging.
All logs must go into one log file.
My problem is, whatever I am logging with the code in the jar file is not being written to the log file.
Initially I had logback.xml. To resolve the above problem I added log4j.properties file to my web app.
It resolved my problem and now I can see all logs in one file.
Again my new probelm is: Earlier the log file was rolling every day according to the rolling policy in logback.xml.
Now it is not rolling. Even after I configured the rolling policy in log4j.properties that matches with the rolling policy that is in logback.xml, rolling is not happening.
I don't want to replace log4j in my dependency with logback. And also I want to write alll my logs into one file.
Is it possible to this, how can I resolve my issue?
FYI: I have "log4j-over-slf4j" dependency included in my pom.xml
Log4j.properties:
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=SOME_PATH/logs/studentApp.log
log4j.appender.file.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.FileNamePattern = SOME_PATH/logs/studentApp.%d{yyyy-MM-dd}.log
log4j.appender.file.maxBackupIndex=14
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.rootLogger=DEBUG,file
Assuming your rolling policy is correct, it's possible that log4j picks up the wrong configuration. log4j looks for the first configuration file in the classpath (first .xml then .properties) and if it happens to be the configuration from one of your dependencies your rolling policy will not work.
To test this you can add a system property -Dlog4j.configuration=... with the path to your configuration to the tomcat startup script. Or simply set your log level to TRACE/DEBUG and see if it affects the output.
Edit: your log4j config looks good, but the date pattern is missing.
Try adding
log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm (note that I've set the pattern to roll-over every minute, for easier testing)
Edit: you can remove every line containing rollingPolicy and maxBackupIndex - those will not be picked up in this case
The following configuration will create a .log file for the current date and keep adding the log in that file. When the date chanages, it will create a new file with the current date. This configuration will also zip the files as or when they exceed a certain size limit.
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 500MB -->
<maxFileSize>300MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{0} - %msg%n</pattern>
</encoder>
</appender>
Hope it helps.

Configuring Spring Boot Logback

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

Can't configure Jetty and log4j

Recently I inherited a project and having some hard time configuring the logging in the sense I can't seem to configure it at all!
So let me first give you the context:
It runs under Jetty 9.2
It uses the Apache Commons logging library
It has a Maven dependency on log4j which I assume the apache commons will pick that up and use it.
There are no existing commons-logging.properties or log4j.properties so I assume it falls back to defaults.
There is no jetty-logging.properties either.
So here is my problem, it currently does not log the timestamp, the output is like this:
5027 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/css/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
and:
912562 [qtp760972690-15] DEBUG com.xxx.xx.Class - Some Action Log Msg
No idea what those fields before the DEBUG are. I had previously worked with Tomcat, sl4j and pretty sure Apache Commons logging too.
So I thought I would config files both 'commons-logging.properties' and 'log4j.properties' in the WAR file WEB-INF/classes.
Now it recognises and picks up the 'commons-logging.properties' but the 'log4j.properities' seems to have no effect at all.
So am I missing a step here, have I forgotten something? Is this a Jetty issue because it uses sl4j and I'm not?
Here is is m commons-logging.properties:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.configuration=log4j.properties
And here is my log4j.properties:
# Set root logger level to DEBUG.
log4j.rootLogger=DEBUG,stdout
# stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Another note is that log lines from Jetty itself do have the nice date-stamp I want:
2014-09-06 19:36:19.356:INFO:/:main: Initializing Spring FrameworkServlet 'appServlet'
But not my main application logging.

Categories