Skip to content

Commit

Permalink
Merge pull request #982 from JoeRobich/log-to-stderr
Browse files Browse the repository at this point in the history
Log warnings and errors to the standard error stream
  • Loading branch information
JoeRobich committed Feb 17, 2021
2 parents ab1d826 + e90c06e commit 849978d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
32 changes: 22 additions & 10 deletions src/Logging/SimpleConsoleLogger.cs
Expand Up @@ -15,7 +15,8 @@ internal class SimpleConsoleLogger : ILogger

private readonly IConsole _console;
private readonly ITerminal _terminal;
private readonly LogLevel _logLevel;
private readonly LogLevel _minimalLogLevel;
private readonly LogLevel _minimalErrorLevel;

private static ImmutableDictionary<LogLevel, ConsoleColor> LogLevelColorMap => new Dictionary<LogLevel, ConsoleColor>
{
Expand All @@ -28,11 +29,12 @@ internal class SimpleConsoleLogger : ILogger
[LogLevel.None] = ConsoleColor.White,
}.ToImmutableDictionary();

public SimpleConsoleLogger(IConsole console, LogLevel logLevel)
public SimpleConsoleLogger(IConsole console, LogLevel minimalLogLevel, LogLevel minimalErrorLevel)
{
_terminal = console.GetTerminal();
_console = console;
_logLevel = logLevel;
_minimalLogLevel = minimalLogLevel;
_minimalErrorLevel = minimalErrorLevel;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
Expand All @@ -45,38 +47,48 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
lock (_gate)
{
var message = formatter(state, exception);
var logToErrorStream = logLevel >= _minimalErrorLevel;
if (_terminal is null)
{
LogToConsole(message);
LogToConsole(_console, message, logToErrorStream);
}
else
{
LogToTerminal(message, logLevel);
LogToTerminal(message, logLevel, logToErrorStream);
}
}
}

public bool IsEnabled(LogLevel logLevel)
{
return (int)logLevel >= (int)_logLevel;
return (int)logLevel >= (int)_minimalLogLevel;
}

public IDisposable BeginScope<TState>(TState state)
{
return NullScope.Instance;
}

private void LogToTerminal(string message, LogLevel logLevel)
private void LogToTerminal(string message, LogLevel logLevel, bool logToErrorStream)
{
var messageColor = LogLevelColorMap[logLevel];
_terminal.ForegroundColor = messageColor;
_terminal.Out.Write($" {message}{Environment.NewLine}");

LogToConsole(_terminal, message, logToErrorStream);

_terminal.ResetColor();
}

private void LogToConsole(string message)
private void LogToConsole(IConsole console, string message, bool logToErrorStream)
{
_console.Out.Write($" {message}{Environment.NewLine}");
if (logToErrorStream)
{
console.Error.Write($" {message}{Environment.NewLine}");
}
else
{
console.Out.Write($" {message}{Environment.NewLine}");
}
}
}
}
4 changes: 2 additions & 2 deletions src/Logging/SimpleConsoleLoggerFactoryExtensions.cs
Expand Up @@ -7,9 +7,9 @@ namespace Microsoft.CodeAnalysis.Tools.Logging
{
internal static class SimpleConsoleLoggerFactoryExtensions
{
public static ILoggerFactory AddSimpleConsole(this ILoggerFactory factory, IConsole console, LogLevel logLevel)
public static ILoggerFactory AddSimpleConsole(this ILoggerFactory factory, IConsole console, LogLevel minimalLogLevel, LogLevel minimalErrorLevel)
{
factory.AddProvider(new SimpleConsoleLoggerProvider(console, logLevel));
factory.AddProvider(new SimpleConsoleLoggerProvider(console, minimalLogLevel, minimalErrorLevel));
return factory;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Logging/SimpleConsoleLoggerProvider.cs
Expand Up @@ -8,17 +8,19 @@ namespace Microsoft.CodeAnalysis.Tools.Logging
internal class SimpleConsoleLoggerProvider : ILoggerProvider
{
private readonly IConsole _console;
private readonly LogLevel _logLevel;
private readonly LogLevel _minimalLogLevel;
private readonly LogLevel _minimalErrorLevel;

public SimpleConsoleLoggerProvider(IConsole console, LogLevel logLevel)
public SimpleConsoleLoggerProvider(IConsole console, LogLevel minimalLogLevel, LogLevel minimalErrorLevel)
{
_console = console;
_logLevel = logLevel;
_minimalLogLevel = minimalLogLevel;
_minimalErrorLevel = minimalErrorLevel;
}

public ILogger CreateLogger(string name)
{
return new SimpleConsoleLogger(_console, _logLevel);
return new SimpleConsoleLogger(_console, _minimalLogLevel, _minimalErrorLevel);
}

public void Dispose()
Expand Down
6 changes: 3 additions & 3 deletions src/Program.cs
Expand Up @@ -62,7 +62,7 @@ private static async Task<int> Main(string[] args)

// Setup logging.
var logLevel = GetLogLevel(verbosity);
var logger = SetupLogging(console, logLevel);
var logger = SetupLogging(console, minimalLogLevel: logLevel, minimalErrorLevel: LogLevel.Warning);

// Hook so we can cancel and exit when ctrl+c is pressed.
var cancellationTokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -288,10 +288,10 @@ internal static DiagnosticSeverity GetSeverity(string? severity)
};
}

private static ILogger<Program> SetupLogging(IConsole console, LogLevel logLevel)
private static ILogger<Program> SetupLogging(IConsole console, LogLevel minimalLogLevel, LogLevel minimalErrorLevel)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(new LoggerFactory().AddSimpleConsole(console, logLevel));
serviceCollection.AddSingleton(new LoggerFactory().AddSimpleConsole(console, minimalLogLevel, minimalErrorLevel));
serviceCollection.AddLogging();

var serviceProvider = serviceCollection.BuildServiceProvider();
Expand Down

0 comments on commit 849978d

Please sign in to comment.