Here is my log4j2.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<!--
Fichier de configuration des loggers
#author theo.lalande
Hiérarchie des lvl:
ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<OFF
-->
<Configuration status="INFO" monitorInterval="30">
<!-- PROPERTIES ______________________________________________________________________________ -->
<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 ______________________________________________________________________________ -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
</Console>
<!-- POUR TOUS LES LOGS -->
<RollingFile name="FileAppenderAllLogs"
fileName="logs/uService_all_logs.log"
filePattern="logs/uService_all_logs-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy
max="7" />
</RollingFile>
<!-- POUR LES LOGS DE NIVEAU INFO -->
<RollingFile name="FileAppenderInfoLogs"
fileName="logs/uService_info_logs.log"
filePattern="logs/uService_info_logs-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="WARN" onMatch="DENY"
onMismatch="ACCEPT" />
</Filters>
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="7" />
</RollingFile>
<!-- POUR LES LOGS DE NIVEAU WARN -->
<RollingFile name="FileAppenderWarnLogs"
fileName="logs/uService_warn_logs.log"
filePattern="logs/uService_warn_logs-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY"
onMismatch="ACCEPT" />
</Filters>
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="7" />
</RollingFile>
<!-- POUR LES LOGS DE NIVEAU ERROR -->
<RollingFile name="FileAppenderErrorLogs"
fileName="logs/uService_error_logs.log"
filePattern="logs/uService_error_logs-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter
level="FATAL" onMatch="DENY" onMismatch="ACCEPT" />
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy
max="7" />
</RollingFile>
<!-- POUR LES LOGS DE NIVEAU FATAL -->
<RollingFile name="FileAppenderFatalLogs"
fileName="logs/uService_fatal_logs.log"
filePattern="logs/uService_fatal_logs-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter
level="OFF" onMatch="DENY" onMismatch="ACCEPT" />
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy
max="7" />
</RollingFile>
</Appenders>
<!-- LOGGERS ______________________________________________________________________________ -->
<Loggers>
<Root level="INFO" additivity="true">
<priority value="INFO" />
<AppenderRef ref="FileAppenderAllLogs" level="INFO" />
<AppenderRef ref="Console" level="INFO" />
</Root>
<Logger name="com.compufirst.outstanding" level="INFO" additivity="false">
<AppenderRef ref="FileAppenderInfoLogs" level="INFO" />
<AppenderRef ref="FileAppenderWarnLogs" level="WARN" />
<AppenderRef ref="FileAppenderErrorLogs" level="ERROR" />
<AppenderRef ref="FileAppenderFatalLogs" level="FATAL" />
</Logger>
</Loggers>
</Configuration>
Every logs are split between different files according their level.
In the software entry point there are those logs :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
#SpringBootApplication
public class OutstandingPoc1Application {
private static final Logger logger = LogManager.getLogger(OutstandingPoc1Application.class);
public static void main(String[] args) {
SpringApplication.run(OutstandingPoc1Application.class, args);
Configurator.setAllLevels(logger.getName(), Level.TRACE);
logger.debug("Debugging log");
logger.info("Info log");
logger.warn("Hey, This is a warning1!");
logger.error("Oops! We have an Error. OK");
logger.fatal("Damn! Fatal error. Please fix me.");
}
}
My problems are:
The logs in the soft entry point never appears in uService_all_logs.log and only INFO and WARN level are written when i would like to have every logs (except debug and trace logs)
Console never display logs from the entry point.
Any Idea?
No messages from the "com.compufirst.outstanding" logger or its children will appear on the console or uService_all_logs.log, because you set additivity="false" on the logger.
BTW: you can simplify your configuration greatly by using the routing appender to define per-level log files:
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="FileAppenderAllLogs"
fileName="logs/uService_all_logs.log"
filePattern="logs/uService_all_logs-%i.log">
<PatternLayout pattern="${LOG_PATTERN}"/>
<SizeBasedTriggeringPolicy size="10MB"/>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
<Routing name="FileAppenderPerLevelLogs">
<Routes pattern="$${event:Level}">
<Route>
<!-- Appender template -->
<RollingFile name="FileAppender${event:Level}Logs"
fileName="logs/uService_${event:Level}_logs.log"
filePattern="logs/uService_${event:Level}_logs-%i.log">
<PatternLayout pattern="${LOG_PATTERN}"/>
<SizeBasedTriggeringPolicy size="10MB"/>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Route>
</Routes>
</RollingFile>
<Root level="INFO">
<AppenderRef ref="FileAppenderAllLogs"/>
<AppenderRef ref="Console"/>
</Root>
<Logger name="com.compufirst.outstanding">
<AppenderRef ref="FileAppenderPerLevelLogs"/>
</Loggers>
Related
Requirement is to change the log level for a specific user . Using DynamicThresholdFilter at the context level does work but applies to all the loggers . My requirement is to only apply it for specific loggers and not impact the others. With this configuration file all the appenders are logging at debug levell.
This is how my file looks like . Any recommendations on applying the same filter or any other filter which can enable debug logging for specific loggers ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Properties>
---------------------
</Properties>
<DynamicThresholdFilter
key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG" />
</DynamicThresholdFilter>
<Appenders>
<Console name="consoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" />
</Console>
<RollingFile name="customLoggingAppender"
fileName="${sys:log_dir}/customLogging.log"
filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<ThreadContextMapFilter onMatch="DENY"
onMismatch="NEUTRAL">
<KeyValuePair key="customLogging-enabled" value="true" />
</ThreadContextMapFilter>
<PatternLayout>
.......................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<RollingFile name="standardAppender"
fileName="${sys:log_dir}/standard.log"
filePattern="standard-%d{yyyy-MM-dd}-%i.log" append="true">
<PatternLayout>
....................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
.......More appenders here
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="consoleAppender" />
<AppenderRef ref="standardAppender" />
</Root>
<!-- DEFAULTED TO ROOT LOGGER - START -->
<Logger name="com.abc" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.helpers" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.business.persist.utils" level="ERROR" />
<Logger
name="com.abc.business.persist.ManagerFactory"
level="ERROR" />
<Logger name="com.da.handlers" level="ERROR" additivity="false">
<AppenderRef ref="abcAppender" />
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.handler.audit" level="ERROR"
additivity="false">
<AppenderRef ref="xyzAppender" />
</Logger>
</Loggers>
</Configuration>
I tried applying the same filter at the appender level but it did not work
<RollingFile name="customLoggingAppender" fileName="${sys:log_dir}/customLogging.log" filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<DynamicThresholdFilter key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>
<PatternLayout>
....
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
My expectation is that ONLY the loggers referring to this appender will be logging at debug level
I have a storm bolt that is responsible for extracting the contents of an email. I would like to log some details about the email in a log file of my choosing instead of in worker.log. The bolt does log to worker.log for errors etc. and I want it to continue to do so.
I've updated the cluster.xml to include the new logger
<configuration monitorInterval="60" shutdownHook="disable">
<properties>
<property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %c{1.} %t [%p] %msg%n</property>
</properties>
<appenders>
<RollingFile name="A1" immediateFlush="false"
fileName="${sys:storm.log.dir}/${sys:logfile.name}"
filePattern="${sys:storm.log.dir}/${sys:logfile.name}.%i.gz">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="9"/>
</RollingFile>
<RollingFile name="TEST-SIZE" immediateFlush="false"
fileName="${sys:storm.log.dir}/test_debug.log"
filePattern="${sys:storm.log.dir}/test_debug.log.%i.gz">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="9"/>
</RollingFile>
<Syslog name="syslog" format="RFC5424" charset="UTF-8" host="localhost" port="514"
protocol="UDP" appName="[${sys:daemon.name}]" mdcId="mdc" includeMDC="true"
facility="LOCAL5" enterpriseNumber="18060" newLine="true" exceptionPattern="%rEx{full}"
messageId="[${sys:user.name}:S0]" id="storm" immediateFlush="true" immediateFail="true"/>
</appenders>
<loggers>
<Logger name="TestSizeLogger" level="info" additivity="false">
<AppenderRef ref="TEST-SIZE"/>
<AppenderRef ref="syslog"/>
</Logger>
<root level="info"> <!-- We log everything -->
<appender-ref ref="A1"/>
<appender-ref ref="syslog"/>
</root>
</logger>
In my Java code I then create two logger instances but they both log to worker.log
static Logger logger = LoggerFactory.getLogger(ExtractorBolt.class);
static Logger testLogger = LoggerFactory.getLogger("TestSizeLogger");
I would appreciate any advice in regards to how I can get my testLogger to log to my new log file.
cluster.xml is for configuring the logging for Nimbus, the supervisor and other daemons. You should edit the log4j2/worker.xml file to configure worker logging.
Im new here, Please help me to understand my new project's log4j2 configuration.
My question is:
How to get all Log outputs?
From where should I search log files?
Also how to save tomcat console outputs in the txt file?
I really appreciate your help and support, Today I want to learn something new from you guys! Thanks!
This is the log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" monitorInterval="1800">
<Properties>
<Property name="LOG_HOME">\Workspaces\logs\paymentweb</Property>
<Property name="LOG_DEBUG">${LOG_HOME}\app\debug.log</Property>
<Property name="LOG_INFO">${LOG_HOME}\app\info.log</Property>
<Property name="LOG_ERROR">${LOG_HOME}\app\error.log</Property>
</Properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
(onMismatch)-->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</Console>
<RollingRandomAccessFile name="app_debug" fileName="${LOG_DEBUG}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\debug-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingRandomAccessFile>
<CustomRollingRandomAccessFile name="app_info" fileName="${LOG_INFO}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\info-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</CustomRollingRandomAccessFile>
<CustomRollingRandomAccessFile name="app_error" fileName="${LOG_ERROR}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\error-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</CustomRollingRandomAccessFile>
</appenders>
<loggers>
<root level="trace" additivity="false">
<appender-ref ref="Console"/>
<appender-ref ref="app_debug"/>
<appender-ref ref="app_info"/>
<appender-ref ref="app_error"/>
</root>
</loggers>
</configuration>
all your logs are in \Workspaces\logs\paymentweb\app*.log
You can find out details about how your appenders are configured by enabling internal Log4j2 "status" logging.
At the top of your configuration file, change it to TRACE <configuration status="trace" monitorInterval="1800">. I believe this will show the full path of the file appenders on the console. (No guarantees for the custom appender CustomRollingRandomAccessFile.)
Also, you have a non-XML compliant snippet inside the <Console> appender configuration. This looks bad and should be removed:
(onMismatch)-->
I recently enabled my log4j2 configuration to generate a CSV file from the loggers. The CSV file is generating fine. However, only the log statements from the web service client classes are realized in the CSV i.e. log statements from managers, action classes (Struts 2 Actions), Utility Classes, Mapper classes and web service request/response printing handlers are not being generated at all in the CSV file. What could be the reason for this?
Here is my code :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="infoLog.filename">/logs/info.log</Property>
<Property name="debugLog.filename">/logs/debug.log</Property>
<Property name="errorLog.filename">/logs/error.log</Property>
<Property name="communicationLog.filename">/logs/communication.log</Property>
<Property name="csvLog.fileName">/logs/csvLog</Property>
<Property name="file-header">column1,column2,column3</Property>
</Properties>
<Appenders>
<RollingFile name="csvFile" fileName="${csvLog.fileName}.csv"
filePattern="${csvLog.fileName}-%d{MM-dd-yyyy}-%i.csv">
<BurstFilter level="WARN" rate="16" maxBurst="100"/>
<CsvParameterLayout delimiter="," header="${file-header}\n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="debugFile" fileName="${debugLog.filename}"
filePattern="${debugLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%-5p [%t] -%-10c (%L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="infoFile" fileName="${infoLog.filename}"
filePattern="${infoLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="errorFile" fileName="${errorLog.filename}"
filePattern="${errorLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<RollingFile name="communicationLog" fileName="${communicationLog.filename}"
filePattern="${communicationLog.filename}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %X{userId} [%t] -%-10c (%-L):-(%method) %m%n%throwable" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | %X{userId} [%t] -%-10c (%-L):-(%method) %m%n" />
</Console>
</Appenders>
<Loggers>
<logger name="com.syb.core.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="com.syb.as.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="com.syb.ao.manager" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="communicationLog" />
<appender-ref ref="csvFile" />
</logger>
<logger name="error.unhandled" additivity="false">
<level value="ERROR" />
<appender-ref ref="STDOUT" level="INFO" />
<appender-ref ref="errorFile" level="ERROR" />
<appender-ref ref="csvFile" />
</logger>
<Root level="debug" additivity="false">
<AppenderRef ref="debugFile" level="debug"/>
<AppenderRef ref="errorFile" level = "error"/>
<AppenderRef ref="infoFile" level="info"/>
<AppenderRef ref="STDOUT" level="info"/>
<AppenderRef ref="csvFile" level="all"/>
</Root>
</Loggers>
Is there anything that could be done to enable CSV record generation in CSV files for log statements throughout the application? It would be really great if you could help.
I want to log the message containing "Here is DEBUG"
my log4j2.xml is like this:
<Appenders>
<!-- Console Appender -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p %c{1} - %m%n" />
</Console>
<!--RollingFile Appender-->
<RollingFile name="rollingFile" fileName="${sys:catalina.base}/logs/${project.name}.log" filePattern="${sys:catalina.base}/logs/${project.name}-%i.log">
<PatternLayout>
<Pattern>%p %d{dd-MMMMMMMMM-yyyy HH:mm:ss:SSS} %m %n%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500kb" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<filter class="org.apache.log4j.varia.StringMatchFilter">
<param name="StringToMatch" value="Here is DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
</Appenders>
<Loggers>
<Root level="debug">
<!--AppenderRef ref="console" /-->
<AppenderRef ref="rollingFile" />
</Root>
</Loggers>
and my java code is :
log.info("Here is DEBUG");
log.info("XXXXXXXXXXXX");
log.warn("this is a warning");
log.error("this is an error");
But I still got everything in the log.
OK I figured it out. log4j 1 and 2 are very different on the configuration. in 2, the filter should look like
<RegexFilter regex="DEBUG .*" onMatch="ACCEPT" onMismatch="DENY"/>