All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.enhancedmule.tools.cli.CLILogFormatter Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha4
Show newest version
/*
 * Copyright (c) Aeontronix 2022
 */

package com.aeontronix.enhancedmule.tools.cli;

import org.fusesource.jansi.Ansi;

import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class CLILogFormatter extends Formatter {
    private final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    private final Date date = new Date();
    private boolean showLevel;
    private boolean showTimestamp;
    private String separator = " ";

    @Override
    public String format(final LogRecord record) {
        final StringBuilder txt = new StringBuilder();
        if (showLevel) {
            txt.append(record.getLevel().getLocalizedName()).append(separator);
        }
        if (showTimestamp) {
            synchronized (dateFormat) {
                date.setTime(record.getMillis());
                txt.append(dateFormat.format(date)).append(separator);
            }
        }
        String errorMessage = formatMessage(record);
        if (record.getLevel().intValue() > Level.INFO.intValue()) {
            errorMessage = Ansi.ansi().fgBrightRed().a("ERROR: " + errorMessage).reset().toString();
        }
        txt.append(errorMessage).append('\n');
        return txt.toString();
    }

    public void setDebug(boolean debug) {
        showLevel = debug;
        showTimestamp = debug;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy