Skip to content

Commit

Permalink
Reuse ClassLoggers (NethermindEth#7509)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored and brbrr committed Sep 30, 2024
1 parent 93394cd commit 6041b9d
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/Nethermind/Nethermind.Core.Test/NUnitLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

using Nethermind.Logging;

namespace Nethermind.Core.Test
Expand All @@ -20,7 +22,7 @@ public NUnitLogManager(LogLevel level = LogLevel.Info)

public ILogger GetClassLogger<T>() => GetClassLogger();

public ILogger GetClassLogger() => new(_logger);
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(_logger);

public ILogger GetLogger(string loggerName) => GetClassLogger();
}
Expand Down
17 changes: 14 additions & 3 deletions src/Nethermind/Nethermind.Logging.NLog/NLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,18 +59,28 @@ private static string SetupLogDirectory(string logDirectory)
}

private static readonly ConcurrentDictionary<Type, ILogger> s_loggers = new();
private static readonly ConcurrentDictionary<string, ILogger> s_namedLoggers = new();
private static readonly Func<Type, ILogger> s_loggerBuilder = BuildLogger;
private static readonly Func<string, ILogger> s_namedLoggerBuilder = BuildNamedLogger;
private static readonly Func<string, ILogger> s_classLoggerBuilder = BuildClassLogger;
private readonly EventHandler<LoggingConfigurationChangedEventArgs> _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<T>() => 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)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/ILogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging
{
public interface ILogManager
{
ILogger GetClassLogger(Type type);
ILogger GetClassLogger<T>();
ILogger GetClassLogger();
ILogger GetClassLogger([CallerFilePath] string filePath = "");
ILogger GetLogger(string loggerName);

void SetGlobalVariable(string name, object value) { }
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/LimboLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Nethermind.Logging
Expand Down Expand Up @@ -29,7 +30,7 @@ private LimboLogs()

public ILogger GetClassLogger<T>() => LimboTraceLogger.Instance;

public ILogger GetClassLogger() => LimboTraceLogger.Instance;
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => LimboTraceLogger.Instance;

public ILogger GetLogger(string loggerName) => LimboTraceLogger.Instance;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/NoErrorLimboLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Nethermind.Logging
Expand All @@ -23,7 +24,7 @@ private NoErrorLimboLogs()

public ILogger GetClassLogger<T>() => LimboNoErrorLogger.Instance;

public ILogger GetClassLogger() => LimboNoErrorLogger.Instance;
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => LimboNoErrorLogger.Instance;

public ILogger GetLogger(string loggerName) => LimboNoErrorLogger.Instance;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/NullLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging
{
Expand All @@ -17,7 +18,7 @@ private NullLogManager()

public ILogger GetClassLogger<T>() => NullLogger.Instance;

public ILogger GetClassLogger() => NullLogger.Instance;
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => NullLogger.Instance;

public ILogger GetLogger(string loggerName) => NullLogger.Instance;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/OneLoggerLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging
{
Expand All @@ -18,7 +19,7 @@ public OneLoggerLogManager(ILogger logger)

public ILogger GetClassLogger<T>() => _logger;

public ILogger GetClassLogger() => _logger;
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => _logger;

public ILogger GetLogger(string loggerName) => _logger;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/SimpleConsoleLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging
{
Expand All @@ -23,7 +24,7 @@ public ILogger GetClassLogger<T>()
return new(SimpleConsoleLogger.Instance);
}

public ILogger GetClassLogger()
public ILogger GetClassLogger([CallerFilePath] string filePath = "")
{
return new(SimpleConsoleLogger.Instance);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/TestErrorLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging;

Expand All @@ -17,7 +18,7 @@ public class TestErrorLogManager : ILogManager

public ILogger GetClassLogger<T>() => GetClassLogger();

public ILogger GetClassLogger() => new(new TestErrorLogger(_errors));
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(new TestErrorLogger(_errors));

public ILogger GetLogger(string loggerName) => GetClassLogger();

Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Logging/TestLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;

namespace Nethermind.Logging
{
Expand All @@ -20,7 +21,7 @@ public TestLogManager(LogLevel level = LogLevel.Info)

public ILogger GetClassLogger<T>() => GetClassLogger();

public ILogger GetClassLogger() => new(new NUnitLogger(_level));
public ILogger GetClassLogger([CallerFilePath] string filePath = "") => new(new NUnitLogger(_level));

public ILogger GetLogger(string loggerName) => GetClassLogger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ public async Task Should_warn_for_missing_capabilities()
var iLogger = Substitute.For<InterfaceLogger>();
iLogger.IsWarn.Returns(true);
var logger = new ILogger(iLogger);
loggerManager.GetClassLogger().Returns(logger);
loggerManager.GetClassLogger(Arg.Any<string>()).Returns(logger);

chain.LogManager = loggerManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()).Returns(logger);

Queue<SyncMode> syncModes = new();
syncModes.Enqueue(SyncMode.FastHeaders);
Expand Down

0 comments on commit 6041b9d

Please sign in to comment.