Skip to content

Commit

Permalink
chore: Add min log level to default logger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jun 21, 2023
1 parent c7edb93 commit ab77148
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/Testcontainers/Clients/TraceProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static TestcontainersSettings()
/// </summary>
[NotNull]
public static ILogger Logger { get; set; }
= new Logger();
= ConsoleLogger.Instance;

/// <summary>
/// Gets or sets the host operating system.
Expand Down
62 changes: 36 additions & 26 deletions src/Testcontainers/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -55,54 +54,65 @@ namespace DotNet.Testcontainers
/// }
/// </code>
/// </example>
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)
{
Console.BufferWidth = short.MaxValue - 1;
}
}

/// <summary>
/// Gets the <see cref="ConsoleLogger" /> instance.
/// </summary>
public static ConsoleLogger Instance { get; }
= new ConsoleLogger();

/// <summary>
/// Gets a value indicating whether the debug log level is enabled or not.
/// </summary>
public bool DebugLogLevelEnabled
{
get
{
return LogLevel.Debug.Equals(_minLogLevel);
}

set
{
_minLogLevel = value ? LogLevel.Debug : LogLevel.Information;
}
}

/// <inheritdoc />
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.
}

/// <inheritdoc />
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> 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);
}

/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel)
{
return true;
return logLevel >= _minLogLevel;
}

/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state)
{
return this;
Expand Down
14 changes: 7 additions & 7 deletions src/Testcontainers/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ private static readonly Action<ILogger, string, Exception> _StartReadinessCheck
private static readonly Action<ILogger, string, Exception> _CompleteReadinessCheck
= LoggerMessage.Define<string>(LogLevel.Information, default, "Docker container {Id} ready");

private static readonly Action<ILogger, string, string, Exception> _ExtractArchiveToDockerContainer
= LoggerMessage.Define<string, string>(LogLevel.Information, default, "Copy tar archive to \"{Path}\" at Docker container {Id}");
private static readonly Action<ILogger, string, string, Exception> _CopyArchiveToDockerContainer
= LoggerMessage.Define<string, string>(LogLevel.Information, default, "Copy tar archive to \"{Path}\" to Docker container {Id}");

private static readonly Action<ILogger, string, string, Exception> _GetArchiveFromDockerContainer
private static readonly Action<ILogger, string, string, Exception> _ReadArchiveFromDockerContainer
= LoggerMessage.Define<string, string>(LogLevel.Information, default, "Read \"{Path}\" from Docker container {Id}");

private static readonly Action<ILogger, Type, string, Exception> _AttachToDockerContainer
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ab77148

Please sign in to comment.