Skip to content

Commit

Permalink
(GH-1131) logging: better defaults for INFO
Browse files Browse the repository at this point in the history
For INFO important messages that use Green, do not
override the background color unless that color conflicts with visibility.
For regular INFO logs, use the same colors already configured in the console.
  • Loading branch information
ferventcoder committed Dec 16, 2018
1 parent f0df5c0 commit c6cc5b6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

namespace chocolatey.infrastructure.logging
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using adapters;
Expand All @@ -30,13 +32,27 @@ namespace chocolatey.infrastructure.logging
using log4net.Repository;
using log4net.Repository.Hierarchy;
using platforms;
using Console = adapters.Console;

public sealed class Log4NetAppenderConfiguration
{
private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(Log4NetAppenderConfiguration));
private static Lazy<IConsole> _console = new Lazy<IConsole>(() => new Console());
[EditorBrowsable(EditorBrowsableState.Never)]
public static void initialize_with(Lazy<IConsole> console)
{
_console = console;
}

private static IConsole Console
{
get { return _console.Value; }
}

private static bool _alreadyConfiguredFileAppender;
private static readonly string _summaryLogAppenderName = "{0}.summary.log.appender".format_with(ApplicationParameters.Name);
private const string NORMAL_LOGGING_COLORED_APPENDER = "NormalLoggingColoredConsoleAppender";
private const string IMPORTANT_LOGGING_COLORED_APPENDER = "ImportantLoggingColoredConsoleAppender";

/// <summary>
/// Pulls xml configuration from embedded location and applies it.
Expand Down Expand Up @@ -69,13 +85,76 @@ public static void configure(string outputDirectory = null, params string[] excl

_logger.DebugFormat("Configured Log4Net configuration ('{0}') from assembly {1}", resource, assembly.FullName);
}

configure_info_logging_colors();

if (!string.IsNullOrWhiteSpace(outputDirectory))
{
set_file_appender(outputDirectory, excludeLoggerNames);
}
}

private static void configure_info_logging_colors()
{
try
{
// configure INFO on same as current background color and foreground colors
var bgColor = Console.BackgroundColor;
var fgColor = Console.ForegroundColor;
ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetCallingAssembly().UnderlyingType);
foreach (var append in logRepository.GetAppenders().Where(a => a.Name.is_equal_to(NORMAL_LOGGING_COLORED_APPENDER)).or_empty_list_if_null())
{
var appender = append as ManagedColoredConsoleAppender;
if (appender != null)
{
var infoMapping = new ManagedColoredConsoleAppender.LevelColors
{
Level = Level.Info,
BackColor = bgColor,
ForeColor = fgColor,
};
appender.AddMapping(infoMapping);
appender.ActivateOptions();
}
}

foreach (var append in logRepository.GetAppenders().Where(a => a.Name.is_equal_to(IMPORTANT_LOGGING_COLORED_APPENDER)).or_empty_list_if_null())
{
var appender = append as ManagedColoredConsoleAppender;
if (appender != null)
{
// add black based on current background color
if (bgColor == ConsoleColor.White
|| bgColor == ConsoleColor.Gray
|| bgColor == ConsoleColor.Yellow
|| bgColor == ConsoleColor.DarkYellow
|| bgColor == ConsoleColor.DarkCyan
|| bgColor == ConsoleColor.DarkGray
|| bgColor == ConsoleColor.DarkGreen
|| bgColor == ConsoleColor.Green
|| bgColor == ConsoleColor.Cyan
|| bgColor == ConsoleColor.Magenta
)
{
var infoMapping = new ManagedColoredConsoleAppender.LevelColors
{
Level = Level.Info,
BackColor = ConsoleColor.Black,
ForeColor = ConsoleColor.Green,
};
appender.AddMapping(infoMapping);

appender.ActivateOptions();
}
}
}
}
catch (Exception)
{
// ignore this and move on
}
}

/// <summary>
/// Adds a file appender to all current loggers. Only runs one time.
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion src/chocolatey/infrastructure/logging/log4net.config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<mapping>
<level value="INFO" />
<foreColor value="Green" />
<backColor value="Black" />
</mapping>
<mapping>
<level value="DEBUG" />
Expand Down

0 comments on commit c6cc5b6

Please sign in to comment.