jack
jack

Reputation: 101

Add a prefix to base class log messages from a child class

I have a base class (void)method which logs a message. Is it possible to add a prefix for that instance from a child class method?

Example:

let say base class logs result data. Can I make it to log myPrefix: result data from a child class.

I'm using slf4j

Upvotes: 0

Views: 335

Answers (1)

asdcamargo
asdcamargo

Reputation: 96

One possible solution is to use inheritance and make the parent class abstract forcing the child class to implement a method that will return myPrefix:

ParentClass

public abstract class ParentClass {

public void log(String message) {
    logger.log(getLogMessagePrefix() + message);
}

abstract String getLogMessagePrefix(); }

ChildClass

public class ChildClass extends ParentClass {

public final String LOG_PREFIX = "myPrefix";

@Override
String getLogMessagePrefix() {
    return LOG_PREFIX;
}}

Upvotes: 1

Related Questions