1

In my WAR I want to have my own settings using log4j.properties (located in WEB-INF/classes). Per-deployment logging as described here does not work, namely JBoss does not log anything from my application. My simple app (to reproduce the problem) contains:

WEB-INF/classes/log4j.properties
WEB-INF/classes/logging/LoggingContextListener.class
WEB-INF/lib/log4j-1.2.17.jar
WEB-INF/lib/slf4j-api-1.7.5.jar
WEB-INF/lib/slf4j-log4j12-1.7.5.jar

where LoggingContextListener just logs some random strings. log4j.properties contains:

log4j.rootLogger=WARN, 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

Anyone encountered similar issue?

Do you know if it's fixable? How?

In order to avoid confusions about the logging threshold here's the body of the LoggingContextListener

System.err.println("Trying to log something using SLF4J-Log4J");
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
logger.error("Hello");
logger.warn("anybody home?");
logger.info("can you hear me?");
logger.debug("WTF?");
System.err.println("Did you notice any logs?");

When I remove log4j.properties from the classpath I get:

ERROR [stderr] (ServerService Thread Pool -- 54) Trying to log something using SLF4J-Log4J
ERROR [logging.LoggingContextListener] (ServerService Thread Pool -- 54) Hello
WARN  [logging.LoggingContextListener] (ServerService Thread Pool -- 54) anybody home?
INFO  [logging.LoggingContextListener] (ServerService Thread Pool -- 54) can you hear me?
ERROR [stderr] (ServerService Thread Pool -- 54) Did you notice any logs?

but these logs come directly from JBoss logger configured in standalone.xml - not what I want.

jakub.g
  • 38,512
  • 12
  • 92
  • 130

2 Answers2

2

There is an ugly workaround to this issue (this is not a proper answer to my question):

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.xxx.properties"));
PropertyConfigurator.configure(props);

This code should be executed only once, so ideally in some ServletContextListener implementation of your own. Be careful - you have to rename your log4j.properties otherwise JBoss will "take care" of it and will swallow all your logs.

But this should be done by JBoss in the first place! At least it is what they write in their doc.

It's an evident bug so I'm going to report it on their JIRA

PS. Funny thing: when you have log4j in your classpath it's not taken into account anyway - even if deployment modules should be independent from the root. JBoss uses home-grown log4j implementation. Consequence: when you try to call:

PropertyConfigurator.configure(getClass().getResourceAsStream("/log4j.xxx.properties"));

having original log4j in build classpath, you will get NoSuchMethodException in runtime as their PropertyConfigurator does not implement configure(java.io.InputStream). Not cool :(

  • I have the same problem in Wildfly, found this jira issue (classified as "minor" :-( https://issues.jboss.org/browse/WFLY-2012 – geert3 Jan 20 '14 at 15:26
0

try this log4j.properties add one more property

log4j.appender.stdout.Threshold=debug

copy paste below properties and test it

log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=debug
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52