Reputation: 101
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
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