Get the beans created by Spring in logging - spring

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

Related

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 2.1.2 : How to pass values from application.properties to log4j2.xml?

Basically I want to dynamically pass the log folder path. (Requirement is to pass the log folder path from command line as arguments when I run the spring boot jar). Below is my log4j2.xml for reference.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
<Property name="APP_LOG_ROOT">logs</Property>
</Properties>
<Appenders>
<Console name="Console" follow="true" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="appLog"
fileName="${APP_LOG_ROOT}/application.log"
filePattern="${APP_LOG_ROOT}/application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="20KB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.test" additivity="false" level="ERROR">
<AppenderRef ref="Console" />
</Logger>
<Logger name="com.test" additivity="false" level="ALL">
<AppenderRef ref="appLog" />
</Logger>
<Root level="ALL">
<AppenderRef ref="appLog" />
</Root>
</Loggers>
</Configuration>
Command line:
You can pass values with -D and with the name of variable.
mvn spring-boot:run -DAPP_LOG_ROOT=/somepath/
you can find more details here https://maven.apache.org/ref/3.6.0/maven-embedder/cli.html
and dont forget to change your xml. I havent tested below but you can figure out.
<Property name="APP_LOG_ROOT">${APP_LOG_ROOT:${APP_LOG_ROOT:./logs}}</Property>
application:properties:
as long as your xml accepting APP_LOG_ROOT as above. probably just adding
APP_LOG_ROOT=/sompath
will be enough.

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

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>

Spring performance interceptor not logging with log4j2

I have used spring aop t to log time of service execution but it's not logging. i am not getting any exception as well.
help me to solve this issue whether it's problem in log4j2 configuration or it's wrong with point cut expression.
Following is my application-context.xml
<bean id="perfMonitor"
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor">
</bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="allServiceMethods" expression="execution(* com.lfr.services..*.*(..))"
/>
<aop:advisor pointcut-ref="allServiceMethods" advice-ref="perfMonitor"
order="1" />
</aop:config>
following is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %level - %m%n%ex%n" />
</Console>
<File name="log4jdbc_file" fileName="d:/logs/log4jdbc21.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %level - %m%n%ex%n" />
</File>
</appenders>
<loggers>
<logger name="log4jdbc.log4j2" level="info" additivity="false">
<MarkerFilter marker="LOG4JDBC_NON_STATEMENT" onMatch="DENY"
onMismatch="NEUTRAL" />
<appender-ref ref="log4jdbc_file" />
</logger>
<logger
name="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"
level="trace">
<appender-ref ref="Console" />
</logger>
<root level="trace">
<appender-ref ref="Console" />
</root>
</loggers>
</configuration>

Resources