log4j2 unable to create INFO level log - spring-boot

I have Log4j2 in my spring boot application and wish to log event into 2 log files. The UserService is unable to create the log for INFO level, but able to create for WARN level. Can anyone advise?
Log4j2.xml
<Configuration>
<Appenders>
<RollingFile name="matchFile"
fileName="${log-path}/match.log"
immediateFlush="true">
<Filters>
<ThresholdFilter level="trace" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="debug" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="info" onMAtch="accept" onMismatch="deny" />
<ThresholdFilter level="warn" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="error" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="fatal" onMAtch="deny" onMismatch="neutral" />
</Filters>
</RollingFile>
<RollingFile name="confirmFile"
fileName="${log-path}/confirm.log"
immediateFlush="true">
<Filters>
<ThresholdFilter level="trace" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="debug" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="info" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="warn" onMAtch="accept" onMismatch="deny" />
<ThresholdFilter level="error" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="fatal" onMAtch="deny" onMismatch="neutral" />
</Filters>
</RollingFile>
</Appenders>
</Configuration>
UserService.java
public class UserService{
Logger matchLogger = LogManager.getLogger("matchFile");
Logger confirmLogger = LogManager.getLogger("confirmFile");
public void log(){
matchLogger.info("print info"); //not able to log this message
confirmLogger.warn("print warn"); //able to log this message
}
}

You're missing the <Loggers> section in your config, where you declare the logger names to use in your code and attach appenders to them.
Also, the ThresholdFilter actually declares a level threshold, so that there's no need to specify a filter for each level.
Your config should look something like this:
<Configuration>
<Appenders>
<RollingFile name="matchFile"
fileName="${log-pathmatchFile}/match.log"
immediateFlush="true">
<Filters>
<!-- deny warn, error and fatal messages -->
<ThresholdFilter level="warn" onMatch="deny" onMismatch="neutral" />
<!-- accept info, warn, error, fatal and deny debug, trace -->
<ThresholdFilter level="info" onMatch="accept" onMismatch="deny" />
</Filters>
</RollingFile>
<RollingFile name="confirmFile"
fileName="${log-path}/confirm.log"
immediateFlush="true">
<Filters>
<!-- deny error and fatal messages -->
<ThresholdFilter level="error" onMatch="deny" onMismatch="neutral" />
<!-- accept warn, error, fatal and deny info, debug, trace -->
<ThresholdFilter level="warn" onMatch="accept" onMismatch="deny" />
</Filters>
</RollingFile>
</Appenders>
<Loggers>
<!-- probably declare the root logger level and appenders:
<Root level="...">
<AppenderRef ref="..." />
</Root>
-->
<Logger name="matchFile" level="info">
<AppenderRef ref="matchFile" />
</Logger>
<Logger name="confirmFile" level="warn">
<AppenderRef ref="confirmFile" />
</Logger>
</Loggers>
</Configuration>

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.

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.

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