log4j2 in Java project - Can't find output - java

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)-->

Related

Loggers from LOG4J2 doesn't display every logs in console

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>

Daily rolling with limit for backup files with logj4

Im usign log4j2 for logging. I need daily logging with keeping a backup of 5 days logs. My log4j2.xml looks as below. My backup files keep on increasing in number eventhough ive limited the number to 5. Where did i go wrong??
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="300">
<Appenders>
<RollingFile name="roll-by-time-and-size"
fileName="C:\\Users\\ann\\logs\\testing.log"
filePattern="C:\\Users\\ann\\logs\\testing.%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="5"/>
<!-- <DefaultRolloverStrategy>
<Delete basePath="C:\\Users\\ann\\logs" maxDepth="1">
<IfFileName glob="C:\\Users\\ann\\logs\\test.*.log.gz" />
<IfLastModified age="3" />
</Delete>
</DefaultRolloverStrategy> -->
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n">
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="roll-by-time-and-size"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Instead of the max consider using the delete rule.

log4j StringMatchFilter configuration

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"/>

Log4j2 in a Unix environment

I have made a project in which I am creating logs with log4j2.xml. My configuration file works perfectly when I am running it in the eclipse of my windows system. Here is the configuration file which I have used to create the log files.
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Configuration status="WARN">
<Properties>
<Property name="log-path">/home/apps/ora_fin/IGL75D/Disco/splunk/log</Property>
</Properties>
<Appenders>
<RollingFile fileName="${log-path}/SplunkOADC.log" filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log" name="info-log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile fileName="${log-path}/SplunkOADC-trace.log" filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log" name="trace-log">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger level="all" name="com.aviva.uk.gi.NUProcessOSBReportDataParserPkg">
<AppenderRef ref="info-log"/>
<AppenderRef ref="trace-log"/>
</Logger>
<Root>
</Root>
</Loggers>
</Configuration>
This configuration file I have put under the src folder in my project and is picked up without any issues. However I need to deploy the application in the Unix environment, where should I put the configuration file in unix environment so that it is picked up by the java class to log ??
Thanks!
How about using a jvm argument always
-Dlog4j.configuration={path to file}
This should work irrespective of the environment you deploy in.
The simplest thing to do is to just add the config file to the classpath of your application.
For a web app that means putting it in WEB-INF/classes/.
Alternatively you can use the system property suggested in the other answer.

Logging data to different log files using log4j2?

I am currently working on an application which will generate 2 different log files for different purposes. Since I am new to log4j2, I am unable to achieve it. Here is my configuration file (log4j2.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">C:/Users/460681/Desktop/SourceFiles</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/SplunkOADC.log"
filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/SplunkOADC-error.log"
filePattern="${log-path}/SplunkOADC-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/SplunkOADC-trace.log"
filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="info-log" level="info"/>
<AppenderRef ref="trace-log" level="trace"/>
</Root>
</Loggers>
</Configuration>
I have tried filters but I'm not sure if it is the correct way. Here is my java method which is trying to log using log4j2
logger.entry("Enter The app");
String report_index_data =
"select REPORT_MODE, INDEX_ID from TABLE_NAME";
ResultSet rs = db.selectQuery(report_index_data, conn);
while(rs.next()){
logger.info("report_index_data =" +rs.getString("report_index_data"));
}
logger.exit();
Thanks!
Add a new logger to configuration.
<root level="debug">
<appender-ref ref="info-log" level="info"/>
</root>
<logger name="logger2">
<AppenderRef ref="trace-log" level="trace"/>
</logger>
First, note that if your root logger level is debug, then it will not send trace-level log events to any of its appenders; it will only debug level or higher log events to its appenders.
Second, your code uses logger.entry() and logger.exit(). For these methods to be useful you need to use a pattern layout with location patterns like %location or %method in the pattern. (E.g. [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}.%M - %msg%n). This will display the name of the method you entered/exited. Be aware that calculating location information has some performance impact.

Categories