0

I've inherited a Mavenized Java / Tomcat application and I'm having trouble getting logging to work as expected. It's probably obvious, but just so it's said - I'm a Java noob.

The issue appears to be that log4j isn't reading its own configuration file (log4j.properties). The file is available in the classpath, and is (afaik) formatted correctly.

Below is my log4j.properties file. For what it's worth I copied it in verbatim from this post on Mkyong's site, just in case my original .properties file was somehow malformed.

# Root logger option
log4j.rootLogger=DEBUG, 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

Path :

/src/main/resources/log4j.properties

Logger declaration in parent class :

public class MyParentClass extends HttpServlet{
    protected static final String CHARSET = "UTF-8";
    static Logger logger = Logger.getLogger(MyParentClass.class);

Here is the code that invokes Logger / log4j. According to the log4j FAQ, class path issues are a common source of problems in WAR apps, so I made sure the class is able to load the resource (log4j.properties) and print it to STDOUT.

public class MyChildClass extends MyParentClass {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //testing output
        System.out.println("SYSTEM.OUT.PRINTLN");
        logger.info("LOGGER.INFO");
        logger.debug("LOGGER.DEBUG");
        logger.trace("LOGGER.TRACE");
        logger.error("LOGGER.ERROR");
        logger.warn("LOGGER.WARN");

        // reading log4j.properties
        java.io.InputStream propertiesStream = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");
        java.util.Scanner s = new java.util.Scanner(propertiesStream).useDelimiter("\\A");
        String result = s.hasNext() ? s.next() : "";

        // writing log4j.properties to stdout
        System.out.println("---CONTENTS OF log4j.properties---");
        System.out.println(result);
        System.out.println("---END CONTENTS---");

Results in :

SYSTEM.OUT.PRINTLN
log4j:WARN No appenders could be found for logger (com.company.client.project.web.MyParentClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
---CONTENTS OF log4j.properties---
# Root logger option
log4j.rootLogger=DEBUG, 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
---END CONTENTS---

All the visible output comes from explicit System.out.println() calls.

Another potentially relevant data point : the log4j appenders error message ("No appenders could be found for logger") only appears because of the logger.error() call. If it's removed or commented the warning disappears.

I haven't yet set up breakpoints to step through the execution flow because the code runs on a vagrant managed Ubuntu box and I haven't configured my IDE for vagrant debugging. Working on that now, and I'll update this question if anything changes. I half expect the answer to be something trivially obvious that I just haven't caught due to inexperience.

Arne
  • 21
  • 4
  • 1
    Have you tried manually setting the log4j via the VM options? you can do that by setting `-Dlog4j.configuration=file:///path/To/Project/src/main/resources/log4j.properties`. (If this works, it could indicate that your class is no picking up the file) – Ishnark May 25 '17 at 16:16
  • Also see [here](https://stackoverflow.com/questions/5132389/if-using-maven-usually-you-put-log4j-properties-under-java-or-resources): this might actually be more useful to you. – Ishnark May 25 '17 at 16:21
  • thanks for the tip - curiously setting the VM options explicitly didn't work. I enabled Dlog4j.debug, and while the properties file is applied to a number of other applications being spun up - the one I'm trying to actually configure is skipped entirely – Arne May 25 '17 at 21:37

1 Answers1

0

Sometime log4j doesn't load automatically on tomcat start only because the server couldn't see the log4j property file. It takes to manually set the path to the log4j property file configuration as a java option or eclipse or intellij VM options. In my case I had to do like(I am on windows)

    -Dlog4j.configuration="file:D:\Users\Projets\api\WebContent\WEB-INF\config\log4j_dev.xml"
onlyme
  • 2,606
  • 2
  • 17
  • 13