From 471412a0a758a5f7ee19ed728654d6c2b1010bb9 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 30 Sep 2024 11:47:10 +0100 Subject: [PATCH] Reuse ClassLoggers (#7509) --- .../Nethermind.Core.Test/NUnitLogManager.cs | 4 +++- .../Nethermind.Logging.NLog/NLogManager.cs | 17 ++++++++++++++--- .../Nethermind.Logging/ILogManager.cs | 3 ++- src/Nethermind/Nethermind.Logging/LimboLogs.cs | 3 ++- .../Nethermind.Logging/NoErrorLimboLogs.cs | 3 ++- .../Nethermind.Logging/NullLogManager.cs | 3 ++- .../Nethermind.Logging/OneLoggerLogManager.cs | 3 ++- .../SimpleConsoleLogManager.cs | 3 ++- .../Nethermind.Logging/TestErrorLogManager.cs | 3 ++- .../Nethermind.Logging/TestLogManager.cs | 3 ++- .../EngineModuleTests.V1.cs | 2 +- .../SyncReportTest.cs | 2 +- 12 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/Nethermind/Nethermind.Core.Test/NUnitLogManager.cs b/src/Nethermind/Nethermind.Core.Test/NUnitLogManager.cs index 383a85c285b..e4be9ff69ed 100644 --- a/src/Nethermind/Nethermind.Core.Test/NUnitLogManager.cs +++ b/src/Nethermind/Nethermind.Core.Test/NUnitLogManager.cs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; + using Nethermind.Logging; namespace Nethermind.Core.Test @@ -20,7 +22,7 @@ public NUnitLogManager(LogLevel level = LogLevel.Info) public ILogger GetClassLogger() => GetClassLogger(); - public ILogger GetClassLogger() => new(_logger); + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(_logger); public ILogger GetLogger(string loggerName) => GetClassLogger(); } diff --git a/src/Nethermind/Nethermind.Logging.NLog/NLogManager.cs b/src/Nethermind/Nethermind.Logging.NLog/NLogManager.cs index 9488a241fd7..9351177bdb1 100644 --- a/src/Nethermind/Nethermind.Logging.NLog/NLogManager.cs +++ b/src/Nethermind/Nethermind.Logging.NLog/NLogManager.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using NLog; using NLog.Config; @@ -58,18 +59,28 @@ private static string SetupLogDirectory(string logDirectory) } private static readonly ConcurrentDictionary s_loggers = new(); + private static readonly ConcurrentDictionary s_namedLoggers = new(); private static readonly Func s_loggerBuilder = BuildLogger; + private static readonly Func s_namedLoggerBuilder = BuildNamedLogger; + private static readonly Func s_classLoggerBuilder = BuildClassLogger; private readonly EventHandler _logManagerOnConfigurationChanged; - private static ILogger BuildLogger(Type type) => new(new NLogLogger(type)); + private static ILogger BuildLogger(Type type) + => new(new NLogLogger(type)); + private static ILogger BuildNamedLogger(string loggerName) + => new(new NLogLogger(loggerName)); + private static ILogger BuildClassLogger(string filePath) + => new(new NLogLogger()); public ILogger GetClassLogger(Type type) => s_loggers.GetOrAdd(type, s_loggerBuilder); public ILogger GetClassLogger() => GetClassLogger(typeof(T)); - public ILogger GetClassLogger() => new(new NLogLogger()); + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => !string.IsNullOrEmpty(filePath) ? + s_namedLoggers.GetOrAdd(filePath, s_classLoggerBuilder) : + new(new NLogLogger()); - public ILogger GetLogger(string loggerName) => new(new NLogLogger(loggerName)); + public ILogger GetLogger(string loggerName) => s_namedLoggers.GetOrAdd(loggerName, s_namedLoggerBuilder); public void SetGlobalVariable(string name, object value) { diff --git a/src/Nethermind/Nethermind.Logging/ILogManager.cs b/src/Nethermind/Nethermind.Logging/ILogManager.cs index b2ac2a8c1a7..c2191025e0e 100644 --- a/src/Nethermind/Nethermind.Logging/ILogManager.cs +++ b/src/Nethermind/Nethermind.Logging/ILogManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; namespace Nethermind.Logging { @@ -9,7 +10,7 @@ public interface ILogManager { ILogger GetClassLogger(Type type); ILogger GetClassLogger(); - ILogger GetClassLogger(); + ILogger GetClassLogger([CallerFilePath] string filePath = ""); ILogger GetLogger(string loggerName); void SetGlobalVariable(string name, object value) { } diff --git a/src/Nethermind/Nethermind.Logging/LimboLogs.cs b/src/Nethermind/Nethermind.Logging/LimboLogs.cs index fb821c2a757..aab78d44790 100644 --- a/src/Nethermind/Nethermind.Logging/LimboLogs.cs +++ b/src/Nethermind/Nethermind.Logging/LimboLogs.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; using System.Threading; namespace Nethermind.Logging @@ -29,7 +30,7 @@ private LimboLogs() public ILogger GetClassLogger() => LimboTraceLogger.Instance; - public ILogger GetClassLogger() => LimboTraceLogger.Instance; + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => LimboTraceLogger.Instance; public ILogger GetLogger(string loggerName) => LimboTraceLogger.Instance; } diff --git a/src/Nethermind/Nethermind.Logging/NoErrorLimboLogs.cs b/src/Nethermind/Nethermind.Logging/NoErrorLimboLogs.cs index e04030e4993..9e2dd316375 100644 --- a/src/Nethermind/Nethermind.Logging/NoErrorLimboLogs.cs +++ b/src/Nethermind/Nethermind.Logging/NoErrorLimboLogs.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; using System.Threading; namespace Nethermind.Logging @@ -23,7 +24,7 @@ private NoErrorLimboLogs() public ILogger GetClassLogger() => LimboNoErrorLogger.Instance; - public ILogger GetClassLogger() => LimboNoErrorLogger.Instance; + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => LimboNoErrorLogger.Instance; public ILogger GetLogger(string loggerName) => LimboNoErrorLogger.Instance; } diff --git a/src/Nethermind/Nethermind.Logging/NullLogManager.cs b/src/Nethermind/Nethermind.Logging/NullLogManager.cs index 6774dd98518..56b175a15e0 100644 --- a/src/Nethermind/Nethermind.Logging/NullLogManager.cs +++ b/src/Nethermind/Nethermind.Logging/NullLogManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; namespace Nethermind.Logging { @@ -17,7 +18,7 @@ private NullLogManager() public ILogger GetClassLogger() => NullLogger.Instance; - public ILogger GetClassLogger() => NullLogger.Instance; + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => NullLogger.Instance; public ILogger GetLogger(string loggerName) => NullLogger.Instance; } diff --git a/src/Nethermind/Nethermind.Logging/OneLoggerLogManager.cs b/src/Nethermind/Nethermind.Logging/OneLoggerLogManager.cs index 2ca2234a81e..64e3c214f07 100644 --- a/src/Nethermind/Nethermind.Logging/OneLoggerLogManager.cs +++ b/src/Nethermind/Nethermind.Logging/OneLoggerLogManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; namespace Nethermind.Logging { @@ -18,7 +19,7 @@ public OneLoggerLogManager(ILogger logger) public ILogger GetClassLogger() => _logger; - public ILogger GetClassLogger() => _logger; + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => _logger; public ILogger GetLogger(string loggerName) => _logger; } diff --git a/src/Nethermind/Nethermind.Logging/SimpleConsoleLogManager.cs b/src/Nethermind/Nethermind.Logging/SimpleConsoleLogManager.cs index fa8a5030927..47261ef1157 100644 --- a/src/Nethermind/Nethermind.Logging/SimpleConsoleLogManager.cs +++ b/src/Nethermind/Nethermind.Logging/SimpleConsoleLogManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; namespace Nethermind.Logging { @@ -23,7 +24,7 @@ public ILogger GetClassLogger() return new(SimpleConsoleLogger.Instance); } - public ILogger GetClassLogger() + public ILogger GetClassLogger([CallerFilePath] string filePath = "") { return new(SimpleConsoleLogger.Instance); } diff --git a/src/Nethermind/Nethermind.Logging/TestErrorLogManager.cs b/src/Nethermind/Nethermind.Logging/TestErrorLogManager.cs index 96025aca247..9afc1fa3ba6 100644 --- a/src/Nethermind/Nethermind.Logging/TestErrorLogManager.cs +++ b/src/Nethermind/Nethermind.Logging/TestErrorLogManager.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Runtime.CompilerServices; namespace Nethermind.Logging; @@ -17,7 +18,7 @@ public class TestErrorLogManager : ILogManager public ILogger GetClassLogger() => GetClassLogger(); - public ILogger GetClassLogger() => new(new TestErrorLogger(_errors)); + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(new TestErrorLogger(_errors)); public ILogger GetLogger(string loggerName) => GetClassLogger(); diff --git a/src/Nethermind/Nethermind.Logging/TestLogManager.cs b/src/Nethermind/Nethermind.Logging/TestLogManager.cs index 4ae13bc4d95..db269c52fb9 100644 --- a/src/Nethermind/Nethermind.Logging/TestLogManager.cs +++ b/src/Nethermind/Nethermind.Logging/TestLogManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Runtime.CompilerServices; namespace Nethermind.Logging { @@ -20,7 +21,7 @@ public TestLogManager(LogLevel level = LogLevel.Info) public ILogger GetClassLogger() => GetClassLogger(); - public ILogger GetClassLogger() => new(new NUnitLogger(_level)); + public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(new NUnitLogger(_level)); public ILogger GetLogger(string loggerName) => GetClassLogger(); diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V1.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V1.cs index 599eaffbf60..35e46353acd 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V1.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V1.cs @@ -1583,7 +1583,7 @@ public async Task Should_warn_for_missing_capabilities() var iLogger = Substitute.For(); iLogger.IsWarn.Returns(true); var logger = new ILogger(iLogger); - loggerManager.GetClassLogger().Returns(logger); + loggerManager.GetClassLogger(Arg.Any()).Returns(logger); chain.LogManager = loggerManager; diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs index fb160fea9ae..fc431ed8fa3 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs @@ -76,7 +76,7 @@ public void Ancient_bodies_and_receipts_are_reported_correctly( iLogger.IsInfo.Returns(true); iLogger.IsError.Returns(true); ILogger logger = new(iLogger); - logManager.GetClassLogger().Returns(logger); + logManager.GetClassLogger(Arg.Any()).Returns(logger); Queue syncModes = new(); syncModes.Enqueue(SyncMode.FastHeaders);