Skip to content

Instantly share code, notes, and snippets.

@mariomeyrelles
Created January 10, 2017 14:10
Show Gist options
  • Save mariomeyrelles/a7d4ac70cf01af70d076b56a9375019b to your computer and use it in GitHub Desktop.
Save mariomeyrelles/a7d4ac70cf01af70d076b56a9375019b to your computer and use it in GitHub Desktop.
Configuring Suave 2.0 to use Logary
open System
open Suave
open Suave.Successful
open Suave.RequestErrors
open Suave.Operators
open Suave.RequestErrors
open Suave.Filters
open Suave.Logging
open Logary
open Logary
open Logary.Configuration
open Logary.Targets
open Logary.Metric
open Logary.Metrics
open Logary.Message
open Logary.Adapters.Facade
open NodaTime
open Hopac
[<EntryPoint>]
let main argv =
// this is our logManager.
let logary =
withLogaryManager "TestApi" (
// configure here some rules. You can change parts of the rule using the construct below.
// note also that you can add many different rules here. So here we are adding to the rules
// an instruction to configure the "console" target, with the minimum log level to be Info, which
// will hide verbose logs of Suave.
withRules
[
{ Rule.createForTarget "console" with level = Info }
]
// the literate console target is used to output text in different colors for different types of logs. Much
// nicer than the normal console.
>> withTarget (LiterateConsole.create LiterateConsole.empty "console")
// the Logary configuration itself is a representation of a Hopac job, that is only defined but not executed
// until the it's told to be executed. So we use run to ask for immediate execution.
) |> run
// this is the most important part - initializes the Logary with a Suave logger.
LogaryFacadeAdapter.initialise logary
// gets a logger from the context of this function
let logger = Logging.getCurrentLogger()
// this is a function that is called when a post request is received. Note that the logger here is the Logary's one.
let logInsidePost (logger: Logger) =
// imperatively logs something to be shown on the coloured console defined above. The log level defined on the
// rule above still applies here. Only information and above will be shown.
logger.info (
eventX "********hello world************"
)
OK "postOk"
let testApi =
choose
[
// when a POST request is received, the function will run and log normally.
POST >=> path "/post" >=> warbler (fun x -> logInsidePost logger)
NOT_FOUND "Found no handlers"
]
// we don't need to change the config anymore, as we used to do in previous version of Suave
startWebServer defaultConfig testApi
Console.ReadLine() |> ignore
0
@mariomeyrelles
Copy link
Author

This the result:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment