Spring performance interceptor not logging with log4j2 - spring

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>

Related

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.

How to log request and response in soap message format while consuming a soap service

Consuming a soap web service.
Report report = new Report()
report.setUsername("test");
report.setPassword("test123");
System.out.println("Going to make the request .....");
logger.info("Request :" +report)
SetReportResponse response =(SetReportResponse) soapConnector.callWebService("http://10.88.12.16/ws/_REQUEST_FEEDBACK/services/Feedback.FeedbackHttpSoap11Endpoint/", report);
logger.info(response.getReturn());
How can I log the request and response in xml format, exactly how it send to the client. Just like below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.mogk">
<soapenv:Header/>
<soapenv:Body>
.
.
.
.
.
Has suggested in one post add the below entries in application properties, but it didnt help.
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.server.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.received=TRACE
logging.level.org.springframework.ws.server.MessageTracing.received=TRACE
Using Spring Boot 2 and log4j2
Log4j2.xml - Enabled org.springframework.ws.client.MessageTracing
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">D:\Logs/MA</Property>
>
</Properties>
<Appenders>
<Console name="CONSOLE_APPENDER" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- this file will have all the logging done with log.info others will
not come in this file due to usage of the filters -->
<RollingFile append="true" name="FILE_APPENDER_IFNO"
fileName="${log-path}/mk-trace.log" filePattern="${log-path}/msa-trace-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d %p [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- <OnStartupTriggeringPolicy /> -->
<SizeBasedTriggeringPolicy size="10 MB" />
<!-- <TimeBasedTriggeringPolicy interval="24" modulate="true" /> -->
</Policies>
<Filters>
<!-- First deny warn, error and fatal messages -->
<ThresholdFilter level="warn" onMatch="DENY"
onMismatch="NEUTRAL" />
<ThresholdFilter level="error" onMatch="DENY"
onMismatch="NEUTRAL" />
<ThresholdFilter level="fatal" onMatch="DENY"
onMismatch="NEUTRAL" />
<!-- Then accept info, warn, error, fatal and deny debug/trace -->
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="DENY" />
</Filters>
</RollingFile>
<!-- this fill will contain all the errors like log.error -->
<RollingFile append="false" name="FILE_APPENDER_ERROR"
fileName="${log-path}/mk-error.log" filePattern="${log-path}/msa-error-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d %p [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
<TimeBasedTriggeringPolicy interval="24"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="FILE_APPENDER_IFNO" level="INFO" />
<AppenderRef ref="FILE_APPENDER_ERROR" level="ERROR" />
<AppenderRef ref="CONSOLE_APPENDER" level="INFO" />
</Root>
<logger name="org.springframework.ws.client.MessageTracing">
<level value="TRACE" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</logger>
<!-- Spring Webservice XML -->
<logger name="org.springframework.ws.server.MessageTracing.received">
<level value="TRACE" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</logger>
<logger name="org.springframework.ws.server.MessageTracing.sent">
<level value="TRACE" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</logger>
</Loggers>
<!-- TRACE->DEBUG->INFO->WARN->ERROR->FATAL -->
</Configuration>

log4j - not writing to file when using executable jar

I'm using Spring Boot and creating an executable jar file. We have log4j2 configured. When the app starts, it creates the log file as designated in the log4j2-spring.xml file but never writes to it. Hot changes to the log level in the config file also does not work...expected I guess since it's not writing to the file anyway. My config is below. All paths have been double and triple checked and appear proper.
It should be noted that spring apparently has some log configured. In the jar file, I have removed the xml config to ensure it's not reading from anything in the jar file...yet logging is happening to the console but never in the file...and, again, changing the level has no effect.
FILE: log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="PID">????</Property>
<property name="name">AddressValidation</property>
<Property name="log.file.path">/opt/java/logs</Property>
<Property name="log.file.archive.path">/opt/java/logs/archive</Property>
<Property name="log.file">AddressValidation</Property>
<Property name="log.file.size">500MB</Property>
<Property name="log.file.max-files">10</Property>
<Property name="log.level">ERROR</Property>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
</Properties>
<Appenders>
<RollingFile name="file" fileName="${log.file.path}/${log.file}.log" filePattern="${log.file.archive.path}/${log.file}-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %c{1}(%M:%L) : %m%n%wEx" />
<Policies>
<TimeBasedTriggeringPolicy /><!-- Rotated everyday -->
<SizeBasedTriggeringPolicy size="${log.file.size}" /> <!-- Or every 100 MB -->
</Policies>
<DefaultRolloverStrategy max="${log.file.max-files}"/>
</RollingFile>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%t]}{faint} %clr{%c{1}(%M:%L)}{cyan} %clr{:}{faint} %m%n%wEx" />
</Console>
</Appenders>
<Loggers>
<AsyncLogger name="org.hibernate.validator.internal.util.Version" level="warn" additivity="false" />
<AsyncLogger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" additivity="false" />
<AsyncLogger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" additivity="false" />
<AsyncLogger name="org.apache.catalina.startup.DigesterFactory" level="error" additivity="false" />
<AsyncLogger name="org.springframework.web" level="error" />
<AsyncLogger name="com.netflix" level="error" additivity="false" />
<AsyncLogger name="org.springframework.cloud" level="error" additivity="false" />
<AsyncLogger name="com.abc.addressvalidation" level="ERROR">
<AppenderRef ref="file"/>
</AsyncLogger>
<Root level="ERROR">
<AppenderRef ref="Console"/>
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
I call it as follows:
java -Dlog4j.configurationFile=C:\test\config\log4j2-spring.xml -jar AddressValidation-0.0.1-SNAPSHOT.jar
I'm running out of ideas here. I find lots of info here on StackOverflow on this. It's all pretty much the same advice that I think I'm following.

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