From ab77148d4b7188e11801fef0ae152180a030a89c Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 21 Jun 2023 11:38:37 +0200 Subject: [PATCH] chore: Add min log level to default logger implementation --- .../Clients/DockerContainerOperations.cs | 4 +- src/Testcontainers/Clients/TraceProgress.cs | 6 +- .../Configurations/TestcontainersSettings.cs | 2 +- src/Testcontainers/Logger.cs | 62 +++++++++++-------- src/Testcontainers/Logging.cs | 14 ++--- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/Testcontainers/Clients/DockerContainerOperations.cs b/src/Testcontainers/Clients/DockerContainerOperations.cs index a9e783844..72121bee0 100644 --- a/src/Testcontainers/Clients/DockerContainerOperations.cs +++ b/src/Testcontainers/Clients/DockerContainerOperations.cs @@ -102,13 +102,13 @@ public Task RemoveAsync(string id, CancellationToken ct = default) public Task ExtractArchiveToContainerAsync(string id, string path, Stream tarStream, CancellationToken ct = default) { - _logger.ExtractArchiveToDockerContainer(id, path); + _logger.CopyArchiveToDockerContainer(id, path); return Docker.Containers.ExtractArchiveToContainerAsync(id, new ContainerPathStatParameters { Path = path, AllowOverwriteDirWithFile = false }, tarStream, ct); } public async Task GetArchiveFromContainerAsync(string id, string path, CancellationToken ct = default) { - _logger.GetArchiveFromDockerContainer(id, path); + _logger.ReadArchiveFromDockerContainer(id, path); var tarResponse = await Docker.Containers.GetArchiveFromContainerAsync(id, new GetArchiveFromContainerParameters { Path = path }, false, ct) .ConfigureAwait(false); diff --git a/src/Testcontainers/Clients/TraceProgress.cs b/src/Testcontainers/Clients/TraceProgress.cs index 95264ae18..f03aa11c7 100644 --- a/src/Testcontainers/Clients/TraceProgress.cs +++ b/src/Testcontainers/Clients/TraceProgress.cs @@ -19,17 +19,17 @@ public void Report(JSONMessage value) if (!string.IsNullOrWhiteSpace(value.Status)) { - _logger.LogTrace(value.Status); + _logger.LogDebug(value.Status); } if (!string.IsNullOrWhiteSpace(value.Stream)) { - _logger.LogTrace(value.Stream); + _logger.LogDebug(value.Stream); } if (!string.IsNullOrWhiteSpace(value.ProgressMessage)) { - _logger.LogTrace(value.ProgressMessage); + _logger.LogDebug(value.ProgressMessage); } if (!string.IsNullOrWhiteSpace(value.ErrorMessage)) diff --git a/src/Testcontainers/Configurations/TestcontainersSettings.cs b/src/Testcontainers/Configurations/TestcontainersSettings.cs index 595807dfe..837c751e1 100644 --- a/src/Testcontainers/Configurations/TestcontainersSettings.cs +++ b/src/Testcontainers/Configurations/TestcontainersSettings.cs @@ -167,7 +167,7 @@ static TestcontainersSettings() /// [NotNull] public static ILogger Logger { get; set; } - = new Logger(); + = ConsoleLogger.Instance; /// /// Gets or sets the host operating system. diff --git a/src/Testcontainers/Logger.cs b/src/Testcontainers/Logger.cs index 0fea6e8b8..757a61e9a 100644 --- a/src/Testcontainers/Logger.cs +++ b/src/Testcontainers/Logger.cs @@ -2,9 +2,8 @@ namespace DotNet.Testcontainers { using System; using System.Diagnostics; - using System.Globalization; - using System.IO; using System.Runtime.InteropServices; + using JetBrains.Annotations; using Microsoft.Extensions.Logging; /// @@ -55,11 +54,14 @@ namespace DotNet.Testcontainers /// } /// /// - internal sealed class Logger : ILogger, IDisposable + [PublicAPI] + public sealed class ConsoleLogger : ILogger, IDisposable { private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); - public Logger() + private LogLevel _minLogLevel = LogLevel.Information; + + private ConsoleLogger() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Console.IsOutputRedirected && !Console.IsErrorRedirected) { @@ -67,42 +69,50 @@ public Logger() } } + /// + /// Gets the instance. + /// + public static ConsoleLogger Instance { get; } + = new ConsoleLogger(); + + /// + /// Gets a value indicating whether the debug log level is enabled or not. + /// + public bool DebugLogLevelEnabled + { + get + { + return LogLevel.Debug.Equals(_minLogLevel); + } + + set + { + _minLogLevel = value ? LogLevel.Debug : LogLevel.Information; + } + } + + /// public void Dispose() { - // The default logger does not support scopes. We return itself as IDisposable implementation. + // The default console logger does not support scopes. We return itself as IDisposable implementation. } + /// public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { - TextWriter console; - - switch (logLevel) + if (IsEnabled(logLevel)) { - case LogLevel.Information: - console = Console.Out; - break; - case LogLevel.Warning: - console = Console.Out; - break; - case LogLevel.Error: - console = Console.Error; - break; - case LogLevel.Critical: - console = Console.Error; - break; - default: - return; + Console.Out.WriteLine("[testcontainers.org {0:hh\\:mm\\:ss\\.ff}] {1}", _stopwatch.Elapsed, formatter.Invoke(state, exception)); } - - var message = string.Format(CultureInfo.CurrentCulture, "[testcontainers.org {0:hh\\:mm\\:ss\\.ff}] {1}", _stopwatch.Elapsed, formatter.Invoke(state, exception)); - console.WriteLine(message); } + /// public bool IsEnabled(LogLevel logLevel) { - return true; + return logLevel >= _minLogLevel; } + /// public IDisposable BeginScope(TState state) { return this; diff --git a/src/Testcontainers/Logging.cs b/src/Testcontainers/Logging.cs index 3fb502c30..eb376f3b2 100644 --- a/src/Testcontainers/Logging.cs +++ b/src/Testcontainers/Logging.cs @@ -31,10 +31,10 @@ private static readonly Action _StartReadinessCheck private static readonly Action _CompleteReadinessCheck = LoggerMessage.Define(LogLevel.Information, default, "Docker container {Id} ready"); - private static readonly Action _ExtractArchiveToDockerContainer - = LoggerMessage.Define(LogLevel.Information, default, "Copy tar archive to \"{Path}\" at Docker container {Id}"); + private static readonly Action _CopyArchiveToDockerContainer + = LoggerMessage.Define(LogLevel.Information, default, "Copy tar archive to \"{Path}\" to Docker container {Id}"); - private static readonly Action _GetArchiveFromDockerContainer + private static readonly Action _ReadArchiveFromDockerContainer = LoggerMessage.Define(LogLevel.Information, default, "Read \"{Path}\" from Docker container {Id}"); private static readonly Action _AttachToDockerContainer @@ -125,14 +125,14 @@ public static void CompleteReadinessCheck(this ILogger logger, string id) _CompleteReadinessCheck(logger, TruncResourceId(id), null); } - public static void ExtractArchiveToDockerContainer(this ILogger logger, string id, string path) + public static void CopyArchiveToDockerContainer(this ILogger logger, string id, string path) { - _ExtractArchiveToDockerContainer(logger, path, TruncResourceId(id), null); + _CopyArchiveToDockerContainer(logger, path, TruncResourceId(id), null); } - public static void GetArchiveFromDockerContainer(this ILogger logger, string id, string path) + public static void ReadArchiveFromDockerContainer(this ILogger logger, string id, string path) { - _GetArchiveFromDockerContainer(logger, path, TruncResourceId(id), null); + _ReadArchiveFromDockerContainer(logger, path, TruncResourceId(id), null); } public static void AttachToDockerContainer(this ILogger logger, string id, Type type)