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

Commit

Permalink
Loggers (#442)
Browse files Browse the repository at this point in the history
* Log to file

* Initialise logging

* log file from commandline

* log file name from settings file

* fix

* Stream logger

* Write the message!

* nulls in tests

* static

* Refactor for review
  • Loading branch information
AnthonySteele authored Sep 24, 2018
1 parent c02b14d commit 71cc2a6
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 69 deletions.
67 changes: 67 additions & 0 deletions NuKeeper.Inspection/Logging/ConfigurableLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;

namespace NuKeeper.Inspection.Logging
{
public class ConfigurableLogger : INuKeeperLogger, IConfigureLogger
{
private IInternalLogger _inner;

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

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

public void Error(string message, Exception ex)
{
CheckLoggerCreated();
_inner.Error(message, ex);
}

public void Minimal(string message)
{
CheckLoggerCreated();
_inner.Log(LogLevel.Minimal, message);
}

public void Normal(string message)
{
CheckLoggerCreated();
_inner.Log(LogLevel.Normal, message);
}

public void Detailed(string message)
{
CheckLoggerCreated();
_inner.Log(LogLevel.Detailed, message);
}

private void CheckLoggerCreated()
{
if (_inner == null)
{
_inner = CreateLogger(LogLevel.Detailed, LogDestination.Console, string.Empty);
}
}

private static IInternalLogger CreateLogger(
LogLevel logLevel, LogDestination destination,
string filePath)
{
switch (destination)
{
case LogDestination.Console:
return new ConsoleLogger(logLevel);

case LogDestination.File:
return new FileLogger(filePath, logLevel);

default:
throw new Exception($"Unknown log destination {destination}");
}
}
}
}
33 changes: 9 additions & 24 deletions NuKeeper.Inspection/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace NuKeeper.Inspection.Logging
{
public class ConsoleLogger : INuKeeperLogger, IConfigureLogLevel
public class ConsoleLogger : IInternalLogger
{
private LogLevel _logLevel = LogLevel.Normal;
private readonly LogLevel _logLevel;

public void Error(string message, Exception ex = null)
public ConsoleLogger(LogLevel logLevel)
{
_logLevel = logLevel;
}

public void Error(string message, Exception ex)
{
if (ex == null)
{
Expand All @@ -22,32 +27,12 @@ public void Error(string message, Exception ex = null)
}
}

public void Minimal(string message)
{
LogWithLevel(message, LogLevel.Minimal);
}

public void Normal(string message)
{
LogWithLevel(message, LogLevel.Normal);
}

public void Detailed(string message)
{
LogWithLevel(message, LogLevel.Detailed);
}

private void LogWithLevel(string message, LogLevel level)
public void Log(LogLevel level, string message)
{
if (_logLevel >= level)
{
Console.WriteLine(message);
}
}

void IConfigureLogLevel.SetLogLevel(LogLevel logLevel)
{
_logLevel = logLevel;
}
}
}
49 changes: 49 additions & 0 deletions NuKeeper.Inspection/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;

namespace NuKeeper.Inspection.Logging
{
public class FileLogger : IInternalLogger
{
private readonly string _filePath;
private readonly LogLevel _logLevel;

public FileLogger(string filePath, LogLevel logLevel)
{
_filePath = filePath;
_logLevel = logLevel;
}

public void Error(string message, Exception ex)
{
if (ex == null)
{
WriteMessageToFile(message);
}
else
{
Log(LogLevel.Quiet, $"{message} {ex.GetType().Name} : {ex.Message}");
if (_logLevel == LogLevel.Detailed)
{
WriteMessageToFile(ex.StackTrace);
}
}
}

public void Log(LogLevel level, string message)
{
if (_logLevel >= level)
{
WriteMessageToFile(message);
}
}

private void WriteMessageToFile(string message)
{
using (var w = File.AppendText(_filePath))
{
w.WriteLine(message);
}
}
}
}
7 changes: 0 additions & 7 deletions NuKeeper.Inspection/Logging/IConfigureLogLevel.cs

This file was deleted.

8 changes: 8 additions & 0 deletions NuKeeper.Inspection/Logging/IConfigureLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NuKeeper.Inspection.Logging
{

public interface IConfigureLogger
{
void Initialise(LogLevel logLevel, string filePath);
}
}
10 changes: 10 additions & 0 deletions NuKeeper.Inspection/Logging/IInternalLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NuKeeper.Inspection.Logging
{
public interface IInternalLogger
{
void Error(string message, Exception ex);
void Log(LogLevel level, string message);
}
}
8 changes: 8 additions & 0 deletions NuKeeper.Inspection/Logging/LogDestination.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NuKeeper.Inspection.Logging
{
public enum LogDestination
{
Console,
File
}
}
6 changes: 3 additions & 3 deletions NuKeeper.Tests/Commands/GlobalCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GlobalCommandTests
public async Task ShouldCallEngineAndNotSucceedWithoutParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand All @@ -37,7 +37,7 @@ await engine
public async Task ShouldCallEngineAndSucceedWithRequiredGithubParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand Down Expand Up @@ -198,7 +198,7 @@ public async Task WillReadMaxRepoFromFile()

public async Task<SettingsContainer> CaptureSettings(FileSettings settingsIn)
{
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

SettingsContainer settingsOut = null;
Expand Down
20 changes: 10 additions & 10 deletions NuKeeper.Tests/Commands/InspectCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class InspectCommandTests
public async Task ShouldCallEngineAndSucceed()
{
var engine = Substitute.For<ILocalEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task WillReadIncludeExcludeFromFile()
public async Task LogLevelIsNormalByDefault()
{
var engine = Substitute.For<ILocalEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand All @@ -115,14 +115,14 @@ public async Task LogLevelIsNormalByDefault()

logger
.Received(1)
.SetLogLevel(LogLevel.Normal);
.Initialise(LogLevel.Normal, null);
}

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

fileSettings.Get().Returns(FileSettings.Empty());
Expand All @@ -134,14 +134,14 @@ public async Task ShouldSetLogLevelFromCommand()

logger
.Received(1)
.SetLogLevel(LogLevel.Minimal);
.Initialise(LogLevel.Minimal, null);
}

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

var settings = new FileSettings
Expand All @@ -157,14 +157,14 @@ public async Task ShouldSetLogLevelFromFile()

logger
.Received(1)
.SetLogLevel(LogLevel.Detailed);
.Initialise(LogLevel.Detailed, null);
}

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

var settings = new FileSettings
Expand All @@ -181,12 +181,12 @@ public async Task CommandLineLogLevelOverridesFile()

logger
.Received(1)
.SetLogLevel(LogLevel.Minimal);
.Initialise(LogLevel.Minimal, null);
}

public async Task<SettingsContainer> CaptureSettings(FileSettings settingsIn)
{
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

SettingsContainer settingsOut = null;
Expand Down
6 changes: 3 additions & 3 deletions NuKeeper.Tests/Commands/OrganisationCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class OrganisationCommandTests
public async Task ShouldCallEngineAndNotSucceedWithoutParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand All @@ -37,7 +37,7 @@ await engine
public async Task ShouldCallEngineAndSucceedWithRequiredGithubParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task<SettingsContainer> CaptureSettings(
bool addCommandRepoExclude = false,
int? maxRepo = null)
{
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

SettingsContainer settingsOut = null;
Expand Down
6 changes: 3 additions & 3 deletions NuKeeper.Tests/Commands/RepositoryCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RepositoryCommandTests
public async Task ShouldCallEngineAndNotSucceedWithoutParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand All @@ -37,7 +37,7 @@ await engine
public async Task ShouldCallEngineAndSucceedWithRequiredGithubParams()
{
var engine = Substitute.For<IGitHubEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand Down Expand Up @@ -199,7 +199,7 @@ public async Task<SettingsContainer> CaptureSettings(FileSettings settingsIn,
bool addLabels = false,
int? maxPr = null)
{
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

SettingsContainer settingsOut = null;
Expand Down
4 changes: 2 additions & 2 deletions NuKeeper.Tests/Commands/UpdateCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class UpdateCommandTests
public async Task ShouldCallEngineAndSucceed()
{
var engine = Substitute.For<ILocalEngine>();
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

fileSettings.Get().Returns(FileSettings.Empty());
Expand Down Expand Up @@ -119,7 +119,7 @@ public async Task WillReadVersionChangeFromFile()
public async Task<SettingsContainer> CaptureSettings(FileSettings settingsIn,
VersionChange? change = null)
{
var logger = Substitute.For<IConfigureLogLevel>();
var logger = Substitute.For<IConfigureLogger>();
var fileSettings = Substitute.For<IFileSettingsCache>();

SettingsContainer settingsOut = null;
Expand Down
Loading

0 comments on commit 71cc2a6

Please sign in to comment.