Skip to content

Commit

Permalink
Merge pull request #24 from Falco20019/Falco20019/fix-levels
Browse files Browse the repository at this point in the history
Use correct levels
  • Loading branch information
josephwoodward authored Oct 2, 2020
2 parents 407d0ec + 5d6c16f commit 88860a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Serilog.Sinks.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ public void Format(IEnumerable<string> logEvents, TextWriter output)

private static string GetLevel(LogEventLevel level)
{
if (level == LogEventLevel.Information)
return "info";

return level.ToString().ToLower();
switch (level)
{
case LogEventLevel.Verbose: return "trace";
case LogEventLevel.Information: return "info";
case LogEventLevel.Fatal: return "critical";
default: return level.ToString().ToLower();
}
}
}
}
36 changes: 36 additions & 0 deletions test/Serilog.Sinks.Loki.Tests/Labels/LogLevelLabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ public LogLevelTests()
_credentials = new BasicAuthCredentials("http://test:80", "Walter", "White");
}

[Fact]
public void VerboseLabelIsSet()
{
// Arrange
var log = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.LokiHttp(_credentials, httpClient: _client)
.CreateLogger();

// Act
log.Verbose("Verbose Level");
log.Dispose();

// Assert
var response = JsonConvert.DeserializeObject<TestResponse>(_client.Content);
response.Streams.First().Labels.ShouldBe("{level=\"trace\"}");
}

[Fact]
public void DebugLabelIsSet()
{
Expand Down Expand Up @@ -70,5 +88,23 @@ public void ErrorLabelIsSet()
var response = JsonConvert.DeserializeObject<TestResponse>(_client.Content);
response.Streams.First().Labels.ShouldBe("{level=\"error\"}");
}

[Fact]
public void FatalLabelIsSet()
{
// Arrange
var log = new LoggerConfiguration()
.MinimumLevel.Fatal()
.WriteTo.LokiHttp(_credentials, httpClient: _client)
.CreateLogger();

// Act
log.Fatal("Fatal Level");
log.Dispose();

// Assert
var response = JsonConvert.DeserializeObject<TestResponse>(_client.Content);
response.Streams.First().Labels.ShouldBe("{level=\"critical\"}");
}
}
}

0 comments on commit 88860a5

Please sign in to comment.