Log4j How do I stop my logger from printing to the console?

The line

MEMORY_APPENDER=false

will not work, you cannot set an appender to have the value false.

In you case better do something like this:

log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = ERROR, MEMORY_APPENDER
log4j.additivity.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = false

The Logger being used in one of your example is nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender and that should map to a logger in the log4j.properties or just the package part like nz.ac.massey.cs.sdc.log4jassignment.


It seems like you are mixing a lot here. Is Log4jMemoryAppender your MEMORY_APPENDER or not?

And why are you calling BasicConfigurator.configure()? Don't you want to use the log4j.properties?


Normally in a class you do this to get a logger:

package com.mycompany;

public class MyClass {
    private static final Logger log = Logger.getLogger(MyClass.class);
    ...
}

The logger name will be the fully qualified classname com.mycompany.MyClass.

Then you can have a log4j.properties like this:

log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.logger.com.mycompany=INFO, file
log4j.additivity.com.mycompany=false

logfj.appender.file = <some file appender>

OK, starting from beginning. A very simple example.

src/main/java/Log4JTest.java

package org.stackoverflow;

import org.apache.log4j.Logger;

/**
 * @author maba, 2012-08-22
 */
public class Log4JTest {

    public static final Logger log = Logger.getLogger(Log4JTest.class);

    public static void main(String[] args) {
        log.error("Error in main");
    }
}

src/main/resources/log4j.properties

log4j.rootLogger = ERROR, console

log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Now compile and make sure that log4j.properties is in your classpath together with the log4j.jar and your own classes when running.

You will see this:

0    [main] ERROR org.stackoverflow.Log4JTest  - Error in main

From here you can try to add a file appender or your own memory appender.