Git Product home page Git Product logo

Comments (6)

dzmitry-lahoda avatar dzmitry-lahoda commented on July 25, 2024

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>

from fsharp.

TysonMN avatar TysonMN commented on July 25, 2024

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).

from fsharp.

TysonMN avatar TysonMN commented on July 25, 2024

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.

from fsharp.

Evangelink avatar Evangelink commented on July 25, 2024

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"}

from fsharp.

sungam3r avatar sungam3r commented on July 25, 2024

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

from fsharp.

github-actions avatar github-actions commented on July 25, 2024

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

from fsharp.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.