How to customize logging in F# Saturn framework?

1k Views Asked by At

I created a default SAFE app as described here. Removing redundant stuff, the server is this:

open Giraffe
open Saturn

let webApp = scope {
    get "/api/init" (fun next ctx ->
        task {
            let number = 42
            let! counter = task { return number }
            return! Successful.OK counter next ctx
        })
}

let app = application {
    url ("http://0.0.0.0:8085/")
    router webApp
    memory_cache
    use_static "../Client/public"
    use_gzip
}

run app

Now, when running app, I see some logging in the console, basically incoming requests:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:8085/api/init

How do I customize the logging? The docs are as scarce as possible, no examples. I need something simple, like logging "going to return 42...". Or at least some links with cases.

2

There are 2 best solutions below

0
On BEST ANSWER

You can pull the fully blown ILogger object from the context, ctx.

Open Microsoft.Extensions.Logging module and then you can do things like this:

let webApp = scope {
    get "/api/init" (fun next ctx ->
        task {
            let logger = ctx.GetLogger();

            let number = 42
            logger.Log(LogLevel.Information, "Going to return " + number.ToString())

            let! counter = task { return number }
            return! Successful.OK counter next ctx
        })
}

This will bring to your console:

info: object[0]
      Going to return 42

I do not have any proper references. I found a similar thing at the Github of Giraffe server for which Saturn is basically a set of abstractions.

0
On

Logging configuration is built into v0.9 at least. I used the case below for myself to suppress most of the logging.

open Microsoft.Extensions.Logging

let app = application {
    url ("http://0.0.0.0:8085/")
    use_router webApp
    logging (fun logger -> logger.SetMinimumLevel LogLevel.Critical |> ignore)
}