Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the difference example of look with and without destructurama #18

Closed
dzmitry-lahoda opened this issue Jan 29, 2020 · 6 comments
Closed
Labels
documentation Improvements or additions to documentation stale

Comments

@dzmitry-lahoda
Copy link

Case 1:

  • with destructurama
  • without destructurama

Case 2:

  • with destructurama and with json formatter
  • without destructurama and with json formatter
@dzmitry-lahoda
Copy link
Author

dzmitry-lahoda commented Jan 30, 2020

not sure what i have to test more:

module Tests


open Expecto
open System
open Serilog
open Serilog.Core
open Serilog.Context
open Serilog.Events
open Serilog.Formatting.Compact
open Destructurama.FSharp

let loggerWithJsonAndDestructure =
    LoggerConfiguration()
        .MinimumLevel.Is(LogEventLevel.Debug)
        .Destructure.FSharpTypes()
        .Enrich.FromLogContext()
        .WriteTo.Console(
            formatter = CompactJsonFormatter()
        )
        .CreateLogger() :> ILogger

let loggerWithJson =
    LoggerConfiguration()
        .MinimumLevel.Is(LogEventLevel.Debug)
        .Enrich.FromLogContext()
        .WriteTo.Console(
            formatter = CompactJsonFormatter()
        )
        .CreateLogger() :> ILogger

type X = Y of int | Z of string
type R = {
  A: int
  B: string
}

type K = R * X

let r = {A = 42; B = "t"}
let k = (r, Y 27)

[<Tests>]
let tests =
  testList "samples" [

    test "u" {
      let _ = LogContext.PushProperty("z", Y 13)
      let _ = LogContext.PushProperty("l", "value")
      loggerWithJsonAndDestructure.Fatal("{UnionDes}", Y 1)
      loggerWithJson.Error("{Union}", Y 1)
      Expect.isTrue true "true"
    }

    test "r" {
      let _ = LogContext.PushProperty("f", {A = 12; B = "bbb"})
      let _ = LogContext.PushProperty("l", "value")
      loggerWithJsonAndDestructure.Fatal("{RecordDes}", {A = 1; B = "bDes"})
      loggerWithJson.Error("{Record}", {A = 1; B = "b"})
      Expect.isTrue true "true"
    } 

    test "t" {
      let _ = LogContext.PushProperty("o", k)
      let _ = LogContext.PushProperty("l", "value")
      loggerWithJsonAndDestructure.Fatal("{TupleDes}", k)
      loggerWithJson.Error("{Tuple}", k)
      Expect.isTrue true "true"
    }         
  ]

with json

{"@t":"2020-01-30T12:55:49.1338020Z","@mt":"{UnionDes}","@l":"Fatal","UnionDes":"Y 1","l":"value","z":"Y 13"}
{"@t":"2020-01-30T12:55:49.1338020Z","@mt":"{RecordDes}","@l":"Fatal","RecordDes":"{ A = 1\n  B = \"b\" }","l":"value","f":"{ A = 12\n  B = \"bbb\" }"}
{"@t":"2020-01-30T12:55:49.1338820Z","@mt":"{TupleDes}","@l":"Fatal","TupleDes":"({ A = 42\n  B = \"t\" }, Y 27)","l":"value","o":"({ A = 42\n  B = \"t\" }, Y 27)"}
{"@t":"2020-01-30T12:55:49.1397800Z","@mt":"{Record}","@l":"Error","Record":"{ A = 1\n  B = \"b\" }","l":"value","f":"{ A = 12\n  B = \"bbb\" }"}
{"@t":"2020-01-30T12:55:49.1398150Z","@mt":"{Union}","@l":"Error","Union":"Y 1","l":"value","z":"Y 13"}
{"@t":"2020-01-30T12:55:49.1398910Z","@mt":"{Tuple}","@l":"Error","Tuple":"({ A = 42\n  B = \"t\" }, Y 27)","l":"value","o":"({ A = 42\n  B = \"t\" }, Y 27)"}

without

[15:56:29 FTL] ({ A = 42
  B = "t" }, Y 27)
[15:56:29 FTL] { A = 1
  B = "b" }
[15:56:29 FTL] Y 1
[15:56:29 ERR] { A = 1
  B = "b" }
[15:56:29 ERR] Y 1
[15:56:29 ERR] ({ A = 42
  B = "t" }, Y 27)

have not tested collections so.

Not sure for usual console output, but I am searching for tool which will make F# objects look like JSON objects in JSON. So these are easily indexed but logging storages.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Sample.fs" />
    <Compile Include="Main.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Destructurama.FSharp" Version="1.1.1-dev-00035" />
    <PackageReference Include="Expecto" Version="8.*" />
    <PackageReference Include="FSharp.Core" Version="4.*" />
    <PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00839" />
    <PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.*" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
  </ItemGroup>
</Project>

@TysonMN
Copy link
Contributor

TysonMN commented Feb 28, 2021

I completely agree with this. I came here to looking for the same comparison.

It would also be nice to see the event in JSON format or specifically what it looks like in Seq (my favorite Serilog sink).

@TysonMN
Copy link
Contributor

TysonMN commented Mar 2, 2021

Here is some more feedback about this.

I added Destructurama.Fsharp to my application at work, but I couldn't find any existing log call that was improved.

Looking more closely at the one example in the README (and combining it with what I think the output would have been without Destructurama.Fsharp), the impression I get is that Destructurama.Fsharp only improves the logging of discriminated unions that have case data that is both suffering from primitive obsession but also compensates for it by giving names to the fields.

Is my impression accurate?

The case data in the discriminated unions in my application at work are not suffering from primitive obsession. Instead, I create stronger types with single-case discriminated unions. This would explain why I was unable to find any logging in my application at work that was improved by Destructurama.Fsharp.

@Evangelink
Copy link

I think the readme should be tweaked a bit.

Assuming the code in the readme:

type Shape =
    | Circle of Radius : double
    | Rectangle of Width : double *  Height : double
    | Arc // ...
let shape = Circle 5.
Log.Information("Drawing a {@Shape}", shape)

Note that the @ is important to get it to work properly.

Without Destructurama.FSharp:

2021-04-14 10:20:27.564 +02:00 [INF] Drawing a {"Radius":5,"Tag":0,"IsCircle":true,"IsRectangle":false,"IsArc":false,"$type":"Circle"}

With Destructurama.FSharp:

2021-04-14 10:20:42.719 +02:00 [INF] Drawing a {"Radius":5,"$type":"Circle"}

@sungam3r
Copy link
Member

@dzmitry-lahoda @TysonMN @Evangelink PR is welcome!

@sungam3r sungam3r added the documentation Improvements or additions to documentation label Jan 30, 2024
Copy link

github-actions bot commented Mar 1, 2024

This issue was marked as stale since it has not been active for a long time

@github-actions github-actions bot added the stale label Mar 1, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation stale
Projects
None yet
Development

No branches or pull requests

4 participants