该打印日志的工具类,不是在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;
}
}