Skip to content

Instantly share code, notes, and snippets.

@blair55
Last active December 4, 2022 21:49
Show Gist options
  • Save blair55/83d0c76c5c3d1a070ee04651cc7c2c40 to your computer and use it in GitHub Desktop.
Save blair55/83d0c76c5c3d1a070ee04651cc7c2c40 to your computer and use it in GitHub Desktop.
Logging example after applying the writer monad
let o x = x :> obj
let getSuperUserData super =
writer {
let sw = Stopwatch.StartNew()
let service = getService ()
let result = service.getSuperUserDetails super
do! write ("serviceElapsed", o sw.ElapsedMilliseconds)
match result with
| Ok res ->
do! write ("serviceResult", o "success")
return (Some res)
| Error e ->
do! write ("serviceResult", o "error")
do! write ("error", o e)
return None
}
let doWork input =
writer {
let sw = Stopwatch.StartNew()
do! write ("userId", o input.UserId)
do!
match input.Status with
| Valid -> write ("status", o "valid")
// ...
| Invalid -> write ("status", o "invalid")
// ...
let! result =
match input.Subscription with
| Basic -> retn None
| Super super -> getSuperUserData (super, log)
do! write ("totalElapsed", o sw.ElapsedMilliseconds)
// ...
return result
}
open System.Text.Json
[<EntryPoint>]
let main input =
let writer = doWork input
let (_, logs) = run writer
Map.ofList logs
|> JsonSerializer.Serialize
|> printfn "%s"
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment