package com.njzscloud.common.core.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.CompositeConverter;

/**
 * 日志颜色转换
 */
public class ColorConverter extends CompositeConverter<ILoggingEvent> {
    @Override
    protected String transform(ILoggingEvent event, String in) {
        AnsiColor color = AnsiColor.parse(getFirstOption());
        if (color == null) {
            switch (event.getLevel().toInt()) {
                case Level.ERROR_INT:
                    color = AnsiColor.RED;
                    break;
                case Level.WARN_INT:
                    color = AnsiColor.YELLOW;
                    break;
                case Level.INFO_INT:
                    color = AnsiColor.BLUE;
                    break;
                case Level.DEBUG_INT:
                    color = AnsiColor.GREEN;
                    break;
                case Level.TRACE_INT:
                    color = AnsiColor.MAGENTA;
                    break;
                default:
                    color = AnsiColor.BLACK;
            }
        }
        return "\033[" + color + "m" + in + "\033[0m";
    }
}
