Create 2 log file in Springboot - java

I creating multiple log file in Springboot , log-back.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HOME" value="./logs" />
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">-->
<syslogHost>10.0.3.20</syslogHost>
<facility>SYSLOG</facility>
<suffixPattern>abc [%thread] %logger %msg</suffixPattern>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/abc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/abc.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
‌
// another log file
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}mon.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/mon.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.rh.project" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="mail-log" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE" />
</logger>
</configuration>
When I check inside log directory, I only saw abc.log , but no mon.log

Did you notice the slash missing inside in second logger??
<file>${HOME}/mon.log</file>
I believe that's the problem. However, nothing to do with spring boot :)

Related

Converting logback.xml to application.properties

How can I convert logback.xml file to properties?
For example, there is such a configuration file or the code I have attached below. I need to write similar properties in application.properties
Are there examples for such configurations?
is it correct to use the example from this question?
<configuration>
<timestamp key="today" datePattern="yyyy-MM-dd"/>
<property name="LOG_HOME" value="log"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/board_of_ads-${today}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/archive/board_of_ads-log_%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 20MB, keep 30 days worth of history, but at most 1GB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<Pattern>
%d{DEFAULT} %-5level [%t] %c: %msg%n
</Pattern>
</encoder>
</appender>
<appender name="AuthController-Audit"
class="ch.qos.logback.core.FileAppender">
<file>${SIMPLE_CONTROLLER_HOME}/auth-controller/AuthController-log_${today}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{DEFAULT} %level - %msg%n
</Pattern>
</encoder>
</appender>
<logger name="com.board_of_ads" level="INFO" additivity="false">
<appender-ref ref="FILE-AUDIT"/>
<appender-ref ref="STDOUT"/>
</logger>
<!-- Controllers loggers setup-->
<logger name="com.board_of_ads.controllers.simple.AuthController" level="INFO" additivity="true">
<appender-ref ref="AuthController-Audit"/>
</logger>
<logger name="com.board_of_ads.controllers.rest.NotificationRestController" level="DEBUG" additivity="true">
<appender-ref ref="NotificationRestController-Audit"/>
<appender-ref ref="STDOUT"/>
</logger>
</configuration>

Slf4j - enable debug,info messages

I need to enable debug,info logs in a existing Java/spring-boot project.
This is the original application.properties file:
logging.level.org.springframework=WARN
logging.level.io.comp=INFO
and this is the original logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<property name="LOGS_PATH" value="./logs" />
<appender name="ERROR_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOGS_PATH}/${NODE_NAME}_error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_error.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="DEBUG_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_PATH}/${NODE_NAME}_debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_debug.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="INFO_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>DENY</onMatch>
</filter>
<file>${LOGS_PATH}/${NODE_NAME}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="DUMP_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_PATH}/${NODE_NAME}_dump.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_dump.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
</rollingPolicy>
</appender>
<!-- Loggers -->
<logger name="io.comp" additivity="true">
<level value="DEBUG" />
<appender-ref ref="DEBUG_LOG_FILE" />
<appender-ref ref="ERROR_LOG_FILE" />
</logger>
<!-- Root Logger -->
<root level="DEBUG" >
<appender-ref ref="DUMP_LOG_FILE" />
<appender-ref ref="ERROR_LOG_FILE" />
</root>
<root level="INFO" >
<appender-ref ref="INFO_LOG_FILE" />
</root>
i tried to change the application.properties file to:
logging.level.org.springframework=DEBUG
logging.level.io.comp=DEBUG
and the logback.xml file to:
<?xml version="1.0" encoding="UTF-8"?>
<property name="LOGS_PATH" value="./logs" />
<appender name="ERROR_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
</filter>
<file>${LOGS_PATH}/${NODE_NAME}_error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_error.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="DEBUG_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_PATH}/${NODE_NAME}_debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_debug.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="INFO_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
</filter>
<file>${LOGS_PATH}/${NODE_NAME}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="DUMP_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_PATH}/${NODE_NAME}_dump.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>
%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread][%-5level][%logger{0}] %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/archived/${NODE_NAME}_dump.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
</rollingPolicy>
</appender>
<!-- Loggers -->
<logger name="io.comp" additivity="true">
<level value="DEBUG" />
<appender-ref ref="DEBUG_LOG_FILE" />
<appender-ref ref="INFO_LOG_FILE" />
<appender-ref ref="ERROR_LOG_FILE" />
</logger>
<!-- Root Logger -->
<root level="DEBUG" >
<appender-ref ref="DUMP_LOG_FILE" />
<appender-ref ref="ERROR_LOG_FILE" />
<appender-ref ref="DEBUG_LOG_FILE" />
</root>
<root level="INFO" >
<appender-ref ref="INFO_LOG_FILE" />
<appender-ref ref="DEBUG_LOG_FILE" />
</root>
but with no success. Only logs with levels: error,warn (log.error, log.warn) are printed to the the files.
What am I doing wrong?
I found the problem
Within the configuration of the project, reference was made to another properties file, in which the Logger level was WARN.

Logback configuration into Java project

I'm using logback to save all my INFO into log file and every day I would like to save this file in a specific folder archived
This is how I have configured it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${CATALINA_HOME}/logs/DartFleetViewer.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>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- <appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/error.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/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender> -->
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>
The odd thing is that I find this in my folder
almost all of the files are empty or with few code lines, for example I don't find nothing about yesterday, In the last file I only find
2016-08-02 02:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 05:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 08:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
This is information about today and not yesterday.
Furthermore yesterday I had some errors, warnings and a lot of info log.
Do you see some error in my configuration file?
UPDATE:
Since I receive warning during deployment I have changed my logback configuration in order to avoid all warning and I use only time rollback. It seems to work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${CATALINA_HOME}/logs/DartFleetViewer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>

logback does not write to file

I have an app with the following logback config
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level{5} %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME}/old/app-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>14</maxHistory>
</rollingPolicy>
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="EXCEPTIONS" class="com.software.nagibator.emulator.logging.ExceptionsAppender">
<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
<jndiLocation>jdbc/AppExceptions</jndiLocation>
</connectionSource>
</appender>
<logger name="FileLogger" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="ExceptionsLogger" level="DEBUG" additivity="false">
<appender-ref ref="EXCEPTIONS" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I use this config on two ubuntu servers. On the first server the config works as expected (it writes the log to file). However, on the second server logback does not write the log to file.
Is there something wrong with my config or is the error elsewhere in the project?
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
...
It's look like you miss < !-- before comment in xml.

How to customize logback.xml file

I want to customize my logback.xml file for 3 levels (error,debug,info) .I do like this :
<configuration>
<appender name="FILE-ERROR" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-DEBUG" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-INFO" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE-ERROR" />
</root>
<root level="DEBUG">
<appender-ref ref="FILE-DEBUG" />
</root>
<root level="ERROR">
<appender-ref ref="FILE-INFO" />
</root>
</configuration>
But I didn't get the correct loges information .Do logback.xml in wrong format?
If I understood correctly, you want to log the Levels ERROR, INFO and DEBUG to one file.
Please change your config as so:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
Logback will then log all events of levels DEBUG and higher. That means it will log DEBUG, INFO, WARN, ERROR.
You can leave out <append>true</append> it is the default.

Categories