From 887d55c6d2d766bcb39258aec102b15e8a2838f2 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sun, 1 Nov 2020 16:43:34 +0400 Subject: [PATCH] Implement basic logger --- .gitignore | 4 + LICENSE | 21 ++++++ Log4Sharp.sln | 16 ++++ README.md | 1 + src/Log4Sharp/Log.cs | 36 +++++++++ src/Log4Sharp/Log4Sharp.csproj | 11 +++ src/Log4Sharp/LogLevel.cs | 11 +++ src/Log4Sharp/Logger.cs | 129 +++++++++++++++++++++++++++++++++ 8 files changed, 229 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Log4Sharp.sln create mode 100644 README.md create mode 100644 src/Log4Sharp/Log.cs create mode 100644 src/Log4Sharp/Log4Sharp.csproj create mode 100644 src/Log4Sharp/LogLevel.cs create mode 100644 src/Log4Sharp/Logger.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4ffd39 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea + +bin +obj diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91732cf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 ChronosX88 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Log4Sharp.sln b/Log4Sharp.sln new file mode 100644 index 0000000..bfb1a73 --- /dev/null +++ b/Log4Sharp.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Log4Sharp", "src\Log4Sharp\Log4Sharp.csproj", "{130E441D-9860-4598-8EFD-22C6FCDB8D7C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {130E441D-9860-4598-8EFD-22C6FCDB8D7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {130E441D-9860-4598-8EFD-22C6FCDB8D7C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {130E441D-9860-4598-8EFD-22C6FCDB8D7C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {130E441D-9860-4598-8EFD-22C6FCDB8D7C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ + diff --git a/src/Log4Sharp/Log.cs b/src/Log4Sharp/Log.cs new file mode 100644 index 0000000..45b5cf9 --- /dev/null +++ b/src/Log4Sharp/Log.cs @@ -0,0 +1,36 @@ +using System; + +namespace Log4Sharp +{ + public class Log + { + private const LogLevel DefaultLogLevel = LogLevel.Error; + + private static readonly Logger DefaultLogger = new Logger("", DefaultLogLevel, false, true); + + public static void Error(string message) { + DefaultLogger.Error(message); + } + + public static void Warning(string message) { + DefaultLogger.Warning(message); + } + + public static void Fatal(string message) { + DefaultLogger.Fatal(message); + } + + public static void Info(string message) { + DefaultLogger.Info(message); + } + + public static void Debug(string message) { + DefaultLogger.Debug(message); + } + + public static void SetLogLevel(LogLevel lvl) + { + DefaultLogger.LogLevel = lvl; + } + } +} \ No newline at end of file diff --git a/src/Log4Sharp/Log4Sharp.csproj b/src/Log4Sharp/Log4Sharp.csproj new file mode 100644 index 0000000..bbd1982 --- /dev/null +++ b/src/Log4Sharp/Log4Sharp.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/src/Log4Sharp/LogLevel.cs b/src/Log4Sharp/LogLevel.cs new file mode 100644 index 0000000..e2ffd29 --- /dev/null +++ b/src/Log4Sharp/LogLevel.cs @@ -0,0 +1,11 @@ +namespace Log4Sharp +{ + public enum LogLevel + { + Fatal = 0, + Error = 1, + Warn = 2, + Info = 3, + Debug = 4 + } +} \ No newline at end of file diff --git a/src/Log4Sharp/Logger.cs b/src/Log4Sharp/Logger.cs new file mode 100644 index 0000000..fd8e821 --- /dev/null +++ b/src/Log4Sharp/Logger.cs @@ -0,0 +1,129 @@ +using System.Diagnostics; +using System; +using System.Drawing; +using Colorful; + +namespace Log4Sharp +{ + public class Logger + { + public LogLevel LogLevel { get; set; } + + private readonly bool _isDefaultLogger; + + internal Logger(string name, LogLevel logLevel, bool systemLog, bool isDefaultLogger) + { + this.LogLevel = logLevel; + this._isDefaultLogger = isDefaultLogger; + } + + public void Error(string message) + { + if (LogLevel > LogLevel.Fatal) + { + writeLog(LogType.Error, message); + } + } + + public void Warning(string message) + { + if (LogLevel > LogLevel.Error) + { + writeLog(LogType.Warning, message); + } + } + + public void Info(string message) + { + if (LogLevel > LogLevel.Warn) + { + writeLog(LogType.Info, message); + } + } + + public void Debug(string message) + { + if (LogLevel > LogLevel.Info) + { + writeLog(LogType.Debug, message); + } + } + + public void Fatal(string message) + { + writeLog(LogType.Fatal, message); + System.Environment.Exit(1); + } + + private void writeLog(LogType logType, string message) + { + var st = new StackTrace(); + StackFrame stFrame; + if (_isDefaultLogger) + { + stFrame = st.GetFrame(3); + } + else + { + stFrame = st.GetFrame(2); + } + + var dateTimeFormatter = new Formatter($"{DateTime.Now}", Color.White); + var frameFormatter = new Formatter($"{stFrame.GetMethod().ReflectedType.FullName}", Color.DeepPink); + + string outputMessage = "[{0}] [{1}] | [{2}]: " + message; + + Formatter tagFormatter = null; + switch (logType) + { + case LogType.Error: + { + tagFormatter = new Formatter("ERROR", Color.Red); + break; + } + case LogType.Fatal: + { + tagFormatter = new Formatter("FATAL", Color.DarkRed); + break; + } + case LogType.Info: + { + tagFormatter = new Formatter("INFO", Color.Aqua); + break; + } + case LogType.Warning: + { + tagFormatter = new Formatter("WARNING", Color.Yellow); + break; + } + case LogType.Debug: + { + tagFormatter = new Formatter("DEBUG", Color.LightGray); + break; + } + default: + { + tagFormatter = new Formatter("UNKNOWN", Color.Green); + break; + } + } + + var formatters = new Formatter[] + { + dateTimeFormatter, + frameFormatter, + tagFormatter + }; + Colorful.Console.WriteLineFormatted(outputMessage, Color.Gray, formatters); + } + + private enum LogType + { + Error, + Fatal, + Info, + Warning, + Debug + } + } +} \ No newline at end of file