java 造轮子之手写logback简单版

代码如下:

package com.example.spring;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyLog {

    public static volatile boolean logJson = true;
    public static volatile boolean logInfo = true;
    public static volatile boolean logDebug = true;
    public static volatile boolean logError = true;

    private static String time1 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    static SimpleDateFormat format = new SimpleDateFormat(time1);

    public static String longToString(long time) {
        Date date = new Date(time);
        String dates = format.format(date);
        return dates;
    }

    static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    public MyLog() {
    }
    public static String getJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            error("failed to parse json");
            e.printStackTrace();
            return null;
        }
    }
    public static void printJson(Object object) {
        if (logInfo) {
            try {
                System.out.println(JSON + getLogPrefix("INFO", 4)+ "\t" + mapper.writeValueAsString(object) + END);
            } catch (JsonProcessingException e) {
                error("fialed to parse json");
            }
        }
    }

    public static void info(Object object) {
        if (logInfo) {
            System.out.println(INFO + getLogPrefix("INFO", 4) + object + END);
        }
    }

    public static void debug(Object object) {
        if (logDebug) {
            System.out.println(DEBUG + getLogPrefix("DEBUG", 4) + object + END);
        }
    }

    public static void error(Object object) {
        if (logError) {
            System.out.println(ERROR + getLogPrefix("ERROR", 4) + " " + object + END);
        }
    }

    public static String getLogPrefix(String level, int depth) {
        return longToString(new Date().getTime()) + " " + level + " [" + Thread.currentThread().getName() + "]" + " [" + getMethodInfo(depth) + "] -";
    }
    public void logStart() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        System.out.println("------method_name:" + stackTrace[2]);
        //Tools.setMdc(Tools.getUuid());
    }

    public static String getMethodInfo(int depth) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StackTraceElement elements = stackTrace[depth];
        return elements.getFileName() + "@" + elements.getMethodName() + ":" + elements.getLineNumber();
    }

    public static void enableJson(boolean open) {
        logJson = open;
    }

    public static void logAll() {
        logInfo = true;
        logDebug = true;
    }

    public static void logInfo() {
        logInfo = true;
        logDebug = false;
    }

    public static void logDebug() {
        logInfo = false;
        logDebug = true;
    }

    public static void logNone() {
        logInfo = false;
        logDebug = false;
    }
    public static void sleep(int timeInMs) {
        try {
            Thread.sleep(timeInMs);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /*private static final String DEBUG = "\033[32m";
    private static final String INFO = "\033[36m";
    private static final String ERROR = "\033[31m";
    private static final String JSON = "\033[36m";
    private static final String END = "\033[0m";
    private static final String EXCEPTION = "";*/
    private static final String DEBUG = "";
    private static final String INFO = "";
    private static final String ERROR = "";
    private static final String JSON = "";
    private static final String END = "";
    private static final String EXCEPTION = "";
    public static void main(String[] args) {
        getMethodInfo(2);
        debug("debug test");
        info("info test");
        error("error test");
        System.out.println(JSON + "json test" + END);
        printJson(new Object());



    }
}

使用方法:

MyLog.info("hello world");

备注:

本博客内其它多篇文章都是使用的这个日志打印类。

posted @ 2019-07-14 12:16  exact  阅读(435)  评论(0编辑  收藏  举报