log4js remove appender prefix

123 Views Asked by At

I have a log4js configuration like the following (from the docs):

log4js.configure({
  appenders: {
    everything: { type: "file", filename: "all-the-logs.log" },
  },
  categories: {
    default: { appenders: ["everything"], level: "debug" },
  },
});
const logger = log4js.getLogger();

and when I log something, like so:

logger.debug("hello there!")

the word "default" is placed in front of all my logs, like so:

[2022-08-18T21:59:36.225] [DEBUG] default - hello there!

Is there a way to remove this? Thank you!

1

There are 1 best solutions below

0
svarog On BEST ANSWER

log4js prints the logger category inside your logs because you are using the basic layout and the category token is present there. You can get rid of it by using a pattern layout that omits the category token

For example this pattern layout definition closly mimics your current configuration

layout: {
    type: "pattern",
    pattern: "[%d] [%p] %c - %m"
}

Notice the %c token, it will print the category, so just remove it

layout: {
    type: "pattern",
    pattern: "[%d] [%p] - %m"
}

So your new appenders configuration will look something like this

appenders: {
    everything: { 
        type: "file", 
        filename: "all-the-logs.log" 
        layout: {
            type: "pattern",
            pattern: "[%d] [%p] - %m"
        }
    },
}