user1421543
user1421543

Reputation:

How to use log4j in the way/format I want?

I want to understand how log4j works. I've read a lot of tutorials and all the answers from this site, but I still didn't get a concrete example which works.

I've tried this:

import org.apache.log4j.*;

public class Exercise {
    private static Logger logger = Logger.getLogger(SimpleLogger.class);

    public static void main(String args[]) {

        BasicConfigurator.configure();

        // ConsoleAppender myAppender = new ConsoleAppender(null, "System.out");
        // myAppender.setLayout(new SimpleLayout());
        // logger.addAppender(myAppender);

        for (int i = 0; i < 5; i++) {
            logger.info("You are here!");
        }

        logger.info("End of program");
    }
}

With this class:

import org.apache.log4j.spi.LoggingEvent;

public class SimpleLogger extends org.apache.log4j.Layout {

    @Override
    public void activateOptions() {

    }

    @Override
    public String format(LoggingEvent event) {
        return "log message = " + event.getMessage().toString() + "/n";
    }

    @Override
    public boolean ignoresThrowable() {
        return true;
    }
}

But my output is:

0 [main] INFO SimpleLogger  - You are here!
1 [main] INFO SimpleLogger  - You are here!
1 [main] INFO SimpleLogger  - You are here!
1 [main] INFO SimpleLogger  - You are here!
1 [main] INFO SimpleLogger  - You are here!
1 [main] INFO SimpleLogger  - End of program

I've tried also, with the code not commented but all I got is the output in double form or something.

Question: How can I make the output (Console or File) be in the format that I want? Do I need to modify an external file like (log4j.properties)?

Upvotes: 0

Views: 941

Answers (3)

rs_atl
rs_atl

Reputation: 8985

This line:

private static Logger logger = Logger.getLogger(SimpleLogger.class);

simply informs the default logger that the current context for logging is SimpleLogger. It does NOT specify the underlying logger implementation. In fact you do not need to implement a custom logger at all. You simply need to use a configurator that allows you to specify a format in your config file. A common technique is to use the XmlConfigurator, then your XML config file can be quite specific as to formatting under various contexts. Look here for details on XML-based configuration.

Upvotes: 1

Tomer
Tomer

Reputation: 17930

the line:

private static Logger logger = Logger.getLogger(SimpleLogger.class);

does not mean that now the logger will use your class to log, it just mean it will look in the log4j.properties file for a logger called SimpleLogger (or if it is in a package it will look for some.package.SimpleLogger ) and it will use the pattern layout defined for that logger.

If it can't find that logger it will default to the root logger and use the pattern layout of the root logger appender.

Upvotes: 0

Himanshu Yadav
Himanshu Yadav

Reputation: 13587

Having log4j configuration in a property file is a best practice.
By specifying ConversionPattern you can get the desired output format. For example

log4j.appender.rollingFile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Upvotes: 0

Related Questions