How to implement custom pattern layout, parser, converter and add to converters in log4j2

68
July 05, 2022, at 07:00 AM

Migrating log4j from 1.x to 2.17, i am not aware how to migrate below code into log4j2 supported. Need help in migrating custom layouts, parser, converts and adding the converters.

PFB the complete details on how custom pattern layout is implemented using

public class CustomPatternParser extends PatternParser {
public CustomPatternParser(String pattern) {
    super(pattern);
}

public void finalizeConverter(char c) {
    if (c == 'D') {
        addConverter(new DayPatternConverter(formattingInfo));
        currentLiteral.setLength(0);
    } else if (c == 'P') {
        addConverter(new LogLevelPatternConverter(formattingInfo));
        currentLiteral.setLength(0);
    } else {
        super.finalizeConverter(c);
    }
}
/**
 * Extract the day from the date 'Tu', 'We', etc.
 * 
 */
private class DayPatternConverter extends PatternConverter {
    DayPatternConverter(FormattingInfo formattingInfo) {
        super(formattingInfo);
    }
    public String convert(LoggingEvent event) {
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String today = sdf.format(new Date());
        return today.substring(0, 2);
    }
}
/**
 * The PatternLayout only provides truncation facilities from the beginning
 * of the string.  To maintain the Protomatter logging capabilities,
 * we need to truncate the end of the string, and therefore needed to
 * extend to PatternLayout class.
 * 
 */
private class LogLevelPatternConverter extends PatternConverter {
    LogLevelPatternConverter(FormattingInfo formattingInfo) {
        super(formattingInfo);
    }
    
    public String convert(LoggingEvent event) {
        String eventLevel = event.getLevel().toString();
        return eventLevel.substring(0, 1);
    }
}

public class CustomPatternLayout extends PatternLayout {

public CustomPatternLayout() {
    this(DEFAULT_CONVERSION_PATTERN);
}
public CustomPatternLayout(String pattern) {
    super(pattern);
}
public PatternParser createPatternParser(String pattern) {
    return new CustomPatternParser(pattern == null ? DEFAULT_CONVERSION_PATTERN
            : pattern);
}
public class CustomPatternLayout extends PatternLayout {
public CustomPatternLayout() {
    this(DEFAULT_CONVERSION_PATTERN);
}
public CustomPatternLayout(String pattern) {
    super(pattern);
}
public PatternParser createPatternParser(String pattern) {
    return new CustomPatternParser(pattern == null ? DEFAULT_CONVERSION_PATTERN
            : pattern);
}
Rent Charter Buses Company
READ ALSO
How to transform a log4j message to fit an avro schema and post to kafka

How to transform a log4j message to fit an avro schema and post to kafka

I am working on a system that sends all logs for all microservices to a single topic apache kafkaMost services are in python but we are now forwarding logs from a Streams app

89
Unable to parse input date containing abbreviated asian components using DateTimeFormatter

Unable to parse input date containing abbreviated asian components using DateTimeFormatter

I'm trying to parse a date string to LocalDate or LocalDateTime (actually any Temporal object), which contains asian abbreviated partsWhen I provide a valid abbreviated chinese component(in example below - dayOfMonth), I get DateTimeParseException

82
Optimize multiple nested for loops? [closed]

Optimize multiple nested for loops? [closed]

Want to improve this question? Add details and clarify the problem by editing this post

112
Priority Queue - Order Messed Up

Priority Queue - Order Messed Up

I'm having a weird issue where my priority queue prints the last created item first, and then prints everything else in order

66