Log4j2.xml configuration file not found after configuration change - java

I'm having a problem with the log4j2.xml configuration file not being found after a change to the configuration file was made. Before the change the file was found and the simple logging was working. I haven't used log4j in a few years and have been making use of the java.util logging for application logging on the weblogic server so I decided to put log4j to use again.
I first create a simple configuration file, tested it out and everything was working just fine.
The original test configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="filename">/opt/www/log</Property>
</Properties>
<Appenders>
<RollingFile name="StipLog" fileName="${filename}/stip-log.log" filePattern="${filename}/stip-log-%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>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="stip-debug" level="debug" additivity="true">
<appender-ref ref="StipLog" level="debug"/>
</Logger>
<Root level="debug">
<AppenderRef ref="StipLog"/>
</Root>
</Loggers>
<Configuraiton>
Runing this code snippet in a backing bean for a page worked just fine with the original configuration file.
LOGGER.info("APP Home Page: User: ".concat(userInfoBean.getUserName()));
LOGGER.debug("DEBUG message!");
LOGGER.error("ERROR Message", new NullPointerException("I AM NULL"));
That all went well so I decided to try and configure different appenders for each log level I wanted to use. I changed the original configuration to the following.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!-- For local logging change this porperty to a local directory. ex: C:\\Public\\log -->
<Property name="LOGGING_ROOT">C:/Public/log</Property>
</Properties>
<Appenders>
<!-- file appenders -->
<RollingFile name="debugLog" fileName="${LOGGING_ROOT}/app-debug.log" filePattern="${LOGGING_ROOT}/app-debug-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<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>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${LOGGING_ROOT}/app-info.log" filePattern="${LOGGING_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<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>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="errorLog" fileName="${LOGGING_ROOT}/app-error.log" filePattern="${LOGGING_ROOT}/app-error-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<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>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<!-- console appender -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="debug-logger" level="debug" additivity="true">
<appender-ref ref="debugLog"/>
</Logger>
<Logger name="info-logger" level="info" additivity="true">
<appender-ref ref="infoLog"/>
</Logger>
<Logger name="error-logger" level="error" additivity="true">
<appender-ref ref="errorLog"/>
</Logger>
<Root level="all">
<AppenderRef ref="console"/>
<AppenderRef ref="debugLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="errorLog"/>
</Root>
</Loggers>
</Configuration>
After changing the configuration file I am now getting the following error message when the application is deploying.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
The configuration file is in the same location as it was when the loggin was working, it's in the source default package. I moved it WEB-INF to see if that would make a difference but the same problem remains. I even changed it back to the original configuration and still have the same problem.

Related

Log4j2.17.2 xml config file in a multithreaded environment throws errors when included routing tags

Thanks for trying to help the question. I have been trying to upgrade the log4j from version 2.2.x to 2.17.2 for that I have included following starters, the spring boot version is 2.6.6 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.4</version>
</dependency>
but when I include the routing tag as per my simplified (& partial) logging-config as below, it throws several runtime errors -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<property name="log-path" value="/path/" />
<!-- log levels precedence : ALL < DEBUG < INFO < WARN < ERROR < FATAL <
OFF -->
<property name="log-level" value="INFO" />
<Appenders>
<appender name="console" target="SYSTEM_OUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</encoder>
</appender>
<Routing name="Routing">
<Routes pattern="$${ctx:docURN}">
<Route>
<appender name="LogFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log-path}/filename.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log-path}/filename-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id=
${ctx:docURN} - %msg%n</pattern>
</encoder>
</appender>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<logger name="com.name" level="${log-level}" additivity="false">
<appender-ref ref="LogFile" level="${log-level}" />
<appender-ref ref="Routing" level="${log-level}" />
</logger>
<root level="info" additivity="false">
<appender-ref ref="Routing" />
</root>
</Loggers>
</Configuration>
I apologize, if the question appears duplicate but I have searched all over the stackoverflow and internet and the apache documentation is so overwhelming, it seems to be correct as per the documentation.
Thank you again for your help.
Edit - runtime errors thrown are for all the nodes in the logging-config.xml -
java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter#10:13 - no applicable action for [Appenders], current ElementPath is [[Configuration][Appenders]]
Edit2 - I tried following configuration example as it is from https://logging.apache.org/log4j/2.x/manual/configuration.html , still it throws those errors.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="RoutingTest" packages="org.apache.logging.log4j.test">
<Properties>
<Property name="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
</Properties>
<ThresholdFilter level="debug"/>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%m%n"/>
<ThresholdFilter level="debug"/>
</Console>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${filename}"
filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
</Route>
<Route ref="STDOUT" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Logger name="EventLogger" level="info" additivity="false">
<AppenderRef ref="Routing"/>
</Logger>
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
Edit3 - This is the resultant logging-config.xml I have now -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<property name="log-path">/logfiles//</property>
<!-- log levels precedence : ALL < DEBUG < INFO < WARN < ERROR < FATAL
< OFF -->
<property name="log-level">INFO</property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<Routing name="Routing">
<Routes pattern="$${ctx:docURN}">
<Route>
<RollingFile name="LogFile" append="true"
fileName="${log-path}/-services.log" filePattern="${log-path}/-services-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id= ${ctx:docURN} - %msg%n"></pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
<Routing name="ErrorRouting">
<Routes pattern="$${ctx:docURN}">
<Route>
<RollingFile name="ErrorLogFile" append="true"
fileName="${log-path}/-services-error.log" filePattern="${log-path}/-services-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id= ${ctx:docURN} - %msg%n"></pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
<RollingFile name="ServerLogFile" fileName="${log-path}/Server.log" filePattern="${log-path}/Server-%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>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="ServerLogFile" />
<AppenderRef ref="Routing" />
</Root>
<Logger name="com.name" level="${log-level}"
additivity="false">
<appender-ref ref="LogFile" level="${log-level}" />
<appender-ref ref="ErrorLogFile" level="error" />
<appender-ref ref="ServerLogFile" level="${log-level}" />
<appender-ref ref="Routing" level="${log-level}" />
<AppenderRef ref="ErrorRouting" level="error"/>
</Logger>
</Loggers>
</Configuration>

log4j2 async logging setup

I changed my logging from sync to async but I am not sure how to put policies on it. I want to apply my sync logging settings to async logging. Please see below.
// I switched from this
<RollingFile name="fileLogger" fileName="${logPath}/log.log"
filePattern="${logPath}/log-%d{yyyy-MM-dd-hh}-%i.log">
<PatternLayout>
pattern="${logPattern}"/>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
//to this
<File name="prodLog" fileName="${logPath}/log.log">
<PatternLayout
pattern="${logPattern}"/>
</File>
<Async name="asyncLogger" includeLocation="true">
<AppenderRef ref="prodLog"/>
<ArrayBlockingQueue/>
</Async>
I dont think you can use the same policies for the File appender.
To achieve an asynchronous behavior for a rolling file, you could use the RollingRandomAccessFile, just like the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
fileName="logs/rollingrandomaccessfile.log"
filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="guru.springframework.blog.log4j2async" level="debug">
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncLogger>
<Root level="debug">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
More on this in this post: https://springframework.guru/asynchronous-logging-with-log4j-2/
Regards

log4j2 writing method and class

I'm using log4j2 in a java program..
this is the line of code where it is initialized
private static final Logger logger = LogManager.getLogger("application-
log");
and this is the configuration file where the format is decided
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="logApplicativo">../logs/logApplicativo.log</Property>
<Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%m MDC%X%n"/>
</Console>
<RollingRandomAccessFile name="fileLogApplicativo"
fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
dd}-%i.log" immediateFlush="false" append="true"
ignoreExceptions="false" >
<PatternLayout>
<pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="fileLogCdr" fileName="${logCdr}"
filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d %-5p %c{2} - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncRoot level="error">
<AppenderRef ref="STDOUT"/>
</AsyncRoot>
<AsyncLogger name="application-log" level="debug" additivity="false">
<AppenderRef ref="fileLogApplicativo" level="debug"/>>
</AsyncLogger>
<AsyncLogger name="cdr-log" level="debug" additivity="false">
<appender-ref ref="fileLogCdr" level="debug"/>
</AsyncLogger>
</Loggers>
I do not understand why, but class name, code name, line code arent printed. Probably the problem is trivial but I'm going crazy from one day.
Thank you all
If you're using asynchronous loggers or asynchronous appenders try adding includeLocation="true" to your logger or appender configuration.
You can read more about it in the log4j2 manual

log4j: How can I direct logs into different files for a cron and for a webservice?

I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example

How to resolve this error with RoutingAppender in Log4J - "AuditLogger cannot be located"?

I'm getting the following error when I run my Log4j code :
2015-07-04 19:08:04,385 ERROR Appender AuditLogger cannot be located. Route ignored
I'm trying to use the RoutingAppender (I learned about it from this question ).
My log4j config files looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="OutputLogFile.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${sd:type}.log"
filePattern="${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
</Route>
<Route ref="AuditLogger" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="all">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/> <!-- added_in now -->
</Root>
</Loggers>
</Configuration>
And my code is just a simple client/server pair (with some logger.debug("xyz") code spread throughout) .
thanks

Categories

Resources