I'm converting a large application from Log4j1 to Log4j2 and found this class. PatternLayout cannot be extended in Log4j2, but I understand that I convert this class to Lug4j2 using Plugins. I checked out these tutorials (Extending Log4j2, Log4j2 Plugins, Programmatic Configuration with Log4j2), but I'm still not sure how to exactly do it. It seems to me that I can only apply the tutorial, if it is the exact usecase as presented. Can anyone help me with this? How to get the specific information? Do I have to look into the Log4j2 Sourcecode?
package aa.bbb.cccc;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
public class PatternLayout extends org.apache.log4j.PatternLayout {
private static final SimpleDateFormat DATE_FORMAT;
/**
* ANSI-Format for the level
*/
private static final HashMap<Level, CliColor> LEVELS;
static {
LEVELS = new HashMap<Level, CliColor>();
LEVELS.put(Level.TRACE, CliColor.BLUE);
LEVELS.put(Level.DEBUG, CliColor.CYAN);
LEVELS.put(Level.INFO, CliColor.GREEN);
LEVELS.put(Level.WARN, CliColor.YELLOW);
LEVELS.put(Level.ERROR, CliColor.RED);
LEVELS.put(Level.FATAL, CliColor.RED);
DATE_FORMAT = new SimpleDateFormat("HH:mm:ss,SSS");
}
/**
* Pads a string until it has the desired length. The space
* is filled with spaces.
*
* @param str The string to pad.
* @param length The desired length of the string.
* @return A padded string.
*/
public static String leftPad(String str, int length) {
if (str.length() > length) {
return str.substring(0, length);
}
if (str.length() < length) {
StringBuilder stringBuilder = new StringBuilder(str);
for (int i = str.length(); i < length; ++i) {
stringBuilder.append(' ');
}
return stringBuilder.toString();
}
return str;
}
@Override
public String format(LoggingEvent event) {
return String.format("%s %s %s %s\n", DATE_FORMAT.format(new Date()),
formatLevel(event), formatLocationInfo(event),
formatMessage(event));
}
/**
* @param event The event that gets formatted
* @return A string representing the log level.
*/
private String formatLevel(LoggingEvent event) {
Level level = event.getLevel();
String levelName = leftPad(level.toString(), 5);
CliColor ansiConfig = LEVELS.get(level);
if (ansiConfig == null) {
return CliColor.color(levelName, CliColor.BOLD);
}
return CliColor.color(levelName, CliColor.BOLD, ansiConfig);
}
/**
* @param event The event that gets formatted
* @return A formatted location info.
*/
private String formatLocationInfo(LoggingEvent event) {
LocationInfo locationInfo = event.getLocationInformation();
final String line = locationInfo.getLineNumber();
String className = locationInfo.getClassName();
className = className.substring(className.lastIndexOf('.') + 1);
return "[\u001b[0;33m" + className + ":" + line + "\u001B[m]";
}
/**
* @param event The event that gets formatted
* @return Formatted message.
*/
private String formatMessage(LoggingEvent event) {
if (event.getMessage() == null) {
return "";
}
return event.getMessage().toString();
}
}