Skip to content

Commit

Permalink
Add time delegate to TimestampFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sschmid committed Nov 14, 2022
1 parent 0a95b09 commit 77c7a13
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [Sherlog.Formatters/2.0.0] - 2022-11-14
### Changed
- Add time delegate to TimestampFormatter

## [Sherlog.Appenders/1.0.3] - 2022-11-11
### Changed
- Use version ranges for package references
Expand Down Expand Up @@ -36,7 +40,8 @@ Sherlog has been extracted from the [DesperateDevs](https://github.com/sschmid/D
Please see previous changelog here:
- https://github.com/sschmid/DesperateDevs/blob/main/CHANGELOG.md

[Unreleased]: https://github.com/sschmid/Sherlog/compare/Sherlog.Appenders/1.0.1...HEAD
[Unreleased]: https://github.com/sschmid/Sherlog/compare/Sherlog.Formatters/2.0.0...HEAD
[Sherlog.Formatters/2.0.0]: https://github.com/sschmid/Sherlog/compare/Sherlog.Appenders/1.0.1...Sherlog.Formatters/2.0.0
[Sherlog.Appenders/1.0.1]: https://github.com/sschmid/Sherlog/compare/Sherlog/1.0.0...Sherlog.Appenders/1.0.1
[Sherlog/1.0.0]: https://github.com/sschmid/Sherlog/releases/tag/Sherlog/1.0.0
[Sherlog.Formatters/1.0.0]: https://github.com/sschmid/Sherlog/releases/tag/Sherlog.Formatters/1.0.0
Expand Down
1 change: 1 addition & 0 deletions samples/SherlogSamples/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
global using System.Globalization;
global using System.Net;
global using Sherlog;
global using Sherlog.Appenders;
Expand Down
4 changes: 2 additions & 2 deletions samples/SherlogSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void FormatterSample()
{
// Add more detailed message formatting
var messageFormatter = new LogMessageFormatter();
var timestampFormatter = new TimestampFormatter();
var timestampFormatter = new TimestampFormatter(() => DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
var consoleAppender = new ConsoleAppender(new Dictionary<LogLevel, ConsoleColor>
{
{LogLevel.Trace, ConsoleColor.Cyan},
Expand Down Expand Up @@ -125,7 +125,7 @@ void SendMessageViaTcpConnectionSample()
Logger.GetLogger(typeof(TcpClientSocket)).LogLevel = LogLevel.Info;

var messageFormatter = new LogMessageFormatter();
var timestampFormatter = new TimestampFormatter();
var timestampFormatter = new TimestampFormatter(() => DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
var colorCodeFormatter = new ColorCodeFormatter();
Logger.AddAppender((logger, level, message) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sherlog.Formatters/Sherlog.Formatters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<Version>1.0.0</Version>
<Version>2.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 7 additions & 2 deletions src/Sherlog.Formatters/TimestampFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ namespace Sherlog.Formatters
{
public class TimestampFormatter
{
readonly Func<string> _timeDelegate;
readonly string _timeFormat;

public TimestampFormatter(string timeFormat = "{0:yyyy/MM/dd/hh:mm:ss:fff}") => _timeFormat = timeFormat;
public TimestampFormatter(Func<string> timeDelegate, string timeFormat = "{0:yyyy/MM/dd/hh:mm:ss:fff}")
{
_timeDelegate = timeDelegate;
_timeFormat = timeFormat;
}

public string FormatMessage(Logger logger, LogLevel logLevel, string message) =>
$"{string.Format(_timeFormat, DateTime.Now)} {message}";
$"{string.Format(_timeFormat, _timeDelegate())} {message}";
}
}
15 changes: 15 additions & 0 deletions tests/Sherlog.Formatters.Tests/TimestampFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Globalization;
using FluentAssertions;
using Xunit;

namespace Sherlog.Formatters.Tests
{
public class TimestampFormatterTests
{
[Fact]
public void FormatsString() => new TimestampFormatter(() => new DateTime(2000, 1, 1).ToString(CultureInfo.InvariantCulture), "{0:yyyy/MM/dd/hh:mm:ss:fff}")
.FormatMessage(new Logger("TestLogger"), LogLevel.Debug, "test message")
.Should().Be("01/01/2000 00:00:00 test message");
}
}

0 comments on commit 77c7a13

Please sign in to comment.