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:
com.test.example
ORcom.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:
2018-02-15 16:46:04.583 INFO [main] com.test.example - Application started
OR2018-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>