com.redislabs.picocliredis.OneLineLogFormat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of picocli-redis Show documentation
Show all versions of picocli-redis Show documentation
Picocli configuration for Redis
package com.redislabs.picocliredis;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class OneLineLogFormat extends Formatter {
private final DateTimeFormatter d = new DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR, 2).optionalStart().appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2).optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 3, 3, true).toFormatter();
private final ZoneId offset = ZoneOffset.systemDefault();
private final boolean verbose;
public OneLineLogFormat(boolean verbose) {
this.verbose = verbose;
}
@Override
public String format(LogRecord record) {
String message = formatMessage(record);
ZonedDateTime time = Instant.ofEpochMilli(record.getMillis()).atZone(offset);
if (record.getThrown() == null) {
if (verbose) {
return String.format("%s %s %s\t: %s%n", time.format(d), record.getLevel().getLocalizedName(),
record.getLoggerName(), message);
}
return String.format("%s\t%s%n", time.format(d), message);
}
if (verbose) {
return String.format("%s %s %s\t: %s%n%s%n", time.format(d), record.getLevel().getLocalizedName(),
record.getLoggerName(), message, stackTrace(record));
}
return String.format("%s\t%s: %s%n", time.format(d), message, ExceptionUtils.getRootCause(record.getThrown()).getMessage());
}
private String stackTrace(LogRecord record) {
StringWriter sw = new StringWriter(4096);
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
return sw.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy