From a4f515755b555b2ff887b3743d43d1110a516a4b Mon Sep 17 00:00:00 2001 From: nilaoda Date: Tue, 5 Jul 2022 23:11:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E4=BD=93?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- src/N_m3u8DL-RE.Common/Log/LogLevel.cs | 17 ++++ src/N_m3u8DL-RE.Common/Log/Logger.cs | 134 +++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/N_m3u8DL-RE.Common/Log/LogLevel.cs create mode 100644 src/N_m3u8DL-RE.Common/Log/Logger.cs diff --git a/.gitignore b/.gitignore index 1ee5385..4b0547a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,7 @@ x86/ bld/ [Bb]in/ [Oo]bj/ -[Ll]og/ +#[Ll]og/ [Ll]ogs/ # Visual Studio 2015/2017 cache/options directory diff --git a/src/N_m3u8DL-RE.Common/Log/LogLevel.cs b/src/N_m3u8DL-RE.Common/Log/LogLevel.cs new file mode 100644 index 0000000..e971658 --- /dev/null +++ b/src/N_m3u8DL-RE.Common/Log/LogLevel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace N_m3u8DL_RE.Common.Log +{ + public enum LogLevel + { + OFF, + ERROR, + WARN, + INFO, + DEBUG, + } +} diff --git a/src/N_m3u8DL-RE.Common/Log/Logger.cs b/src/N_m3u8DL-RE.Common/Log/Logger.cs new file mode 100644 index 0000000..03aa7a0 --- /dev/null +++ b/src/N_m3u8DL-RE.Common/Log/Logger.cs @@ -0,0 +1,134 @@ +using Spectre.Console; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace N_m3u8DL_RE.Common.Log +{ + public class Logger + { + /// + /// 日志级别,默认为INFO + /// + public static LogLevel LogLevel { get; set; } = LogLevel.INFO; + + /// + /// 是否写出日志文件 + /// + public static bool IsWriteFile { get; set; } = true; + + private static string GetCurrTime() + { + return DateTime.Now.ToString("HH:mm:ss.fff"); + } + + private static void HandleLog(string write, string subWrite = "") + { + if (subWrite == "") + { + AnsiConsole.MarkupLine(write); + } + else + { + AnsiConsole.Markup(write); + Console.WriteLine(subWrite); + } + if (IsWriteFile) + { + var plain = write.RemoveMarkup(); + } + } + + private static string ReplaceVars(string data, params object[] ps) + { + for (int i = 0; i < ps.Length; i++) + { + data = new Regex("{}").Replace(data, $"{ps[i]}", 1); + } + return data; + } + + public static void Info(string data, params object[] ps) + { + if (LogLevel >= LogLevel.INFO) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline #548c26]INFO[/] : "; + HandleLog(write, data); + } + } + + public static void InfoMarkUp(string data, params object[] ps) + { + if (LogLevel >= LogLevel.INFO) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline #548c26]INFO[/] : " + data; + HandleLog(write); + } + } + + public static void Debug(string data, params object[] ps) + { + if (LogLevel >= LogLevel.DEBUG) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline grey]DEBUG[/] : "; + HandleLog(write, data); + } + } + + public static void DebugMarkUp(string data, params object[] ps) + { + if (LogLevel >= LogLevel.DEBUG) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline grey]DEBUG[/] : " + data; + HandleLog(write); + } + } + + public static void Warn(string data, params object[] ps) + { + if (LogLevel >= LogLevel.WARN) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline #a89022]WARN[/] : "; + HandleLog(write, data); + } + } + + public static void WarnMarkUp(string data, params object[] ps) + { + if (LogLevel >= LogLevel.WARN) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline #a89022]WARN[/] : " + data; + HandleLog(write); + } + } + + public static void Error(string data, params object[] ps) + { + if (LogLevel >= LogLevel.ERROR) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline red1]ERROR[/] : "; + HandleLog(write, data); + } + } + + public static void ErrorMarkUp(string data, params object[] ps) + { + if (LogLevel >= LogLevel.ERROR) + { + data = ReplaceVars(data, ps); + var write = GetCurrTime() + " " + "[underline red1]ERROR[/] : " + data; + HandleLog(write); + } + } + } +}