WireMock unable to set log4j level to INFO instead of DEBUG - java

I'm running JUnit testcases with WireMock. Embedded Jetty Server is configured as follows.
#Before
public void setup() {
final ResponseTemplateTransformer theTemplateTransformer =
new ResponseTemplateTransformer(false);
templateTransformerName = theTemplateTransformer.getName();
mWireMockServer = new WireMockServer(
WireMockConfiguration
.options()
.notifier(new ConsoleNotifier(false))
.extensions(theTemplateTransformer));
mWireMockServer.start();
}
The output of the setup() method alone is very verbose, about 200 lines long. It starts like this:
> Task :processResources
> Task :classes
> Task :compileTestJava
> Task :processTestResources
> Task :testClasses
> Task :test
log4j: Threshold ="null".
log4j: Level value for root is [DEBUG].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [threshold] to [ALL].
log4j: Setting property [target] to [System.out].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] [%t] [%c{1}] [%F:%L] [%X{uuid}] - %m%n].
log4j: Adding appender named [ConsoleAppender] to category [root].
2019-12-09 11:28:06.813 [DEBUG] [Test worker] [log] [Log.java:159] [] - Logging to org.slf4j.impl.Log4jLoggerAdapter(wiremock.org.eclipse.jetty.util.log) via wiremock.org.eclipse.jetty.util.log.Slf4jLog
2019-12-09 11:28:06.820 [ INFO] [Test worker] [log] [Log.java:169] [] - Logging initialized #1394ms to wiremock.org.eclipse.jetty.util.log.Slf4jLog
2019-12-09 11:28:06.841 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - Server#2173e69b{STOPPED}[9.4.20.v20190813] added {QueuedThreadPool[qtp547179568]#209d4c30{STOPPED,8<=0<=14,i=0,r=-1,q=0}[NO_TRY],AUTO}
2019-12-09 11:28:06.856 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - HttpConnectionFactory#4f25c098[HTTP/1.1] added {HttpConfiguration#613f727b{32768/8192,8192/8192,https://:0,[]},POJO}
2019-12-09 11:28:06.864 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - NetworkTrafficServerConnector#5a238421{null,[]}{0.0.0.0:0} added {Server#2173e69b{STOPPED}[9.4.20.v20190813],UNMANAGED}
2019-12-09 11:28:06.865 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - NetworkTrafficServerConnector#5a238421{null,[]}{0.0.0.0:0} added {QueuedThreadPool[qtp547179568]#209d4c30{STOPPED,8<=0<=14,i=0,r=-1,q=0}[NO_TRY],AUTO}
2019-12-09 11:28:06.865 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - NetworkTrafficServerConnector#5a238421{null,[]}{0.0.0.0:0} added {ScheduledExecutorScheduler#5839ea4{STOPPED},AUTO}
2019-12-09 11:28:06.866 [DEBUG] [Test worker] [ContainerLifeCycle] [ContainerLifeCycle.java:412] [] - NetworkTrafficServerConnector#5a238421{null,[]}{0.0.0.0:0} added {wiremock.org.eclipse.jetty.io.ArrayByteBufferPool#f73022f,POJO}
I do not want to print out DEBUG logs, just INFO level stuff. I tried to do this by changing the WireMock Configuration as follows. What I'm doing is described in the official documentation, section Notification (logging), so feel free to have a look.
.notifier(new ConsoleNotifier(false)) //attribute verbose is set to false
Also tried to solve this by adding a log4j.properties file under src/test/resources/
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.apache.http.wire=INFO
Can someone help me please?

This worked in my case (Spring, Logback) - src/test/resources/logback-test.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<!-- Decrease amount of logs from Jetty started by WireMock -->
<logger name="org.eclipse.jetty" level="info"/>
<logger name="org.eclipse.jetty.server.handler.ContextHandler" level="warn"/>
</configuration>

Related

log4j config.properties file configuration example

Hi guys just want to share how to configure your log4j.properties file
note that the # is commented out line
FATAL: shows messages at a FATAL level only
ERROR: Shows messages classified as ERROR and FATAL
WARNING: Shows messages classified as WARNING, ERROR, and FATAL
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL
DEBUG: Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL
TRACE : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL
ALL : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL
OFF : No log messages display
log4j.rootLogger= INFO, file, console
#Level/rules TRACE < DEBUG < INFO < WARN < ERROR < FATAL.
#######################################
# Direct log messages to a log file
log4j.appender.file.Threshold=ALL
log4j.appender.file.file=logs/myFol.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1} - %m%n
# set file size limit
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=20MB
log4j.appender.file.MaxBackupIndex=50
#############################################
# Direct log messages to System Out
log4j.appender.console.Threshold=INFO
log4j.appender.console.Target=System.out
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
#log4j.appender.console.layout.ConversionPattern=%-5p %L - %m%n
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %c{1}: %m%n
#log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %M:%L - %m%n

log4j2 shuts down all appenders with slf4j

I could use some help with my log4j2 configuration. When I debug the logger, it looks like it shuts down all of the appenders, and then I never receive any application logs. Luckily I know that log4j is at least finding the properties file and giving logger initialization debug output.
Here is my log4j2.properties file:
status = debug
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = info
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
appender.file.type = File
appender.file.name = application
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
Here is the logger debug init output:
DEBUG Starting LoggerContext[name=ROOT] from configuration at null
DEBUG Starting LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4...
DEBUG Shutdown hook enabled. Registering a new one.
DEBUG Apache Log4j Core 2.11.1 initializing configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4
DEBUG Installed 1 script engine
DEBUG Oracle Nashorn version: 1.8.0_162, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
DEBUG PluginManager 'Core' found 118 plugins
DEBUG PluginManager 'Level' found 0 plugins
DEBUG PluginManager 'Lookup' found 14 plugins
DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
DEBUG PluginManager 'TypeConverter' found 26 plugins
DEBUG createAppenderRef(ref="STDOUT", level="null", Filter=null)
DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={STDOUT}, ={}, Configuration(PropertiesConfig), Filter=null)
DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
DEBUG createLoggers(={root})
DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
DEBUG PatternLayout$Builder(pattern="%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n", PatternSelector=null, Configuration(PropertiesConfig), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
DEBUG PluginManager 'Converter' found 44 plugins
DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
DEBUG ConsoleAppender$Builder(target="null", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n), name="STDOUT", Configuration(PropertiesConfig), Filter=null)
DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
DEBUG createAppenders(={STDOUT})
DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
DEBUG createFilter(level="INFO", onMatch="null", onMismatch="null")
DEBUG Configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 initialized
DEBUG Starting configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4
DEBUG Started configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 OK.
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
DEBUG Appender DefaultConsole-1 stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration#605d8cc OK
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=StatusLogger
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=ContextSelector
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Loggers,name=
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Appenders,name=STDOUT
DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
DEBUG LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] started OK with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4.
DEBUG Log4jServletContextListener ensuring that Log4j starts up properly.
DEBUG Log4jServletContextListener ensuring that Log4j shuts down properly.
DEBUG Removing LoggerContext for [ROOT].
DEBUG Stopping LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b]...
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
DEBUG Appender STDOUT stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 OK
DEBUG Stopped LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] with status true
Thanks for any help! I need it.
I'd really like to get the async appenders working at some point, but its hard to find examples via the log4j2.properties syntax for log4j2
P.S. - here is my pom.xml:
<!-- log4j is used for logging within the application -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.11.1</version>
<scope>runtime</scope>
</dependency>
<!-- slf4j is used to tie Spring to log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
I just experienced what seems to be a similar problem. In my case, I was able to receive application logs, but with what appeared to be a default log4j2 config. The logs were INFO level. None of the levels set for loggers in the log4j2 config (XML) were being applied. I saw a very similar output in the startup logs as posted by the OP, including the message about "shutting down OutputStreamManager".
Mine is a spring-boot application and I was able to resolve the problem by disabling the spring-boot LoggingSystem. I added this system variable to my startup command:
-Dorg.springframework.boot.logging.LoggingSystem=none
Once I did that, the logging worked as expected, according to the levels set in my log4j2 XML config. It appears to be spring-boot hijacking the log system, rather than a conflict between SLF4J an LOG4J2.
Note that I still see the same logging startup messages -- the OutputStreamManager is still shutting down. Speculation: this is normal behavior, possibly just shutting down the bootstrap logger that prints the log4j2 initialization process.
Versions:
log4j: 2.13.3
slf4j: 1.7.30
spring-boot: 2.2.7-RElEASE

PropertyConfigurator for Log4j does not exists anymore?

I only get this on the console:
13:08:18.379 [main] ERROR log4.prueba - This is Error
13:08:18.381 [main] FATAL log4.prueba - This is Fatal
I dont get a file or all the other levels.
What I am doing wrong?
My directory looks like this:
projectFolder/src/log4/prueba.java
projectFolder/src/log4j.properties
The code:
package log4;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class prueba {
private static Logger logger = LogManager.getLogger(prueba.class);
public static void main( String[] args )
{
logger.debug("This is Debug");
logger.info("This is Info");
logger.warn("This is Warn");
logger.error("This is Error");
logger.fatal("This is Fatal");
}
}
And the properties file look like this:
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p
%c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p
%c{1}:%L - %m%n
Log4j 2 also supports configuration in properties files, but be aware:
the file needs to be called log4j2.properties
the configuration syntax is different from the Log4j 1.2 syntax (the configuration in the question uses the old Log4j 1.2 syntax, Log4j 2 will not understand this)
The Log4j 2 user manual has mostly XML examples, so many people find it easier to use the XML configuration syntax. (Again, the config file should be named log4j2.xml, Log4j 2 will ignore log4j.xml.)
define log4j property file
# prueba.java
import org.apache.log4j.Logger;
public class prueba {
private static Logger LOGGER = Logger.getLogger(prueba.class);
public static void main(String[] args) {
LOGGER.debug("This is Debug");
...
}
}
# pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
at src/resources/log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log/log4j-application.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
output to stdout and log/log4j-application.log file
2018-05-11 14:29:43 DEBUG prueba:8 - This is Debug
2018-05-11 14:29:43 INFO prueba:9 - This is Info
2018-05-11 14:29:43 WARN prueba:10 - This is Warn
2018-05-11 14:29:43 ERROR prueba:11 - This is Error
2018-05-11 14:29:43 FATAL prueba:12 - This is Fatal

How to add log4j to a web application in java

I am trying to add log4j to my web application to record the exchange between the server and the client so I added the dependency in my pom.xml:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
and I created the log4j.proporties:
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\\test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%
and the following is my code :
static final Logger logger = Logger.getLogger(hellologger.class);
public static void main(String[] args) {
String log4JPropertyFile = "C:\\Users\\xxxx\\Desktop\\log4j.properties";
Properties p = new Properties();
try {
p.load(new FileInputStream(log4JPropertyFile));
PropertyConfigurator.configure(p);
logger.info("Wow! I'm configured!");
} catch (IOException e) {
}
}
But finally I got this error :
Infos: org.osgi.framework.BundleException: Unresolved constraint in bundle com.mycompany.webclient [329]: Unable to resolve 329.7: missing requirement [329.7] osgi.wiring.package; (&(osgi.wiring.package=org.apache.log4j)(version>=1.2.0)(!(version>=2.0.0)))
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1175)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1153)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.processAllBundles(DirectoryWatcher.java:1146)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:456)
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:263)
any idea how to fix this problem ?!!!!
You can go through this tutorial on log4j .
The properties file and pom.xml files are explained properly .
logs generated are like this :
2014-07-02 20:52:39 DEBUG HelloExample:19 - This is debug : mkyong
2014-07-02 20:52:39 INFO HelloExample:23 - This is info : mkyong
2014-07-02 20:52:39 WARN HelloExample:26 - This is warn : mkyong
2014-07-02 20:52:39 ERROR HelloExample:27 - This is error : mkyong
2014-07-02 20:52:39 FATAL HelloExample:28 - This is fatal : mkyong

log4j2 LoggerContext not showing up in JMX Gui or JConsole

My Google foo is letting me down today! I am trying to configure log4j2 so that I can access the loggers via JMX and change their log levels.
However, when I hook everything up, none of the loggers show up in the JMX tree (or log4j-jmx-gui) as shown in the log4j2 JMX docs. Only the log4j2 ContextSelector and StatusLogger in the JConsole attribute tree and the StatusLogger tab in the log4j-jmx-gui (stand-alone or as a plugin) show up. No LoggerContext elements.
I've googled and not come up with anything! That probably means I'm missing something somewhere. Any help would be greatly appreciated by me and wall which I'm banging my head against :)
I'm using Log4j2 beta9 with Java 1.6.0_45-b06 on win7. I have tried it on Java 7 and it results in the same behaviour.
The log4j2 dependencies being used:
log4j-api-2.0-beta9.jar
log4j-core-2.0-beta9.jar
My log4j2.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="debug">
<Appenders>
<Console name="log-test" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="top.Log4J2TestApp" level="debug" additivity="false">
<AppenderRef ref="log-test" />
</Logger>
<Root level="trace">
<AppenderRef ref="log-test" />
</Root>
</Loggers>
</Configuration>
My test program:
package top;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4J2TestApp {
public static void main(String[] args) {
System.out.println("******** Started ZZZ ****************");
Logger log = LogManager.getLogger(Log4J2TestApp.class);
int cnt = 0;
while (true) {
log.warn("=========== Test message: {} ================", cnt++);
Thread.sleep(1000);
}
}
}
The output of the test program looks like this:
******** Started ZZZ ****************
2013-11-06 15:07:29,720 DEBUG Generated plugins in 0.000020959 seconds
2013-11-06 15:07:29,733 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n", Configuration(log4j2.xml), null, charset="null",alwaysWriteExceptions="null")
2013-11-06 15:07:29,735 DEBUG Generated plugins in 0.000013687 seconds
2013-11-06 15:07:29,737 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%d{DATE} %-5p LJX2 %c{1}: %m%n), null, target="SYSTEM_OUT", name="log-test",follow="null", ignoreExceptions="null")
2013-11-06 15:07:29,739 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream2013-11-06 15:07:29,740 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element Appenders with params(Appenders={log-test})
2013-11-06 15:07:29,742 DEBUG Generated plugins in 0.000012832 seconds
2013-11-06 15:07:29,743 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:07:29,746 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(additivity="false", level="debug", name="top.Log4J2TestApp", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:07:29,749 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:07:29,751 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(additivity="null", level="trace", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:07:29,754 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPluginfor element Loggers with params(Loggers={top.Log4J2TestApp, root})
2013-11-06 15:07:29,755 DEBUG Reconfiguration completed
06 Nov 2013 15:07:29,763 WARN LJX2 Log4J2TestApp: =========== Test message: 0 ================
....
06 Nov 2013 15:07:36,766 WARN LJX2 Log4J2TestApp: =========== Test message: 7 ================
2013-11-06 15:07:51,450 DEBUG ServletContext not present - WebLookup not added
2013-11-06 15:07:51,451 DEBUG Shutting down OutputStreamManager SYSTEM_OUT
The StatusLogger tab in the log4j-jmx-gui does have one exception but it would seem to have nothing to do with the loggers.
2013-11-06 15:41:01,403 WARN Multiple logging implementations found:
Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
Using factory: org.apache.logging.log4j.core.impl.Log4jContextFactory
2013-11-06 15:41:01,505 WARN JmDNS or serviceInfo class not found java.lang.ClassNotFoundException: javax.jmdns.JmDNS
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at org.apache.logging.log4j.core.net.MulticastDNSAdvertiser.initializeJMDNS(MulticastDNSAdvertiser.java:228)
at org.apache.logging.log4j.core.net.MulticastDNSAdvertiser.<clinit>(MulticastDNSAdvertiser.java:41)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at org.apache.logging.log4j.core.config.plugins.PluginManager.decode(PluginManager.java:241)
at org.apache.logging.log4j.core.config.plugins.PluginManager.collectPlugins(PluginManager.java:152)
at org.apache.logging.log4j.core.config.plugins.PluginManager.collectPlugins(PluginManager.java:130)
at org.apache.logging.log4j.core.pattern.PatternParser.<init>(PatternParser.java:116)
at org.apache.logging.log4j.core.pattern.PatternParser.<init>(PatternParser.java:102)
at org.apache.logging.log4j.core.layout.PatternLayout.createPatternParser(PatternLayout.java:183)
at org.apache.logging.log4j.core.layout.PatternLayout.<init>(PatternLayout.java:115)
at org.apache.logging.log4j.core.layout.PatternLayout.createLayout(PatternLayout.java:219)
at org.apache.logging.log4j.core.config.DefaultConfiguration.<init>(DefaultConfiguration.java:51)
at org.apache.logging.log4j.core.LoggerContext.<init>(LoggerContext.java:63)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:217)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:114)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:81)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:83)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:387)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:332)
at top.Log4J2TestApp.main(Log4J2TestApp.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
2013-11-06 15:41:01,612 DEBUG Generated plugins in 0.000017537 seconds
2013-11-06 15:41:01,625 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n", Configuration(log4j2.xml), null, charset="null", alwaysWriteExceptions="null")
2013-11-06 15:41:01,627 DEBUG Generated plugins in 0.000011977 seconds
2013-11-06 15:41:01,629 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%d{DATE} %-5p LJX2 %c{1}: %m%n), null, target="SYSTEM_OUT", name="log-test", follow="null", ignoreExceptions="null")
2013-11-06 15:41:01,631 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2013-11-06 15:41:01,632 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element Appenders with params(Appenders={log-test})
2013-11-06 15:41:01,633 DEBUG Generated plugins in 0.000011977 seconds
2013-11-06 15:41:01,635 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:41:01,637 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(additivity="false", level="debug", name="top.Log4J2TestApp", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:41:01,640 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:41:01,642 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(additivity="null", level="trace", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:41:01,644 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPlugin for element Loggers with params(Loggers={top.Log4J2TestApp, root})
2013-11-06 15:41:01,646 DEBUG Reconfiguration completed
Your config looks good, the only thing a bit suspicious is the error. The error does seem to occur in the LoggerContext constructor, so it may be related after all.
You may have found a bug. Would you mind reporting this issue in the log4j2 issue tracker so the log4j team can look at it?
Log4j2 uses ClassLoaderContextSelector as ContextSelector by default. In some cases (it is not clear for me, may be for simple applications like yours) it returns nothing as getLoggerContexts() method call result. No logger contexts - nothing no show, so LoggerContext category doesn't present in the tree. It can be fixed by selecting another ContextSelector implementation. I used org.apache.logging.log4j.core.selector.BasicContextSelector for this purpose. It can be set using system property named "Log4jContextSelector". See docs for more info.
It solves my problem, I hope it helps to solve yours too.

Categories

Resources