How to log CXF webservice requests with log4j2? - java

I'd like to log all incoming and outgoing CXF requests to a specific logfile. But all I get with the following configuration is a console output. What is wrong here?
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="CXF" fileName="cxf.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="org.apache.cxf" additivity="false" level="info">
<AppenderRef ref="CXF"/>
</logger>
<logger name="org.apache.cxf.interceptor.LoggingInInterceptor" additivity="false" level="info">
<AppenderRef ref="CXF" />
</logger>
<logger name="org.apache.cxf.interceptor.LoggingOutInterceptor" additivity="false" level="info">
<AppenderRef ref="CXF" />
</logger>
<Root level="all">
<AppenderRef ref="CONSOLE" />
</Root>
</Loggers>
</Configuration>
src/main/resources/META-INF/cxf/org.apache.cxf.Logger:
org.apache.cxf.common.logging.Log4jLogger

As a workaround, I'm now using org.apache.cxf.common.logging.Slf4jLogger and the bridge dependency.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0</version>
</dependency>

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<property name="main">logs/main.log</property>
<property name="webservice">logs/webservice.log</property>
<property name="sql">logs/sql.log</property>
<property name="mongoexceptions">logs/mongoexceptions.log</property>
</Properties>
<Appenders>
<RollingFile name="mongoexceptions-all" fileName="${mongoexceptions}"
filePattern="${mongoexceptions}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="main-all" fileName="${main}"
filePattern="${main}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="webservice-all" fileName="${webservice}" filePattern="${webservice}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m %n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
<RollingFile name="sql-all" fileName="${sql}" filePattern="${sql}.%i" bufferedIO="true" bufferSize="8192">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="1000KB" />
</Policies>
<DefaultRolloverStrategy max="1"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="main-all"/>
</Root>
<Logger name="org.apache.cxf" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="org.springframework" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="org.eclipse.jetty" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="com.main.bam.db.edp.EventAuditDAO" level="error" additivity="false">
<appender-ref ref="mongoexceptions-all" />
</Logger>
<Logger name="com.main.bam.db.edp.ExceptionsLogDAO" level="error" additivity="false">
<appender-ref ref="mongoexceptions-all" />
</Logger>
<Logger name="com.main.bam.db.edp.ClaimDAO" level="debug">
<appender-ref ref="webservice-all" />
</Logger>
<Logger name="com.main.bam.db.edp.CPDSummaryDAO" level="debug">
<appender-ref ref="webservice-all" />
</Logger>
<Logger name="DBConnectionFactory" level="info">
<appender-ref ref="sql-all"/>
</Logger>
</Loggers>
</Configuration>
Here is the working example.

Related

How to split catalina.out for tomcat?

I have log4j2.xml properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<RollingFile name="RollingFile"
fileName="/opt/tomcat/mylogs/logs.log"
filePattern="/opt/tomcat/mylogs/$${date:yyyy-MM}/log-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches 50 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" level="info" />
<AppenderRef ref="RollingFile" level="info" />
<AppenderRef ref="SmtpAppender" level="exception"/>
</Root>
<Logger name="com.kot" level="debug">
<AppenderRef ref="RollingFile"/>
</Logger>
</Loggers>
</Configuration>
But this properteis create logg files on another folder on server: "/opt/tomcat/mylogs/logs.log"
I want to split catalina.out logs. This logs are located on "/opt/tomcat/logs/catalina.out" by default
Also, how to split server logs and application logs?

How to configure log4j2.xml from file to stdout?

Here is my current log4j2.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="logFilename">abc</Property>
<Property name="metricsLogFilename">abc-metrics</Property>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="metricsPattern">%d : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
<RollingRandomAccessFile name="file"
fileName="${tex:appLogFilePath}${logFilename}.log"
filePattern="${tex:appLogFilePath}${logFilename}-%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="metricsFile"
fileName="${tex:appLogFilePath}${metricsLogFilename}.log"
filePattern="${tex:appLogFilePath}${metricsLogFilename}-%i.log.gz"
immediateFlush="false">
<PatternLayout pattern="${metricsPattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="metricsFile" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Root>
</Loggers>
I want to switch to stdout instead of file logs. What should I change ? (I've never used log4j)
The reason why I want to have it stdout is because I'll need to deploy my app on heroku, and on heroku we need to use system out logging.
As per current configuration, except metrics, your are logging at both places - in a file as well as on console. For metrices, you are logging in file only.
If you can want to log only at console, remove other appender and their references from logger configuration. For metrices, you have to refer console appender.
Update file may look like -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
</Root>
</Loggers>

Log4j2 Delete on Rollover is not working as expected, in Pingfederate

In Pingfederate, which internally uses log4j2 to log events. I am trying to rollover and delete older logs. While rollover functionality works, and the log is rolled over, but the deletion functionality doesn't seem to work. What do i don't understand ? Kindly explain and help me fix this.
Also, I am trying to log log4j2 itself, but for some reason it is not getting logged in the console logs.
Thanks a bunch.
If you need more information, kindly comment.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
</Console>
<!-- Main log : A size based file rolling appender -->
<RollingFile name="FILE" fileName="${sys:pf.log.dir}/server_${sys:pf.ip}.log" filePattern="${sys:pf.log.dir}/server_${sys:pf.ip}.log.%i" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d %X{trackingid} %-5p [%c] %enc{%m}%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000 KB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<!-- Security Audit log : A time/date based rolling appender -->
<RollingFile name="SecurityAudit2File" fileName="${sys:pf.log.dir}/audit_${sys:pf.ip}.log" filePattern="${sys:pf.log.dir}/audit_${sys:pf.ip}.%d{yyyy-MM-dd:HH:mm}.log" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:pf.log.dir}" maxDepth="1">
<IfFileName glob="audit*.log" />
<IfLastModified age="2m" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<!-- Admin Audit log : A time/date based rolling appender -->
<!--
<RollingFile name="AdminAudit" fileName="${sys:pf.log.dir}/admin.log" filePattern="${sys:pf.log.dir}/admin.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%X{user}|%X{roles}|%X{event}|%m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
-->
<!-- Admin API Audit log : A time/date based rolling appender -->
<!--
<RollingFile name="AdminApiAudit" fileName="${sys:pf.log.dir}/admin-api.log" filePattern="${sys:pf.log.dir}/admin-api.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%X{api.user}|%X{api.authtype}|%X{api.clientip}|%X{api.httpmethod}|%X{api.url}|%X{api.status}%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
-->
<!-- Runtime API Audit log : A time/date based rolling appender -->
<RollingFile name="RuntimeApiAudit" fileName="${sys:pf.log.dir}/runtime-api.log" filePattern="${sys:pf.log.dir}/runtime-api.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%enc{%X{api.user}}|%X{api.authtype}|%X{api.clientip}|%X{api.httpmethod}|%X{api.url}|%X{api.status}%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="httpclient.wire.content" level="WARN" />
<!-- Adjust the priority value to DEBUG to get additional logging to help troubleshoot XML Signature problems -->
<Logger name="org.sourceid.common.dsig" level="WARN" />
<!-- set to DEBUG to see inbound and outbound protocol messages -->
<Logger name="org.sourceid.saml20.bindings.LoggingInterceptor" level="WARN"/>
<Logger name="org.eclipse.jetty" level="WARN" />
<Logger name="org.eclipse.jetty.io.nio" level="WARN" />
<Logger name="org.apache.logging.log4j" level="TRACE">
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.sourceid.websso.profiles.sp.SpAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.websso.profiles.idp.IdpAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.websso.profiles.idp.AsAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.wstrust.log.STSAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<!--
<Logger name="AuditLogger" level="INFO" additivity="false">
<appender-ref ref="AdminAudit" />
</Logger>
<Logger name="AdminApiAuditLogger" level="INFO" additivity="false" includeLocation="false">
<appender-ref ref="AdminApiAudit" />
</Logger>
-->
<Logger name="RuntimeApiAuditLogger" level="INFO" additivity="false" includeLocation="false">
<appender-ref ref="RuntimeApiAudit" />
</Logger>
<Root level="TRACE">
<AppenderRef ref="CONSOLE" />
</Root>
</Loggers>
</Configuration>

Log4j2: Suppress only DEBUG messages

I am getting a lot of DEBUG messages in my logs. I want to suppress these, while preserving the INFO and ERROR messages. Here is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- Author: Crunchify.com -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
</Console>
<RollingFile name="RollingFile" filename="test.log"
filepattern="${LOG_PATH}${LOG_NAME}Log.%d{yyyyMMdd}.log.gz">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<NoSql name="elasticAppender">
<Elasticsearch cluster="test" host="test" port="test" index="test" type="log4j2"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="NoSql" />
<AppenderRef ref="RollingFile" />
</Root>
<Logger name="test" level="info" additivity="false">
<AppenderRef ref="RollingFile" />
</Logger>
<Logger name="test" level="info">
</Logger>
<Logger name="test" level="INFO">
</Logger>
<Logger name="com.datastax.driver.core" level="INFO">
</Logger>
</Loggers>
</Configuration>
Does anyone know what change needs to be made here?
According to the Log4j manual, you might want to change <Root level="debug"> to <Root level="info">.

Turn off log4j2 startup debug logging

How do I turn off the debug logging that log4j2 spits out when it initialises itself?
These sort of things:
2014-10-22 11:16:45,505 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter]. Searching for builder factory method...
2014-10-22 11:16:45,505 DEBUG No builder factory method found in class org.apache.logging.log4j.core.filter.ThresholdFilter. Going to try finding a factory method instead.
2014-10-22 11:16:45,505 DEBUG Still building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter]. Searching for factory method...
I'm calling log4j2 from a simple Java application via eclipse. By that I mean:
public static void main(String[] args) { ... }
The loggers portion of the xml config is:
<Loggers>
<Root level="trace"/>
<logger name="audit">
<appender-ref ref="AUDITOUT"/>
<appender-ref ref="DEBUGOUT"/>
<appender-ref ref="ORACLEOUTINFO"/>
<appender-ref ref="ORACLEOUTWARN"/>
<appender-ref ref="ORACLEOUTERROR"/>
<appender-ref ref="ORACLEOUTFATAL"/>
</logger>
<logger name="org.apache.log4j">
<appender-ref ref="FILEOUT"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="ORACLEOUTWARN"/>
<appender-ref ref="ORACLEOUTERROR"/>
<appender-ref ref="ORACLEOUTFATAL"/>
</logger>
<logger name="jh.Runner2">
<appender-ref ref="DEBUGOUT"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="ORACLEOUTTRACE"/>
<appender-ref ref="ORACLEOUTDEBUG"/>
<appender-ref ref="ORACLEOUTINFO"/>
<appender-ref ref="ORACLEOUTWARN"/>
<appender-ref ref="ORACLEOUTERROR"/>
<appender-ref ref="ORACLEOUTFATAL"/>
</logger>
</Loggers>
Appenders:
<Appenders>
<Console name="STDOUT">
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %p [%t] - %m%n"/>
</Console>
<RollingFile name="FILEOUT" fileName="${log-path}/mainlog2.log" filePattern="${log-path}/mainlog-%d{yyyy-MM-dd}.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %p [%t] - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="DEBUGOUT" fileName="${log-path}/debuglog2.log" filePattern="${log-path}/debuglog-%d{yyyy-MM-dd}.log">
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %p %C [%t] - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="AUDITOUT" fileName="${log-path}/audit2.log" filePattern="${log-path}/auditlog-%d{yyyy-MM-dd}.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %p [%t] - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<JDBC name="ORACLEOUTTRACE" tableName="J0T_EVENT">
<Filters>
<ThresholdFilter level="DEBUG" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<ConnectionFactory class="com.xxxxxxxxx.db.LoggingDBConnectionFactory" method="getDatabaseConnection" />
<Column name="EVENT_ID" literal="${nextEventID}"/>
<Column name="APP_NM" literal="${appName}"/>
<Column name="CREATE_DT" isEventTimestamp="true"/>
<Column name="EVENT_TYPE_ID" literal="1"/>
<Column name="EVENT_TXT" pattern="${eventTxtPattern}" isUnicode="false"/>
</JDBC>
etc.
Your configuration starts with
<Configuration status="debug" ...
Change this to
<Configuration status="warn" ...
and you will only see WARN-level internal log4j messages (which is probably what you want).

Categories

Resources