mirror of
https://github.com/ChronosX88/log4sharp.git
synced 2024-11-21 16:32:17 +00:00
Implement basic logger
This commit is contained in:
parent
bbcb8248e6
commit
887d55c6d2
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/.idea
|
||||
|
||||
bin
|
||||
obj
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -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.
|
16
Log4Sharp.sln
Normal file
16
Log4Sharp.sln
Normal file
@ -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
|
36
src/Log4Sharp/Log.cs
Normal file
36
src/Log4Sharp/Log.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
11
src/Log4Sharp/Log4Sharp.csproj
Normal file
11
src/Log4Sharp/Log4Sharp.csproj
Normal file
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Colorful.Console" Version="1.2.10" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
11
src/Log4Sharp/LogLevel.cs
Normal file
11
src/Log4Sharp/LogLevel.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace Log4Sharp
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
Fatal = 0,
|
||||
Error = 1,
|
||||
Warn = 2,
|
||||
Info = 3,
|
||||
Debug = 4
|
||||
}
|
||||
}
|
129
src/Log4Sharp/Logger.cs
Normal file
129
src/Log4Sharp/Logger.cs
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user