Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
(GH-118) Update to different handling of logging
Browse files Browse the repository at this point in the history
resolves #118
  • Loading branch information
AdmiringWorm committed Jun 23, 2020
1 parent 897fbaa commit 856c5d7
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 88 deletions.
3 changes: 1 addition & 2 deletions Source/Codecov.Tests/Coverage/Tool/CoverageTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using Codecov.Coverage.Tool;
Expand All @@ -15,7 +15,6 @@ public void CoverageReport_Should_Be_Empty_If_The_File_Does_Not_Exits(string fil
{
// Given
var options = Substitute.For<ICoverageOptions>();
Logger.Log.Create(false, false);
options.Files.Returns(new[] { fileData });
var coverage = new Codecov.Coverage.Tool.Coverage(options);

Expand Down
2 changes: 1 addition & 1 deletion Source/Codecov/Codecov.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<PackageReference Include="Glob" Version="1.1.6" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
</ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions Source/Codecov/Coverage/Tool/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Codecov.Exceptions;
using GlobExpressions;
using Serilog;

namespace Codecov.Coverage.Tool
{
Expand All @@ -29,7 +30,7 @@ private static bool VerifyReportFileAndExpandGlobPatterns(string path, out IEnum
expandedPath = expanded;
if (string.IsNullOrWhiteSpace(path))
{
Logger.Log.Warning("Invalid report path.");
Log.Warning("Invalid report path.");
return false;
}

Expand All @@ -51,7 +52,7 @@ private static bool VerifyReportFileAndExpandGlobPatterns(string path, out IEnum
}
else if (!File.Exists(path))
{
Logger.Log.Warning($"The file {path} does not exist.");
Log.Warning($"The file {path} does not exist.");
return false;
}

Expand Down
65 changes: 0 additions & 65 deletions Source/Codecov/Logger/Log.cs

This file was deleted.

58 changes: 58 additions & 0 deletions Source/Codecov/Logging/LogConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Diagnostics;
using Codecov.Program;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace Codecov.Logging
{
internal static class LogConfiguration
{
private const string ConsoleFullTemplate = "[{Level:u3}] " + ConsoleInfoTemplate;
private const string ConsoleInfoTemplate = "{Message:l}{NewLine}{Exception}";
private static readonly ConsoleTheme _consoleTheme = AnsiConsoleTheme.Code;
private static readonly ConsoleTheme _noColorTheme = ConsoleTheme.None;

public static void ConfigureLogging(CommandLineOptions options)
{
var config = new LoggerConfiguration()
.MinimumLevel.Verbose();

CreateDebugLogger(config);
CreateConsoleInformationLogger(config, ConsoleInfoTemplate, options);
CreateConsoleFullLogger(config, ConsoleFullTemplate, options);

Log.Logger = config.CreateLogger();
}

private static void CreateConsoleFullLogger(LoggerConfiguration config, string consoleTemplate, CommandLineOptions options)
{
var color = options.NoColor ? _noColorTheme : _consoleTheme;

config.WriteTo.Logger((config) => config
.Filter.ByExcluding((logEvent) => !options.Verbose && logEvent.Level == LogEventLevel.Verbose)
.Filter.ByExcluding((logEvent) => logEvent.Level == LogEventLevel.Information)
.WriteTo.Console(
outputTemplate: consoleTemplate,
standardErrorFromLevel: LogEventLevel.Warning,
theme: color));
}

private static void CreateConsoleInformationLogger(LoggerConfiguration config, string consoleTemplate, CommandLineOptions options)
{
var color = options.NoColor ? _noColorTheme : _consoleTheme;

config.WriteTo.Logger((config) => config
.Filter.ByIncludingOnly((logEvent) => logEvent.Level == LogEventLevel.Information)
.WriteTo.Console(
outputTemplate: consoleTemplate,
theme: color));
}

[Conditional("DEBUG")]
private static void CreateDebugLogger(LoggerConfiguration config)
{
config.WriteTo.Debug();
}
}
}
8 changes: 4 additions & 4 deletions Source/Codecov/Program/Run.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using Codecov.Logger;
using Codecov.Upload;
using Codecov.Utilities;
using CommandLine;
using Serilog;

namespace Codecov.Program
{
Expand All @@ -30,7 +30,7 @@ internal static int Runner(IEnumerable<string> args)
{
// Cleaning up undisposed fields/Properties
CodecovUploader.Cleanup();
Log.Cleanup();
Log.CloseAndFlush();
}
}

Expand All @@ -41,7 +41,7 @@ private static void Init(IEnumerable<string> args)
{
ParseAndSetCommandLineArgs(args);
ConfigureHowProgramExitsOnFail();
Log.Create(_commandLineOptions.Verbose, _commandLineOptions.NoColor);
Logging.LogConfiguration.ConfigureLogging(_commandLineOptions);
About.DisplayFiglet();
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Codecov/Program/UploadFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Codecov.Coverage.SourceCode;
using Codecov.Coverage.Tool;
using Codecov.Factories;
using Codecov.Logger;
using Codecov.Services;
using Codecov.Services.ContinuousIntegrationServers;
using Codecov.Services.VersionControlSystems;
Expand All @@ -16,6 +15,7 @@
using Codecov.Url;
using Codecov.Utilities;
using Codecov.Yaml;
using Serilog;

namespace Codecov.Program
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public void Uploader()
}
else if (ci.Equals("TeamCity"))
{
Log.Information("TeamCity detected.");
Log.Information($"{ci} detected.");
if (string.IsNullOrWhiteSpace(ContinuousIntegrationServer.Branch))
{
Log.Warning("Teamcity does not automatically make build parameters available as environment variables.\nAdd the following environment parameters to the build configuration.\nenv.TEAMCITY_BUILD_BRANCH = %teamcity.build.branch%.\nenv.TEAMCITY_BUILD_ID = %teamcity.build.id%.\nenv.TEAMCITY_BUILD_URL = %teamcity.serverUrl%/viewLog.html?buildId=%teamcity.build.id%.\nenv.TEAMCITY_BUILD_COMMIT = %system.build.vcs.number%.\nenv.TEAMCITY_BUILD_REPOSITORY = %vcsroot.<YOUR TEAMCITY VCS NAME>.url%.");
Expand Down Expand Up @@ -130,7 +130,7 @@ public void Uploader()
Log.Information($"query: {DisplayUrl}");

var response = Upload.Uploader();
Log.Verboase($"response: {response}");
Log.Verbose($"response: {response}");
var splitResponse = response.Split('\n');
if (splitResponse.Length > 1)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Codecov/Terminal/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text;
using System.Threading;
using Codecov.Exceptions;
using Codecov.Logger;
using Serilog;

namespace Codecov.Terminal
{
Expand Down Expand Up @@ -78,7 +78,7 @@ public virtual string Run(string command, string commandArguments)
}
catch (TerminalException ex)
{
Log.VerboaseException(ex);
Log.Verbose(ex, "Exception while running the terminal.");
return string.Empty;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Codecov/Upload/CodecovFallbackUploader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Net.Http;
using Codecov.Coverage.Report;
using Codecov.Logger;
using Codecov.Url;
using Serilog;

namespace Codecov.Upload
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Codecov/Upload/CodecovUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System.Reflection;
using System.Text;
using Codecov.Coverage.Report;
using Codecov.Logger;
using Codecov.Url;
using Serilog;

namespace Codecov.Upload
{
Expand Down Expand Up @@ -58,7 +58,7 @@ protected virtual HttpResponseMessage CreateResponse(HttpRequestMessage request)

protected override string Post()
{
Log.Verboase("Trying to upload using HttpClient");
Log.Verbose("Trying to upload using HttpClient");
using (var request = new HttpRequestMessage(new HttpMethod("POST"), Url.GetUrl))
{
request.Headers.TryAddWithoutValidation("X-Reduced-Redundancy", "false");
Expand Down
8 changes: 4 additions & 4 deletions Source/Codecov/Upload/Upload.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System;
using Codecov.Coverage.Report;
using Codecov.Logger;
using Codecov.Url;
using Serilog;

namespace Codecov.Upload
{
Expand All @@ -28,7 +28,7 @@ public string Uploader()
var response = Post();
if (string.IsNullOrWhiteSpace(response))
{
Log.Verboase("Failed to ping codecov.");
Log.Verbose("Failed to ping codecov.");
return string.Empty;
}

Expand All @@ -43,7 +43,7 @@ public string Uploader()
}
catch (Exception ex)
{
Log.VerboaseException(ex);
Log.Verbose(ex, "Error during uploading.");
return string.Empty;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Codecov/Upload/Uploads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Linq;
using Codecov.Coverage.Report;
using Codecov.Exceptions;
using Codecov.Logger;
using Codecov.Url;
using Serilog;

namespace Codecov.Upload
{
Expand All @@ -29,7 +29,7 @@ public string Uploader()
return response;
}

Log.Verboase("Uploader failed.");
Log.Verbose("Uploader failed.");
}

throw new UploadException("Failed to upload the report.");
Expand Down

0 comments on commit 856c5d7

Please sign in to comment.