zoukankan      html  css  js  c++  java
  • 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)+ "	" + 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 = "33[32m";
        private static final String INFO = "33[36m";
        private static final String ERROR = "33[31m";
        private static final String JSON = "33[36m";
        private static final String END = "33[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");

    备注:

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

  • 相关阅读:
    省选模拟25
    有关树链剖分
    有关矩阵的一点讨论
    洛谷 P3390 【模板】矩阵快速幂
    HDU P2089
    有关动态规划(主要是数位DP)的一点讨论
    HDU P2222 Keywords Search
    普通平衡树Tyvj1728、luogu P3369 (treap)
    POJ P2104 K-th Number
    POJ 3311Hie with the Pie
  • 原文地址:https://www.cnblogs.com/gc65/p/11183823.html
Copyright © 2011-2022 走看看