How to enable Tomcat Logs in Spring Boot - java

When we use external tomcat to deploy our Spring MVC application, there are certain log files which get printed in the logs folder, i.e., host-manager.log, localhost.log, manager.log, etc.
I needed to enable the generation of these logs in my Spring Boot Application too. How can we do that?

Please check spring docs for logging in file-output: https://docs.spring.io/spring-boot/docs/2.7.0/reference/htmlsingle/#features.logging.file-output
Or alternatively you can use logback-spring.xml to achieve this.
Sample logback-spring.xml which prints logs in the console and file and it daily rollovers the file or if it reaches 10MB:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/> <!-- the folder where you want your log files to be -->
<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 10MBs -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="INFO">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
More about how to config logback: https://logback.qos.ch/documentation.html

Related

How to log specific log to text file with logback in Spring?

I have my current logback file, which to my understanding is a classic logback implementation:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>Hello %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I have a specific log:
log.info(String.format("ProxyICA %s has an integrity violation. %s.",
processorId.getProxyIca(),
fkViolationMsg)
);
that I am trying to get in a text file. The only configuration I need in my logback is to put every instance of this log into a text file. Every other log can default log to the console (which it was doing without the logback.xml).
Thank you in advanced!
Here is an example of setting it up log to a specific file "Custom-Errors.txt", and to have the logs be deleted if they reach a certain size 20MB or 182 days elapse
<appender name="Custom_Logger
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>Custom-Errors.txt</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>Custom-Errors-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 20MB, keep 182 days worth of history,but
at most 500MB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>182</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO, DEBUG, TRACE</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%date %level [%thread] [%file:%line] %msg%n%throwable
</Pattern>
</layout>
</appender>
<logger name="CUSTOM_LOGGER" additivity="false">
<appender-ref ref="Custom_Logger" />
</logger>
Then in your java class make a private variable to use the logger like this
private static final Logger CUSTOM_LOGGER =
LoggerFactory.getLogger("CUSTOM_LOGGER");
logging a dependency, or use to log anything you want based on package
here is an example of logging a library
<appender name="ConsoleOutForSpring" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<logger name="org.springframework" additivity="false">
<appender-ref ref="ConsoleOutForSpring" />
</logger>
I do admit there are a lot of tags and settings to this logger that are kind of complicated, but reading docs and looking at others examples has helped me.
Here were some docs I noted in my logback.xml
<!-- http://logback.qos.ch/manual/configuration.html this helped me with this weird looking stuff i.e. ===> %date %level [%thread] [%file:%line] %msg%n%throwable -->
<!-- http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
helped me set this up:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>Errors-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>182</maxHistory>
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>

My logger does not create a RollingFile with Spring (config with logback.xml)

I generate a spring boot project, and I want to log in an external file.
I choose to use Logback implementation, and here is my logback.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" 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="SAVE_TO_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/spring-boot-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
</appender>
<!-- LOG everything at INFO level -->
<root level="INFO">
<appender-ref ref="SAVE_TO_FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
I use to log with the classic line of code to log :
LoggerFactory.getLogger(CustomLogger.class);
I know that there is an implementation by default, and suspect my logback.xml (which is as the root of the project) is not taking in consideration.
Is there somewhere I have to specify the name of the file, or import it in my application as a resource ?
OK,
I had to put the logback.xml in the main path of the project.

Spring Boot Logback Profile Logger Issue

For my spring boot app, I am trying to setup logback so that logs are written to a file. I have defined 2 profile one for prod and one default. The prod one is supposed to write to a file and default is supposed to write to the default console(std out). While the prod profile seems to be working, when I run the default profile the logs don't print in the eclipse console - they too are logged to the file. What did I do wrong
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<springProfile name="default">
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="prod">
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
<totalSizeCap>20GB</totalSizeCap>
<!-- 60 days to keep -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="edu.aus" level="DEBUG"/>
<root level="ERROR">
<appender-ref ref="FILE-ROLLING"/>
</root>
</springProfile>
</configuration>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HOME_LOG" value="logs/app.log"/>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 20GB,
it will delete old archived file -->
<totalSizeCap>20GB</totalSizeCap>
<!-- 60 days to keep -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<logger name="edu.aus" level="debug" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<root level="error">
<appender-ref ref="FILE-ROLLING"/>
</root>
</configuration>
application.properties
# logging level
logging.level.org.springframework=ERROR
logging.level.edu.aus=DEBUG
# output to a file
logging.file=app.log
# temp folder example
#logging.file=${java.io.tmpdir}/app.log
logging.pattern.file=%d %p %c{1.} [%t] %m%n
logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
## if no active profile, default is 'default'
spring.profiles.active=default
# root level
#logging.level.=INFO
I don't know what did you write in the included *.xml.
You are using CONSOLE, but I cannot see it in you code.If it is not rightly called, I think you should define CONSOLE like below:
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d|%-5level|%thread|%logger{40}|%L|%msg%n</pattern>
</encoder>
</appender>
put code above in logback.xml between <configuration> tag

Spring Logback - SizeAndTimeBasedRollingPolicy is not creating log file

I am using SizeAndTimeBasedRollingPolicy of logback to create log files based on time and size. But log files are not creating in specified folder.
But when I used FixedWindowRollingPolicy or TimeBasedRollingPolicy, it is working as expected and log files are getting created in specified folder.
Spring boot version 1.2.0.RELEASE used in my application.
Below is my logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="LOG_PATH" value="logs"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>utf-8</charset>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
</appender>
<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{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/archived/log_%d{dd-MM-yyyy}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>100MB</totalSizeCap>
</rollingPolicy>
</appender>
<root level="${logback.loglevel}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="SAVE-TO-FILE" />;
</root>
</configuration>
Thanks in advance for any help.

how to cofigure slf4j in spring-mvc for logging?

I want to use SLF4j in Spring mvc-hibernate project. i have made
logback.xml for logging
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="c:/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.mkyong.web" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
but i do not know to cofigure logback.xml in spring-mvc.
In which folder in project should i keep my logback.xml?
how do i configure logback.xml in DefaultServlet-servlet.xml
No need to do anything in Spring configuration. Just add the slf4j and logback dependencies (e.g. in pom.xml if you're using maven), keep logback.xml in the classpath (e.g. src/main/resources) and use the SLF4J API in your spring-mvc Controllers.
You can also see more details here:
https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

Categories

Resources