Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

featured log #848

Merged
merged 40 commits into from
Feb 27, 2023
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f80068f
featured log
Jim8y Jan 22, 2022
50af2c6
update
Jim8y Jan 22, 2022
ee766dd
update
Jim8y Jan 22, 2022
d3b6bea
rich log feature
Jim8y Jan 23, 2022
3b900a3
log on and off
Jim8y Jan 23, 2022
d5fb64e
reverse change to copyright
Jim8y Jan 23, 2022
9a4e628
revert change to namespace
Jim8y Jan 25, 2022
4890b67
reset the color set
Jim8y Jan 26, 2022
97e8757
Merge branch 'master' into feature-log
Jim8y Apr 21, 2022
4841835
Merge branch 'master' into feature-log
Jim8y May 25, 2022
c161964
remove icons, avoid newline as much as possible
Jim8y May 25, 2022
5e3f131
Merge branch 'feature-log' of github.com:Liaojinghui/neo-node into fe…
Jim8y May 25, 2022
acdd752
update format
Jim8y May 25, 2022
e8a68e3
Since Icons are removed, delete unused code.
Jim8y May 25, 2022
cf97742
Merge branch 'feature-log' of github.com:Liaojinghui/neo-node into fe…
Jim8y May 25, 2022
2b37f62
delete more unused code
Jim8y May 25, 2022
04c08ad
rename variables
Jim8y May 25, 2022
0bd2a78
`console log on` and `console log off`
Jim8y May 26, 2022
7b62f9c
Update Logger.cs
erikzhang May 26, 2022
8d7ffe3
Merge branch 'master' into feature-log
Jim8y May 26, 2022
794f37b
Update neo-cli/CLI/Logger.cs
Jim8y May 26, 2022
a5e8ddf
Merge branch 'master' into feature-log
Jim8y May 27, 2022
5e99384
Update MainService.Logger.cs
erikzhang May 28, 2022
1df7977
Merge branch 'master' into feature-log
Jim8y Jun 1, 2022
17cf262
Update neo-cli/CLI/MainService.Logger.cs
Jim8y Jun 2, 2022
a152d98
fix format
Jim8y Jun 2, 2022
c503d1c
be of the same color
Jim8y Jun 22, 2022
09d59be
Merge branch 'master' into feature-log
Jim8y Jun 23, 2022
5830055
white log
Jim8y Jun 23, 2022
8ae764e
add format explain
Jim8y Jun 23, 2022
8b52f55
Merge branch 'feature-log' of github.com:Liaojinghui/neo-node into fe…
Jim8y Jun 23, 2022
694fbe6
Format and remove unused KeyColor
shargon Jun 24, 2022
abd1341
Merge branch 'master' into feature-log
Jim8y Aug 2, 2022
10c9bb8
Merge branch 'master' into feature-log
Jim8y Aug 18, 2022
5a14323
merge write
Jim8y Sep 23, 2022
95d2d9c
Merge branch 'master' into feature-log
Jim8y Oct 28, 2022
773ef0c
Merge branch 'master' into feature-log
Jim8y Dec 8, 2022
c19087d
Merge branch 'master' into feature-log
superboyiii Feb 2, 2023
27f3bae
optimize the log code
Jim8y Feb 10, 2023
4331dc6
Merge branch 'master' into feature-log
vncoelho Feb 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 91 additions & 33 deletions neo-cli/CLI/MainService.Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
//
// The neo-cli is free software distributed under the MIT software
// license, see the accompanying file LICENSE in the main directory of
// the project or http://www.opensource.org/licenses/mit-license.php
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
// the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.ConsoleService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.IO.Path;

namespace Neo.CLI
{
partial class MainService
{
private static readonly ConsoleColorSet DebugColor = new ConsoleColorSet(ConsoleColor.Cyan);
private static readonly ConsoleColorSet InfoColor = new ConsoleColorSet(ConsoleColor.White);
private static readonly ConsoleColorSet WarningColor = new ConsoleColorSet(ConsoleColor.Yellow);
private static readonly ConsoleColorSet ErrorColor = new ConsoleColorSet(ConsoleColor.Red);
private static readonly ConsoleColorSet FatalColor = new ConsoleColorSet(ConsoleColor.Red);
private static readonly ConsoleColorSet DebugColor = new(ConsoleColor.Cyan);
private static readonly ConsoleColorSet InfoColor = new(ConsoleColor.White);
private static readonly ConsoleColorSet WarningColor = new(ConsoleColor.Yellow);
private static readonly ConsoleColorSet ErrorColor = new(ConsoleColor.Red);
private static readonly ConsoleColorSet FatalColor = new(ConsoleColor.Red);

private readonly object syncRoot = new();
private bool _showLog = Settings.Default.Logger.ConsoleOutput;

private void Initialize_Logger()
{
Expand All @@ -36,6 +39,24 @@ private void Dispose_Logger()
Utility.Logging -= OnLog;
}

/// <summary>
/// Process "console log off" command to turn off console log
/// </summary>
[ConsoleCommand("console log off", Category = "Log Commands")]
private void OnLogOffCommand()
{
_showLog = false;
}

/// <summary>
/// Process "console log on" command to turn on the console log
/// </summary>
[ConsoleCommand("console log on", Category = "Log Commands")]
private void OnLogOnCommand()
{
_showLog = true;
}

private static void GetErrorLogs(StringBuilder sb, Exception ex)
{
sb.AppendLine(ex.GetType().ToString());
Expand Down Expand Up @@ -71,43 +92,80 @@ private void OnLog(string source, LogLevel level, object message)
lock (syncRoot)
{
DateTime now = DateTime.Now;
var log = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}";

if (Settings.Default.Logger.ConsoleOutput)
var log = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}]";
if (_showLog)
{
var currentColor = new ConsoleColorSet();

var messages = message is string msg ? Parse(msg) : new[] { message.ToString() };
ConsoleColorSet logColor;
string logLevel;
switch (level)
{
case LogLevel.Debug: DebugColor.Apply(); break;
case LogLevel.Error: ErrorColor.Apply(); break;
case LogLevel.Fatal: FatalColor.Apply(); break;
case LogLevel.Info: InfoColor.Apply(); break;
case LogLevel.Warning: WarningColor.Apply(); break;
case LogLevel.Debug: logColor = DebugColor; logLevel = "DEBUG"; break;
case LogLevel.Error: logColor = ErrorColor; logLevel = "ERROR"; break;
case LogLevel.Fatal: logColor = FatalColor; logLevel = "FATAL"; break;
case LogLevel.Info: logColor = InfoColor; logLevel = "INFO"; break;
case LogLevel.Warning: logColor = WarningColor; logLevel = "WARN"; break;
default: logColor = InfoColor; logLevel = "INFO"; break;
}
logColor.Apply();
Console.Write($"{logLevel} {log} \t{messages[0],-20}");
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 1; i < messages.Length; i++)
{
if (messages[i].Length > 20)
{
messages[i] = $"{messages[i][..10]}...{messages[i][(messages[i].Length - 10)..]}";
}
Console.Write(i % 2 == 0 ? $"={messages[i]} " : $" {messages[i]}");
}

Console.WriteLine(log);
currentColor.Apply();
Console.WriteLine();
}

if (!string.IsNullOrEmpty(Settings.Default.Logger.Path))
if (string.IsNullOrEmpty(Settings.Default.Logger.Path)) return;
var sb = new StringBuilder(source);
foreach (var c in GetInvalidFileNameChars())
sb.Replace(c, '-');
var path = Combine(Settings.Default.Logger.Path, sb.ToString());
Directory.CreateDirectory(path);
path = Combine(path, $"{now:yyyy-MM-dd}.log");
try
{
StringBuilder sb = new StringBuilder(source);
foreach (char c in GetInvalidFileNameChars())
sb.Replace(c, '-');
var path = Combine(Settings.Default.Logger.Path, sb.ToString());
Directory.CreateDirectory(path);
path = Combine(path, $"{now:yyyy-MM-dd}.log");
try
{
File.AppendAllLines(path, new[] { $"[{level}]{log}" });
}
catch (IOException)
{
Console.WriteLine("Error writing the log file: " + path);
}
File.AppendAllLines(path, new[] { $"[{level}]{log} {message}" });
}
catch (IOException)
{
Console.WriteLine("Error writing the log file: " + path);
}
}
}

/// <summary>
/// Parse the log message
/// </summary>
/// <param name="message">expected format [key1 = msg1 key2 = msg2]</param>
/// <returns></returns>
private static string[] Parse(string message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment about the expected format to parse?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we never follow any standard in messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we never follow any standard in messages.

I will work on that in my next pr after this pr is merged. I have discussed with @nicolegys and decided to make the log system more test friendly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment about the expected format to parse?

Agree, we'd better add a comment. @Liaojinghui

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that it's all the same color, do we still need to parse the message?

{
var equals = message.Trim().Split('=');

if (equals.Length == 1) return new[] { message };

var messages = new List<string>();
foreach (var t in @equals)
{
var msg = t.Trim();
var parts = msg.Split(' ');
var d = parts.Take(parts.Length - 1);

if (parts.Length > 1)
{
messages.Add(string.Join(" ", d));
}
messages.Add(parts.LastOrDefault());
}

return messages.ToArray();
}
}
}