Skip to content

Commit

Permalink
Add support for outputTemplate
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
Falco20019 committed Nov 24, 2020
1 parent bcf022b commit 793ef2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
25 changes: 1 addition & 24 deletions src/Serilog.Sinks.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ public LokiBatchFormatter(IList<LokiLabel> globalLabels)
this.LogLabelProvider = new DefaultLogLabelProvider(globalLabels);
}

// This avoids additional quoting as described in https://github.com/serilog/serilog/issues/936
private static void RenderMessage(TextWriter tw, LogEvent logEvent)
{
bool IsString(LogEventPropertyValue pv)
{
return pv is ScalarValue sv && sv.Value is string;
}

foreach(var t in logEvent.MessageTemplate.Tokens)
{
if (t is PropertyToken pt &&
logEvent.Properties.TryGetValue(pt.PropertyName, out var propVal) &&
IsString(propVal))
tw.Write(((ScalarValue)propVal).Value);
else
t.Render(logEvent.Properties, tw);
}
tw.Write('\n');
}

public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
{
if (logEvents == null)
Expand All @@ -75,11 +55,8 @@ public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, Te
var sb = new StringBuilder();
using (var tw = new StringWriter(sb))
{
RenderMessage(tw, logEvent);
formatter.Format(logEvent, tw);
}
if (logEvent.Exception != null)
// AggregateException adds a Environment.Newline to the end of ToString(), so we trim it off
sb.AppendLine(logEvent.Exception.ToString().TrimEnd());

HandleProperty("level", GetLevel(logEvent.Level), labels, sb);
foreach (KeyValuePair<string, LogEventPropertyValue> property in logEvent.Properties)
Expand Down
6 changes: 5 additions & 1 deletion src/Serilog.Sinks.Loki/LokiSinkConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using System.Collections.Generic;
using System;
using Serilog.Sinks.Http;
using Serilog.Sinks.Loki.Labels;

namespace Serilog.Sinks.Loki
{
public class LokiSinkConfiguration
{
internal const string DefaultTemplate = "{Message}{NewLine}{Exception}";

public string LokiUrl { get; set; }
public string LokiUsername { get; set; }
public string LokiPassword { get; set; }
public ILogLabelProvider LogLabelProvider { get; set; }
public IHttpClient HttpClient { get; set; }
public string OutputTemplate { get; set; } = DefaultTemplate;
public IFormatProvider FormatProvider { get; set; }
}
}
20 changes: 15 additions & 5 deletions src/Serilog.Sinks.Loki/LokiSinkExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Serilog.Configuration;
using Serilog.Formatting.Display;
using Serilog.Sinks.Http;
using Serilog.Sinks.Loki.Labels;

Expand All @@ -13,8 +14,8 @@ public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConf
public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConfiguration, string serverUrl, string username, string password)
=> sinkConfiguration.LokiHttp(new BasicAuthCredentials(serverUrl, username, password));

public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials, ILogLabelProvider labelProvider = null, LokiHttpClient httpClient = null)
=> LokiHttpImpl(sinkConfiguration, credentials, labelProvider, httpClient);
public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials, ILogLabelProvider labelProvider = null, LokiHttpClient httpClient = null, string outputTemplate = LokiSinkConfiguration.DefaultTemplate, IFormatProvider formatProvider = null)
=> LokiHttpImpl(sinkConfiguration, credentials, labelProvider, httpClient, outputTemplate, formatProvider);

public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConfiguration, Func<LokiSinkConfiguration> configFactory)
=> LokiHttpImpl(sinkConfiguration, configFactory());
Expand All @@ -25,10 +26,16 @@ private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration ser
? (LokiCredentials)new NoAuthCredentials(lokiConfig.LokiUrl)
: new BasicAuthCredentials(lokiConfig.LokiUrl, lokiConfig.LokiUsername, lokiConfig.LokiPassword);

return LokiHttpImpl(serilogConfig, credentials, lokiConfig.LogLabelProvider, lokiConfig.HttpClient);
return LokiHttpImpl(serilogConfig, credentials, lokiConfig.LogLabelProvider, lokiConfig.HttpClient, lokiConfig.OutputTemplate, lokiConfig.FormatProvider);
}

private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials, ILogLabelProvider logLabelProvider, IHttpClient httpClient)
private static LoggerConfiguration LokiHttpImpl(
this LoggerSinkConfiguration sinkConfiguration,
LokiCredentials credentials,
ILogLabelProvider logLabelProvider,
IHttpClient httpClient,
string outputTemplate,
IFormatProvider formatProvider)
{
var formatter = new LokiBatchFormatter(logLabelProvider ?? new DefaultLogLabelProvider());
var client = httpClient ?? new DefaultLokiHttpClient();
Expand All @@ -37,7 +44,10 @@ private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration sin
c.SetAuthCredentials(credentials);
}

return sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url), batchFormatter: formatter, httpClient: client);
return sinkConfiguration.Http(LokiRouteBuilder.BuildPostUri(credentials.Url),
batchFormatter: formatter,
textFormatter: new MessageTemplateTextFormatter(outputTemplate, formatProvider),
httpClient: client);
}
}
}

0 comments on commit 793ef2e

Please sign in to comment.