SpringBoot打印日志(二)(原生工具类)

           该打印日志的工具类,不是在logback,log4j的基础上进行封装的。你只需将这两个类复制到你的util包下即可使用,不需要其他配置。

  1.LogInfoUtils.java

package com.qtc.backend.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@RestController
public class LogInfoUtils {

    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    private static final DateTimeFormatter DAY_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static Level logOutLevel = Level.INFO;

    /**
     * @Description 初始日志设置
     */
    public static enum Level {

        DEBUG("DEBUG", 1), INFO("INFO ", 2), WARN("WARN ", 3), ERROR("ERROR", 4);

        private String tag;
        private int levelValue;

        private Level(String tag, int levelValue) {
            this.tag = tag;
            this.levelValue = levelValue;
        }

        public String getTag() {
            return this.tag;
        }

        public int getLevelValue() {
            return this.levelValue;
        }
    }

    /**
     * @param tag      类名标识
     * @param message  日志信息
     * @param fileName 文件名称(带文件扩展名)
     * @Description 打印 DEBUG 日志
     */
    public static void debug(String tag, String message, String fileName) {
        printLog(Level.DEBUG, tag, message, false, fileName);
    }

    /**
     * @param tag      类名标识
     * @param message  日志信息
     * @param fileName 文件名称(带文件扩展名)
     * @Description 打印 INFO 日志
     */
    public static void info(String tag, String message, String fileName) {
        printLog(Level.INFO, tag, message, false, fileName);
    }

    /**
     * @param tag      类名标识
     * @param message  日志信息
     * @param fileName 文件名称(带文件扩展名)
     * @Description 打印 WARN 日志
     */
    public static void warn(String tag, String message, String fileName) {
        printLog(Level.WARN, tag, message, false, fileName);
    }

    /**
     * @param tag      类名标识
     * @param message  日志信息
     * @param fileName 文件名称(带文件扩展名)
     * @Description 打印 ERROR 日志
     */
    public static void error(String tag, String message, String fileName) {
        printLog(Level.ERROR, tag, message, true, fileName);
    }

    /**
     * @param level      日志等级
     * @param tag        位置标识
     * @param message    日志信息
     * @param isOutToErr 是否是错误
     * @param fileName   日志文件
     * @Description 打印日志
     */
    private static void printLog(Level level, String tag, String message, boolean isOutToErr, String fileName) {
        if (level.getLevelValue() >= logOutLevel.getLevelValue()) {

            String log = LocalDateTime.now().format(DATE_FORMAT) + "  [" + level.getTag() + "]  " + tag + ": "
                    + message;

            if (("".equals(ConsoleIn.getConsoleTag()) || fileName.equals(ConsoleIn.getConsoleTag())) && !"stop".equals(ConsoleIn.getConsoleTag())) {
                outLogToConsole(isOutToErr, log);
            }

            outLogToFile(log, fileName);

        }
    }

    /**
     * @param isOutToErr 是否是错误
     * @param log        日志信息
     * @Description 控制台打印
     */
    private static void outLogToConsole(boolean isOutToErr, String log) {
        if (isOutToErr) {
            System.err.println(log);
        } else {
            System.out.println(log);
        }
    }

    public static String url;

    public static String getUrl() {
        return url;
    }
    @Value("${logInfo.url}")
    public void setUrl(String url) {
        this.url = url;
    }
    /**
     * @param log      日志内容
     * @param fileName 文件名(不带扩展名)
     * @Description 日志文件打印
     */
    private static void outLogToFile(String log, String fileName) {

        try {
            Path fpath = Paths.get(url + "/" + LocalDate.now().format(DAY_FORMAT) + "/" + fileName + ".log");
            System.err.println("=================日志路径=================");
            System.err.println(fpath);
            System.err.println("========================================");
            // 创建文件
            if (!Files.exists(fpath)) {
                if (!Files.exists(fpath.getParent())) {
                    fpath.getParent().toFile().mkdirs();
                }
                Files.createFile(fpath);
            }
            // 创建BufferedWriter
            BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fpath.toFile(), true)));
            bfw.write(log + "\n");
            bfw.flush();
            bfw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.ConsoleIn.java

package com.qtc.backend.utils;

import org.springframework.stereotype.Component;

import java.util.Scanner;


@Component
public class ConsoleIn {

    private static String CONSOLE_TAG = "";

    @SuppressWarnings("resource")
    public void getScanIn() {

        while (true) {
            Scanner scan = new Scanner(System.in);
            String in_txt = scan.nextLine();
            System.out.println(in_txt);
            CONSOLE_TAG = in_txt;

            if (in_txt.contains("stop") || in_txt.contains("STOP")) {
                CONSOLE_TAG = in_txt;
            }

            if (in_txt.contains("clear") || in_txt.contains("CLEAR")) {
                CONSOLE_TAG = "";
            }
        }

    }

    /**
     * @return String 控制台标识
     * @Description 获取控制台标识
     */
    public static String getConsoleTag() {

        return CONSOLE_TAG;
    }


}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值