com.github.libgraviton.workerbase.logging.TruncatedMessageConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of worker-base Show documentation
Show all versions of worker-base Show documentation
A base library to simplify the creation of Graviton queue workers.
package com.github.libgraviton.workerbase.logging;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import java.util.List;
/**
* Allows to truncate the message size and add a postfix to mark the message visibly as truncated.
*
* @author List of contributors {@literal }
* @see http://swisscom.ch
* @version $Id: $Id
*/
public class TruncatedMessageConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
if(getOptions().size() != 2) {
throw new IllegalArgumentException("Expected 2 arguments but got " + getOptionList().size() + ".");
}
int maxMessageLength = Integer.parseInt(getOptions().get(0));
String postfix = getOptions().get(1);
String message = event.getFormattedMessage();
if (message.length() >= maxMessageLength) {
message = message.substring(0, maxMessageLength) + " " + postfix;
}
return message;
}
/**
* For some reason getOptionList() is protected. With getOptions() it's exposed and testable.
*/
public List getOptions() {
return getOptionList();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy