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"/>
Related
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>
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'm working on web project using jsf and jpa I also use log4j to log mu webapp
the problem is the html lof file look the same as sample log file there is no html layout the same thing with my xml layout
everything fine logging and creating files but there is no layout
the three appenders produce the same log formatt
here is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="all">
<Properties>
<Property name="baseDir">webapp_logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="DailyRollingLog" fileName="${baseDir}/mylog.log"
filePattern="${baseDir}/$${date:yyyy-MM}/prime_hms-%d{yyyy-MM-dd-HH-mm}.log">
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<PatternLayout pattern="%d %p %c{1.} [%t] %m%"/>
</RollingFile >
<RollingFile name="DailyRollingHTML" fileName="${baseDir}/mylog.html"
filePattern="${baseDir}/$${date:yyyy-MM}/prime_hms-%d{yyyy-MM-dd-HH-mm}.html">
<param name="immediateFlush" value="true"/>
<layout class="org.apache.log4j.HTMLLayout">
<param name="LocationInfo" value="true"/>
</layout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
<RollingFile name="DailyRollingXML" fileName="${baseDir}/mylog.xml"
filePattern="${baseDir}/$${date:yyyy-MM}/prime_hms-%d{yyyy-MM-dd-HH-mm}.xml">
<layout class="org.apache.log4j.XMLLayout">
<param name="properties" value="true"/>
<param name="LocationInfo" value="true"/>
<param name="complete" value="true"/>
</layout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
<AppenderRef ref="DailyRollingLog"/>
<AppenderRef ref="DailyRollingHTML"/>
<AppenderRef ref="DailyRollingXML"/>
</Root>
</Loggers>
</Configuration>
With Log4j 2 you never specify a class attribute with a Class name. Log4j 2 uses plugins. You should have:
<HTMLLayout locationInfo="true"/>
and
<XMLLayout locationInfo="true" complete="true" properties="true"/>
i am using Log4j/log4j extras for logging the logs.Heres the configuration
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="fileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true" />
<rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="minIndex" value="1"/>
<param name="maxIndex" value="10"/>
<param name="FileNamePattern" value="/appserver/Logs/Logs1/log.%i.gz"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="maxFileSize" value="1000000000"/><!-- 1GB -->
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd MMM yy HH:mm:ss:SSS}] [%p] - [%t %l]- %m%n" />
</layout>
</appender>
<!--disable logging from the library Jars -->
<category name="org.apache.velocity">
<priority value="off" />
</category>
<category name="com.someting.www">
<priority value="off" />
</category>
<category name="org.apache">
<priority value="off" />
</category>
<root>
<level value="DEBUG" />
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
I am expecting logs to come only in the file but i am getting logs written in both file and console.
Please suggest the change for getting expected results.
I am using log4j2 in my new project.I am getting logs written in both file and console.
This is my log4j2.xml:
<properties>
<property name="logPath">/opt/weego_main/logs</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n"/>
</Console>
<RollingFile name="RollingFile" filename="${logPath}/weego_main.log"
filepattern="${logPath}/$${date:yyyy-MM}/weego_main-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
I think you should create Appenders node named Console and add it to Root node.
I'm use to RollingFileAppender on normal log4j. Now I'm switching to log4j2, and cannot get the appender to work.
The File appender below works as expected. But the logging file for RollingFile is never created. Why?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FILE" fileName="c:/logs.log">
<PatternLayout pattern="%d %p %c: %m%n" />
</File>
<RollingFile name="ROLLING" fileName="c:/logsroll.log">
<PatternLayout pattern="%d %p %c: %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="0.001 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="FILE" />
<AppenderRef ref="ROLLING" />
</Root>
</Loggers>
</Configuration>
The RollingFile tag is missing a filePattern attribute.
<RollingFile name="ROLLING"
fileName="c:/logsroll.log"
filePattern="c:/logsroll-%i.log">
I used log4j2 version 2.0, in some cases it throws error if you do not set any date in file pattern, in this case you can use some thing like below:
<RollingFile name="MyFile" fileName="d:/log/bsi/admin/total/totalLog.log"
filePattern="d:/log/totalLog-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="2000"/>
</RollingFile>