Openshift : Spring boot : log4j settings -- configuration and finding the application log file - spring-boot

My Spring boot application on Redhat/Openshift uses the shown log4j2.xml file.
The issue is that I cannot find the application log file.
My questions are:
Below I describe the way of configuring the log4j logging for Spring boot on Openshift. Is this correct?
Where can I find the application logging file logs/app2.log? It is not on Openshift.
Yes, I know that there is also an app.log file via the application-openshift.properties setting. That one is never changed. Why?
I was adviced to use this in de application-openshift.properties file. Leaving these lines out has no consequences.
logging.file = ${OPENSHIFT_DATA_DIR}/logs/app.log
logging.level = INFO
My src/main/resources/log4j2.xml file contains the following. I configured it also for Console for local testing. Yes, later I could distinguish per environment (not for now).
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="logs/app2.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File" />
</Root>
<Logger name="nl.xyz.mod" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="nl.xyz.mod" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>

Finally solved it!
The change in the log4j2.xml file is marked with ** ... ***.
You can specify a good target for the log file via an OpenShift environment variable. Within the log4j2.xml file you can specify this via ${env:OPENSHIFT_DATA_DIR}.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- ** location of the file is: ${env:OPENSHIFT_DATA_DIR}/logs/app2.log" ** -->
<File name="File" fileName="${env:OPENSHIFT_DATA_DIR}/logs/app2.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File" />
</Root>
<Logger name="nl.deholtmans.tjm1706" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="nl.deholtmans.tjm1706b" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>

Related

spring cloud set logging.level does not work in application.yml(I have a logback-spring.xml, too)

these screenshot are my configurations about logback
logback-spring.xml
<root level="info">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
application.properties
logging.file.path=./logs
logging.level.cn.hsa=debug
logging.level.root=info
logging.level.org.springframework.web=trace
It’s not working when i change logging.level.root to debug or others in application.properties, it's always use this configure in logback-spring.xml
I read spring source code, logback-srping.xml loaded before application.properties
but i dont know why these configures were not override
You can define something like this if you want to customize:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="LOG_PATH" source="app.log.path"
defaultValue="your_log_path" />
<timestamp key="dateStamp" datePattern="yyyy-MM-dd" />
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</Pattern>
</encoder>
</appender>
<springProfile name="test,prod">
<appender name="Access-FileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_PATH}/logs/xyz.%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</Pattern>
</encoder>
</appender>
<appender name="Async-Access-FileAppender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="Access-FileAppender" />
</appender>
</springProfile>
<root>
<level value="INFO" />
<appender-ref ref="consoleAppender" />
</root>
<springProfile name="local">
<logger name="com.xxx.yyy" level="DEBUG" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
</springProfile>
<springProfile name="dev">
<logger name="com.xxx.yyy" level="DEBUG" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
</springProfile>
<springProfile name="test,prod">
<logger name="com.xxx.yyy" level="INFO" additivity="true">
<appender-ref ref="consoleAppender" />
</logger>
</springProfile>
</configuration>
As you can see, test/prod have the similar logging configuration. Similarly, you can configure for local and dev as well.
Hope this helps.

How to exclude a package from root tag in xml configuration log4j2

I am trying to disable the logging of a package for certain appenders, but I still have them logged everything else.
I have tried:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="console">
</Console>
<RollingFile name="files">
</RollingFile>
</Appenders>
<category name="org.springframework.beans">
<priority value="info" />
</category>
<Loggers>
<Logger name="my.package.to.disable.log" level="off">
<AppenderRef ref="console" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="files" />
</Root>
</Loggers>
</Configuration>
This disables my package logs in both appenders. How could I prevent this?

How to create seperate log file for each batches running in a single project?

Working on Spring boot and spring scheduler project to run multiple batches.
Here all batches information I am writing into one log file.(One log file got created)
Now I need to write information in separate log files for different batches i.e no. of batches = that many no. of log files.
Note that I have only one main class as I am using spring boot and all batches comes under only one package, only one service for all batches and one repository for all batches.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">$${ctx:filename} %d %p %c{1.} [%t] %m%n
</Property>
<Property name="APP_LOG_ROOT">C:/job-logs/claims-dms/</Property>
<Property name="APP_LOG_BACK_ROOT">C:/job-logs/claims-dms/back/</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="appLog" fileName="${APP_LOG_ROOT}claims-dms.log"
filePattern="${APP_LOG_BACK_ROOT}claims-dms-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.bct" additivity="false" level="all">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.hibernate.SQL" additivity="false" level="all">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.hibernate.type.descriptor.sql" additivity="false"
level="all">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.springframework.jdbc.core" additivity="false"
level="all">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Root>
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
you can put your batchname in MDC from where your job is getting triggerd and use that key in logback.xml
#Scheduled
public void scheduleJob(){
MDC.put("jobname", jobName);
// other stuff
}
and logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- in the absence of the class attribute, it is assumed that the
desired discriminator type is
ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
<discriminator>
<key>jobName</key>
<defaultValue>batch-service</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${jobName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${server.docroot}/logs/${jobName}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${server.docroot}/logs/${jobName}.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>5</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>
<root level="INFO">
<appender-ref ref="SIFT" />
</root>
</configuration>
now for each job a new log file will be created.

Spring boot application, integration of log4j, no file is created whenusing rolling file appender

In my spring boot application I wanna add logging, for that I tried to use log4j2
This my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<!-- Rolling File Appender -->
<RollingFile name="FileAppender"
fileName="logs/log4j2-demo.log"
filePattern="logs/log4j2-demo-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="tn.manual.controller" level="debug"
additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Logger>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
In the console, I can see the log messages.
But NO file is created, the logs folder should I create it before?
Really, I'm disturbed, I should finish this task today.
I need your help please
you can use like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This the default configuration path of logback if you want to modify
the location please provide its entry in application.properties file as value
of logging.config key -->
<configuration scan="false">
<property name="logPattern"
value="%-4relative [%thread] %-5level %logger{35} - %msg%n" />
<property name="logEncoding" value="UTF-8" />
<property name="logDirectory" value="logs" />
<property name="file_base_name" value="Your service name" />
<property name="archive" value="archive" />
<timestamp key="current_date" datePattern="yyyy-MM-dd" />
<!-- Shut down hook registered to close logging factory Elegantly . -->
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook" />
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDirectory}/${file_base_name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDirectory}/${archive}/${file_base_name}_%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<charset>${logEncoding}</charset>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<appender name="ANALYTICS-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDirectory}/${file_base_name}_error</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDirectory}/${archive}/${file_base_name}_error_%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<charset>${logEncoding}</charset>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<!--<logger name="analytics" level="INFO" additivity="false">-->
<!--<appender-ref ref="ANALYTICS-FILE"/>-->
<!--</logger>-->
<!-- <appender name="ASYNC500" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="fileAppender"/> <queueSize>5000</queueSize> <discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE" /> </appender> -->
<!-- Only use this appender when you are developing your application but
during production it is advisable to disable logging to console to reduce
latency in logging as it costs more I/O time. -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %highlight(%-5level)
%logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<!-- Configuration for loggers ,Loggers who do not have logging level defined
will inherit levels from root
-->
<root level="error">
<appender-ref ref="ANALYTICS-FILE"/>
</root>
<root level="info" additivity="false">
<appender-ref ref="ANALYTICS-FILE"/>
</root>
<!-- -->
<root level="WARN" additivity="false">
<appender-ref ref="fileAppender" />
<appender-ref ref="stdout" />
</root>
<logger name="Your package name" level="DEBUG" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
<logger name="org.springframework.web" level="WARN" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
<logger name="org.apache.commons.beanutils.converters" level="WARN" additivity="false">
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdout" />
</logger>
</configuration>
I hope this will help. If it will help, you can accept it as your accepted answer.
Thanks,

Get the beans created by Spring in logging

I'm new at logging stuff and I want to see the logging of spring, all the beans he creates, everything ...
I have try this code in my log4j2.xml but nothing happened (for my classes in controller package it's okey I got the log)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="CONSOLE"/>
</Root>
<logger name="controller" level="ALL" />
<logger name="org.springframework" level="ALL" />
<logger name="org.springframework.*" level="ALL" />
<logger name="org.springframework.**" level="ALL" />
</Loggers>
</Configuration>
So in the classes of Spring they no contain something like static final Logger logger = LogManager.getLogger(...); and logger.debug(...); or what !!
Take a look at this
org.springframework.beans.factory.support.DefaultListableBeanFactory
Also more info you can get from this post

Resources