Skip to content

Commit

Permalink
Even better logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Mar 22, 2022
1 parent 357137d commit 0ebcf32
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,42 @@
// ==========================================================================

using System.Globalization;
using Microsoft.Extensions.Logging;
using Squidex.CLI.Commands.Implementation;
using Squidex.Infrastructure;
using ILog = Microsoft.Extensions.Logging.ILogger;
using Squidex.Log;

namespace Squidex.Domain.Apps.Entities.Apps.Templates
{
public sealed class StringLogger : CLI.Commands.Implementation.ILogger, ILogLine
public sealed class StringLogger : ILogger, ILogLine
{
private const int MaxActionLength = 40;
private readonly ILog log;
private readonly ISemanticLog log;
private readonly string template;
private readonly List<string> lines = new List<string>();
private readonly List<string> errors = new List<string>();
private string startedLine = string.Empty;

public StringLogger(ILog log)
public StringLogger(string template, ISemanticLog log)
{
this.template = template;

this.log = log;
}

public void Dispose()
{
var mesage = string.Join('\n', lines);

log.LogInformation("CLI executed with full logs {steps}.", mesage);
log.LogInformation(w => w
.WriteProperty("message", $"CLI executed or template {template}.")
.WriteProperty("template", template)
.WriteArray("steps", a =>
{
foreach (var line in lines)
{
a.WriteValue(line);
}
}));

if (errors.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
using Squidex.Domain.Apps.Core;
using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Infrastructure.Commands;
using Squidex.Log;

namespace Squidex.Domain.Apps.Entities.Apps.Templates
{
public sealed class TemplateCommandMiddleware : ICommandMiddleware
{
private readonly TemplatesClient templatesClient;
private readonly IUrlGenerator urlGenerator;
private readonly ILogger<TemplateCommandMiddleware> log;
private readonly ISemanticLog log;

public TemplateCommandMiddleware(TemplatesClient templatesClient, IUrlGenerator urlGenerator,
ILogger<TemplateCommandMiddleware> log)
ISemanticLog log)
{
this.templatesClient = templatesClient;
this.urlGenerator = urlGenerator;

this.log = log;
}

Expand All @@ -59,11 +61,13 @@ private async Task ApplyTemplateAsync(IAppEntity app, string? template)

if (string.IsNullOrEmpty(repository))
{
log.LogWarning("Cannot find template {template}.", template);
log.LogWarning(w => w
.WriteProperty("message", "Template not found.")
.WriteProperty("template", template));
return;
}

using (var cliLog = new StringLogger(log))
using (var cliLog = new StringLogger(template, log))
{
var session = CreateSession(app);

Expand Down

0 comments on commit 0ebcf32

Please sign in to comment.