Skip to content

Commit

Permalink
✨More things! [read desc]
Browse files Browse the repository at this point in the history
- Way more customization, including changing background color for text that technically don't have a background, and letting colors for Critical-Emergency text be changed
- Fixed a major bug where background colors would wrap to the next line
  • Loading branch information
asoji committed Aug 26, 2022
1 parent feb08c9 commit a888079
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
23 changes: 13 additions & 10 deletions EasyLogPlus/Config.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyLogPlus {
public class Config {
Expand All @@ -13,12 +8,20 @@ public class Config {

public string LogPath = Environment.CurrentDirectory + $@"\Application.log";

public ConsoleColor DebugForeground = ConsoleColor.Blue;
public ConsoleColor InfoForeground = ConsoleColor.Gray;
public ConsoleColor NoticeForeground = ConsoleColor.Green;
public ConsoleColor WarningForeground = ConsoleColor.DarkYellow;
public ConsoleColor ErrorForeground = ConsoleColor.Red;
public ConsoleColor DebugText = ConsoleColor.Blue;
public ConsoleColor InfoText = System.Console.ForegroundColor;
public ConsoleColor NoticeText = ConsoleColor.Green;
public ConsoleColor WarningText = ConsoleColor.DarkYellow;
public ConsoleColor ErrorText = ConsoleColor.Red;
public ConsoleColor CriticalText = ConsoleColor.White;
public ConsoleColor AlertText = ConsoleColor.White;
public ConsoleColor EmergencyText = ConsoleColor.White;

public ConsoleColor DebugBackground = System.Console.BackgroundColor;
public ConsoleColor InfoBackground = System.Console.BackgroundColor;
public ConsoleColor NoticeBackground = System.Console.BackgroundColor;
public ConsoleColor WarningBackground = System.Console.BackgroundColor;
public ConsoleColor ErrorBackground = System.Console.BackgroundColor;
public ConsoleColor CriticalBackground = ConsoleColor.DarkRed;
public ConsoleColor AlertBackground = ConsoleColor.DarkBlue;
public ConsoleColor EmergencyBackground = ConsoleColor.DarkMagenta;
Expand Down
59 changes: 33 additions & 26 deletions EasyLogPlus/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ enum LogLevel { // trying to conform to the Syslog standard, found here
}

public string ProcessLogText(int level) {
if (isInit == false) {
return "Please Init logger first! you can init it by calling *YourLogName*.InitLogger();";
}
if (isInit == false) { return "Please Init logger first! you can init it by calling *YourLogName*.InitLogger();"; }

object text = string.Empty;

Expand Down Expand Up @@ -82,8 +80,10 @@ public string ProcessLogText(int level) {
public void Debug(object Content) {
string LogText = ProcessLogText((int)LogLevel.Debug) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = cfg.DebugForeground);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(LogText, Console.ForegroundColor = cfg.DebugText,
Console.BackgroundColor = cfg.DebugBackground);
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
Expand All @@ -92,18 +92,22 @@ public void Debug(object Content) {
public void Info(object Content) {
string LogText = ProcessLogText((int)LogLevel.Info) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = cfg.InfoForeground);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(LogText, Console.ForegroundColor = cfg.InfoText,
Console.BackgroundColor = cfg.InfoBackground);
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
}

public void Notice(object Content) {
string LogText = ProcessLogText((int)LogLevel.Notice) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = cfg.NoticeForeground);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(LogText, Console.ForegroundColor = cfg.NoticeText,
Console.BackgroundColor = cfg.NoticeBackground);
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
Expand All @@ -112,8 +116,10 @@ public void Notice(object Content) {
public void Warning(object Content) {
string LogText = ProcessLogText((int)LogLevel.Warning) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = cfg.WarningForeground);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(LogText, Console.ForegroundColor = cfg.WarningText,
Console.BackgroundColor = cfg.WarningBackground);
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
Expand All @@ -122,9 +128,10 @@ public void Warning(object Content) {
public void Error(object Content) {
string LogText = ProcessLogText((int)LogLevel.Error) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = cfg.ErrorForeground);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(LogText, Console.ForegroundColor = cfg.ErrorText,
Console.BackgroundColor = cfg.ErrorBackground);
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
Expand All @@ -133,34 +140,34 @@ public void Error(object Content) {
public void Critical(object Content) {
string LogText = ProcessLogText((int)LogLevel.Critical) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = ConsoleColor.White,
Console.Write(LogText, Console.ForegroundColor = cfg.CriticalText,
Console.BackgroundColor = cfg.CriticalBackground);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
}

public void Alert(object Content) {
string LogText = ProcessLogText((int)LogLevel.Alert) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = ConsoleColor.White,
Console.Write(LogText, Console.ForegroundColor = cfg.AlertText,
Console.BackgroundColor = cfg.AlertBackground);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
}

public void Emergency(object Content) {
string LogText = ProcessLogText((int)LogLevel.Emergency) + Content;
if (cfg.Console) {
Console.WriteLine(LogText, Console.ForegroundColor = ConsoleColor.White,
Console.Write(LogText, Console.ForegroundColor = cfg.EmergencyText,
Console.BackgroundColor = cfg.EmergencyBackground);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.ResetColor();
Console.WriteLine();
}

File.AppendAllText(cfg.LogPath, LogText + Environment.NewLine);
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,23 @@ void SetConfig()
// ENTIRELY optional, just add and change if you want.
cfg.UseColon = true; // If set to true, uses `:` in logs instead of `=>`
cfg.DebugForeground = ConsoleColor.Blue;
cfg.InfoForeground = ConsoleColor.Gray;
cfg.NoticeForeground = ConsoleColor.Green;
cfg.WarningForeground = ConsoleColor.DarkYellow;
cfg.ErrorForeground = ConsoleColor.Red;

cfg.LogPath = Environment.CurrentDirectory + $@"\Application.log"; // Sets where your Log saves and under what name
// Logger color customization
cfg.DebugText = ConsoleColor.Blue;
cfg.InfoText = System.Console.ForegroundColor;
cfg.NoticeText = ConsoleColor.Green;
cfg.WarningText = ConsoleColor.DarkYellow;
cfg.ErrorText = ConsoleColor.Red;
cfg.CriticalText = ConsoleColor.White;
cfg.AlertText = ConsoleColor.White;
cfg.EmergencyText = ConsoleColor.White;

cfg.DebugBackground = System.Console.BackgroundColor;
cfg.InfoBackground = System.Console.BackgroundColor;
cfg.NoticeBackground = System.Console.BackgroundColor;
cfg.WarningBackground = System.Console.BackgroundColor;
cfg.ErrorBackground = System.Console.BackgroundColor;
cfg.CriticalBackground = ConsoleColor.DarkRed;
cfg.AlertBackground = ConsoleColor.DarkBlue;
cfg.EmergencyBackground = ConsoleColor.DarkMagenta;
Expand Down Expand Up @@ -121,6 +132,7 @@ class Program {
cfg.UseColon = false;

cfg.CriticalBackground = ConsoleColor.Cyan;
cfg.NoticeText = ConsoleColor.Magenta;
}

static void Main(string[] args) {
Expand Down

0 comments on commit a888079

Please sign in to comment.