0

I am using a sifting appender where sometimes my logger name can have a string appended with a special string at the end. So for example, my logger name could either be:

  1. com.test.example OR
  2. com.test.example.#SPECIAL#

The logger will only follow one of the two above formats where its either the loggerName or the loggerName followed by "." followed by some string enclosed in #,. When using such loggers my logs look like:

  1. 2018-02-15 16:46:04.583 INFO [main] com.test.example - Application started OR
  2. 2018-02-12 16:46:04.583 INFO [main] com.test.example.#SPECIAL# - Application started

Is there a way to edit the logger name before supplying it to the pattern. I am looking to get rid of the .#SPECIAL# at the end of the logger name before I actually log the message. This is how my appender is currently setup as:

<appender name="APPLICATION-FILE" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator class="com.tibco.bw.extensions.logback.LoggerNameDiscriminator"/>
<sift>
    <appender name="FILE-${loggerName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>../log/${loggerName}.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>../log/${loggerName}%i.log</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>20</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
</sift>

I see one option writing my own Layout class that strips out the last .#..# from the logger name. Is there a way to still maintain the same pattern and also still allow a user to configure the length of the logger(in this case its set at 36).

My layout class will look like this:

public class MySampleLayout extends LayoutBase<ILoggingEvent> {
    public String doLayout(ILoggingEvent event) {
        StringBuffer sbuf = new StringBuffer(128);
        sbuf.append(event.getTimeStamp()); // how to keep the {yyyy-MM-dd HH:mm:ss.SSS} format?
        sbuf.append(" ");
        sbuf.append(event.getLevel());
        sbuf.append(" [");
        sbuf.append(event.getThreadName());
        sbuf.append("] ");
        String loggerName = event.getLoggerName();
        int poundIdx = loggerName.indexOf('#');
        loggerName = poundIdx > 0 ? loggerName.substring(0, poundIdx - 1) : loggerName;
        sbuf.append(loggerName);
        sbuf.append(" - ");
        sbuf.append(event.getFormattedMessage());
        sbuf.append(CoreConstants.LINE_SEP);
        return sbuf.toString();
    }
}

To use this my the encoder part of my appender would change to:

<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="com.example.MySampleLayout" />
</encoder>

As you can see, the layout class will strip out #SPECIAL#from the logger name. Is there a better way to do this? Also, is there a way edit the length of the logger from within the logback file?

UPDATE: I ended up using a simple pattern matcher. The only thing I am looking for now is to be able to abbreviate the logger after applying the replace function. Looking to maintain the same abbreviate length of 36 that was set earlier. This is my encoder now:

<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %replace(%logger){'\.#.*#', ''} - %msg%n</pattern>
</encoder>
1Mojojojo1
  • 163
  • 1
  • 2
  • 12
  • 1
    Take a look at Log4j MDC (Mapped Diagnostic Context). It may help you to build a cleaner answer. – LMC Feb 16 '18 at 18:27
  • I actually ended up using replace function of pattern, but I'm still trying to figure out how I can use the abbreviation function on the loggerName after doing the replace. This is my encoder now: ` %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %replace(%logger){'\.#(?<=#)(.*?#)', ''} - %msg%n ` – 1Mojojojo1 Feb 16 '18 at 19:04

0 Answers0