Blee Blee - 2 months ago 12x
Java Question

log4j add prefix to all messages from one abstract class

I am trying to use log4j to add prefix to all log commands.

Previously I have been using @Slf4j in front of all classes, but now I need to add certain prefix to all messages, so this is no longer possible.

My class structure is as follows:

abstract class MyAbstractClass {
protected final MyLogger log;
MyAbstractClass(String foodType) {
this.log = new MyLogger(Logger.getLogger(this.getClass(), foodType));

class MyClass1 extends MyAbstractClass {
MyClass1(String foodType) { super(foodType); }
public static void myMethod1() {"hehe");

class MyClass2 extends MyAbstractClass {
MyClass2(String foodType) { super(foodType); }
public static void myMethod2() {"hohohoho");

/* */

public class MyLogger {
private final Logger LOGGER;
private String PREFIX;
public MyLogger(Logger logger, String foodType) {
LOGGER = logger;
PREFIX = foodType + ": ";

public void info(final String str) { + str);

public void warn(final String str) {
log.warn(PREFIX + str);

public void debug(final String str) { log.debug(PREFIX + str); }

public void error(final String str) {
log.error(PREFIX + str);

public void error(final String str, Exception e) {
log.error(PREFIX + str, e);

// i know.. it's so inconvenient because I have to override all the methods that I want to use from log4j Logger. I wish there is an alternative.


If I were to do something like this,

public static void main() {
MyClass1 c1 = new MyClass1("burger");
MyClass2 c2 = new MyClass2("pizza");

I would get something like this.

/* console */
[INFO] MyLogger#info - burger: hehe
[INFO] MyLogger#info - pizza: hohoho

BUT, i would like to get something like this to be able to trace where exactly it came from (original Log4j does this).

/* console */
[INFO] MyClass1#MyMethod1 - burger: hehe
[INFO] MyClass2#MyMethod2 - pizza: hohoho

Is there way for me to accomplish this?



I solved this by using the MDC Pattern. I no longer need MyLogger class. I can just use the Log4j 's Logger class.

You can do something like

abstract class MyAbstractClass {
    protected final Logger log;
    MyAbstractClass(String foodType) {
        MDC.put("myfood", foodType);
        this.log = LoggerFactory.getLogger(getClass()));

and in the pattern, you can add on %X{myfood} to access the variables you set :)

More on MDC here: