Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Added logdestination option (#481)
Browse files Browse the repository at this point in the history
* Added logdestination option
which works like "outputdestination"
Symmetry

* Remove unused
  • Loading branch information
AnthonySteele authored Oct 22, 2018
1 parent f86530d commit 20a9fec
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 30 deletions.
9 changes: 4 additions & 5 deletions NuKeeper.Inspection/Logging/ConfigurableLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ public class ConfigurableLogger : INuKeeperLogger, IConfigureLogger
{
private IInternalLogger _inner;

public void Initialise(LogLevel logLevel, string filePath)
public void Initialise(LogLevel logLevel, LogDestination destination, string filePath)
{
var destination = string.IsNullOrWhiteSpace(filePath) ?
LogDestination.Console :
LogDestination.File;

_inner = CreateLogger(logLevel, destination, filePath);
}

Expand Down Expand Up @@ -59,6 +55,9 @@ private static IInternalLogger CreateLogger(
case LogDestination.File:
return new FileLogger(filePath, logLevel);

case LogDestination.Off:
return new NullLogger();

default:
throw new Exception($"Unknown log destination {destination}");
}
Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.Inspection/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class FileLogger : IInternalLogger

public FileLogger(string filePath, LogLevel logLevel)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentException("File logger has no file path", nameof(filePath));
}

_filePath = filePath;
_logLevel = logLevel;
}
Expand Down
3 changes: 1 addition & 2 deletions NuKeeper.Inspection/Logging/IConfigureLogger.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace NuKeeper.Inspection.Logging
{

public interface IConfigureLogger
{
void Initialise(LogLevel logLevel, string filePath);
void Initialise(LogLevel logLevel, LogDestination dest, string filePath);
}
}
5 changes: 3 additions & 2 deletions NuKeeper.Inspection/Logging/LogDestination.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace NuKeeper.Inspection.Logging
namespace NuKeeper.Inspection.Logging
{
public enum LogDestination
{
Console,
File
File,
Off
}
}
15 changes: 15 additions & 0 deletions NuKeeper.Inspection/Logging/NullLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace NuKeeper.Inspection.Logging
{
public class NullLogger : IInternalLogger
{
public void Log(LogLevel level, string message)
{
}

public void LogError(string message, Exception ex)
{
}
}
}
2 changes: 1 addition & 1 deletion NuKeeper.Inspection/Report/OutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace NuKeeper.Inspection.Report
{
public enum OutputFormat
{
None,
Off,
Text,
Csv,
Metrics,
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.Inspection/Report/Reporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static IReportFormat MakeReporter(
{
switch (format)
{
case OutputFormat.None:
case OutputFormat.Off:
return new NullReportFormat();

case OutputFormat.Text:
Expand Down
50 changes: 46 additions & 4 deletions NuKeeper.Tests/Commands/InspectCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task LogLevelIsNormalByDefault()

logger
.Received(1)
.Initialise(LogLevel.Normal, null);
.Initialise(LogLevel.Normal, LogDestination.Console, Arg.Any<string>());
}

[Test]
Expand All @@ -135,7 +135,7 @@ public async Task ShouldSetLogLevelFromCommand()

logger
.Received(1)
.Initialise(LogLevel.Minimal, null);
.Initialise(LogLevel.Minimal, LogDestination.Console, Arg.Any<string>());
}

[Test]
Expand All @@ -158,7 +158,7 @@ public async Task ShouldSetLogLevelFromFile()

logger
.Received(1)
.Initialise(LogLevel.Detailed, null);
.Initialise(LogLevel.Detailed, LogDestination.Console, Arg.Any<string>());
}

[Test]
Expand All @@ -182,7 +182,49 @@ public async Task CommandLineLogLevelOverridesFile()

logger
.Received(1)
.Initialise(LogLevel.Minimal, null);
.Initialise(LogLevel.Minimal, LogDestination.Console, Arg.Any<string>());
}

[Test]
public async Task LogToFileBySettingFileName()
{
var engine = Substitute.For<ILocalEngine>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

var settings = FileSettings.Empty();

fileSettings.GetSettings().Returns(settings);

var command = new InspectCommand(engine, logger, fileSettings);
command.LogFile = "somefile.log";

await command.OnExecute();

logger
.Received(1)
.Initialise(LogLevel.Normal, LogDestination.File, "somefile.log");
}

[Test]
public async Task LogToFileBySettingLogDestination()
{
var engine = Substitute.For<ILocalEngine>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

var settings = FileSettings.Empty();

fileSettings.GetSettings().Returns(settings);

var command = new InspectCommand(engine, logger, fileSettings);
command.LogDestination = LogDestination.File;

await command.OnExecute();

logger
.Received(1)
.Initialise(LogLevel.Normal, LogDestination.File, "nukeeper.log");
}

[Test]
Expand Down
10 changes: 8 additions & 2 deletions NuKeeper.Tests/Configuration/FileSettingsReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void MissingFileReturnsNoSettings()
Assert.That(data.OutputDestination, Is.Null);
Assert.That(data.OutputFormat, Is.Null);
Assert.That(data.OutputFileName, Is.Null);
Assert.That(data.LogDestination, Is.Null);
}

[Test]
Expand All @@ -58,6 +59,7 @@ public void EmptyConfigReturnsNoSettings()
Assert.That(data.OutputDestination, Is.Null);
Assert.That(data.OutputFormat, Is.Null);
Assert.That(data.OutputFileName, Is.Null);
Assert.That(data.LogDestination, Is.Null);
}

private const string FullFileData = @"{
Expand All @@ -75,7 +77,8 @@ public void EmptyConfigReturnsNoSettings()
""Change"": ""Minor"",
""OutputFormat"": ""Text"",
""OutputDestination"": ""Console"",
""OutputFileName"" : ""out_42.txt""
""OutputFileName"" : ""out_42.txt"",
""LogDestination"" : ""file""
}";

[Test]
Expand Down Expand Up @@ -138,8 +141,11 @@ public void PopulatedConfigReturnsEnumSettings()

var data = fsr.Read(path);

Assert.That(data.Verbosity, Is.EqualTo(LogLevel.Detailed));
Assert.That(data.Change, Is.EqualTo(VersionChange.Minor));

Assert.That(data.Verbosity, Is.EqualTo(LogLevel.Detailed));
Assert.That(data.LogDestination, Is.EqualTo(LogDestination.File));

Assert.That(data.OutputDestination, Is.EqualTo(OutputDestination.Console));
Assert.That(data.OutputFormat, Is.EqualTo(OutputFormat.Text));
}
Expand Down
38 changes: 25 additions & 13 deletions NuKeeper/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@ internal abstract class CommandBase
// ReSharper disable once MemberCanBePrivate.Global
protected string[] Source { get; }

protected NuGetSources NuGetSources => Source == null? null : new NuGetSources(Source);

[Option(CommandOptionType.SingleValue, ShortName = "v", LongName = "verbosity",
Description = "Sets the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed].")]
public LogLevel? Verbosity { get; set; }

[Option(CommandOptionType.SingleValue, ShortName = "lf", LongName = "logfile",
Description = "Log to the named file")]
public string LogFile { get; set; }
protected NuGetSources NuGetSources => Source == null ? null : new NuGetSources(Source);

[Option(CommandOptionType.SingleValue, ShortName = "a", LongName = "age",
Description = "Exclude updates that do not meet a minimum age, in order to not consume packages immediately after they are released. Examples: 0 = zero, 12h = 12 hours, 3d = 3 days, 2w = two weeks. The default is 7 days.")]
Expand All @@ -52,6 +44,18 @@ internal abstract class CommandBase
Description = "Do not consider packages matching this regex pattern.")]
public string Exclude { get; set; }

[Option(CommandOptionType.SingleValue, ShortName = "v", LongName = "verbosity",
Description = "Sets the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed].")]
public LogLevel? Verbosity { get; set; }

[Option(CommandOptionType.SingleValue, ShortName = "ld", LongName = "logdestination",
Description = "Destination for logging.")]
public LogDestination? LogDestination { get; set; }

[Option(CommandOptionType.SingleValue, ShortName = "lf", LongName = "logfile",
Description = "Log to the named file.")]
public string LogFile { get; set; }

[Option(CommandOptionType.SingleValue, ShortName = "om", LongName = "outputformat",
Description = "Format for output.")]
public OutputFormat? OutputFormat { get; set; }
Expand Down Expand Up @@ -89,11 +93,19 @@ public async Task<int> OnExecute()

private void InitialiseLogging()
{
var fileSettings = FileSettingsCache.GetSettings();
var logLevel = Concat.FirstValue(Verbosity, fileSettings.Verbosity, LogLevel.Normal);
var logFile = Concat.FirstValue(LogFile, fileSettings.LogFile);
var settingsFromFile = FileSettingsCache.GetSettings();

var defaultLogDestination = string.IsNullOrWhiteSpace(LogFile)
? Inspection.Logging.LogDestination.Console
: Inspection.Logging.LogDestination.File;

var logDest = Concat.FirstValue(LogDestination, settingsFromFile.LogDestination,
defaultLogDestination);

var logLevel = Concat.FirstValue(Verbosity, settingsFromFile.Verbosity, LogLevel.Normal);
var logFile = Concat.FirstValue(LogFile, settingsFromFile.LogFile, "nukeeper.log");

_configureLogger.Initialise(logLevel, logFile);
_configureLogger.Initialise(logLevel, logDest, logFile);
}

private SettingsContainer MakeSettings()
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper/Configuration/FileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class FileSettings
public OutputDestination? OutputDestination { get; set; }
public string OutputFileName { get; set; }

public LogDestination? LogDestination { get; set; }

public static FileSettings Empty()
{
return new FileSettings();
Expand Down

0 comments on commit 20a9fec

Please sign in to comment.