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

cool.scx.logging.ScxLogger Maven / Gradle / Ivy

There is a newer version: 2.7.4
Show newest version
package cool.scx.logging;

import java.time.LocalDateTime;
import java.util.Set;

import static cool.scx.logging.ScxLoggerFactory.defaultConfig;
import static cool.scx.logging.ScxLoggerHelper.getFilteredStackTrace;
import static java.lang.System.Logger.Level;

/**
 * ScxLogger
 *
 * @author scx567888
 * @version 0.0.1
 */
public class ScxLogger {

    private final String name;

    private final ScxLoggerConfig config;

    public ScxLogger(String name) {
        this.name = name;
        //所有 ScxLogger 的 config 的 父 config 都是 ScxLoggerFactory.defaultConfig()
        this.config = new ScxLoggerConfig(defaultConfig());
    }

    /**
     * 打印日志方法
     *
     * @param level     a
     * @param msg       a
     * @param throwable a
     */
    public void log(Level level, String msg, Throwable throwable) {
        if (shouldSkip(level)) {
            return;
        }
        var now = LocalDateTime.now();
        //堆栈跟踪对象
        var contextStack = stackTrace() ? getFilteredStackTrace(new Exception()) : null;
        // 格式化 message
        var logRecord = new ScxLogRecord(now, level, this.name, msg, Thread.currentThread().getName(), throwable, contextStack);

        var recorders = recorders();

        for (var r : recorders) {
            r.record(logRecord);
        }

    }

    protected boolean shouldSkip(Level level) {
        return level == Level.OFF;
    }

    public ScxLoggerConfig config() {
        return config;
    }

    public Level level() {
        return config.level();
    }

    public boolean stackTrace() {
        return config.stackTrace();
    }

    public Set recorders() {
        return config.recorders();
    }

    public String name() {
        return name;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy