Created
July 24, 2016 17:13
-
-
Save jeremyabbott/38d09efd885640cbe46fcc1f29aaff89 to your computer and use it in GitHub Desktop.
Suave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SuaveLogging.SuaveExample | |
open System | |
open System.IO | |
open System.Net | |
open Suave | |
open Suave.Filters | |
open Suave.Successful | |
open Suave.Logging | |
open Suave.Operators | |
open Suave.Files | |
open Hopac | |
open NodaTime | |
open Logary | |
open Logary.Message | |
open Logary.Logging | |
open Logary.Configuration | |
open Logary.Targets | |
open Logary.Metric | |
open Logary.Metrics.WinPerfCounter | |
open SuaveLogging.Utilities | |
let app () = | |
let path = System.Reflection.Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName | |
let logName = "suave" | |
// Configuration settings for text writer target | |
let textConf = | |
TextWriter.TextWriterConf.create( | |
// The Happy log for std output | |
Path.Combine(path, DateTime.UtcNow.ToString("yyyy-MM") + "-happy.log") |> File.AppendText, | |
// The sad log for error output | |
Path.Combine(path, DateTime.UtcNow.ToString("yyyy-MM") + "-sad.log") |> File.AppendText) | |
let textConf' = { textConf with flush = true } | |
let logary = | |
withLogaryManager logName ( | |
withTargets [ | |
Console.create Console.empty (PointName.ofSingle("console")) | |
Debugger.create Debugger.empty (PointName.ofSingle("debugger")) | |
TextWriter.create textConf' (PointName.ofSingle("textwriter")) | |
] >> | |
(*withMetrics [ | |
MetricConf.create (Duration.FromMilliseconds 3000L) (PointName.ofSingle "cpu") Sample.cpuTime | |
] >>*) | |
withRules [ | |
Rule.createForTarget (PointName.ofSingle("console")) | |
Rule.createForTarget (PointName.ofSingle("debugger")) | |
Rule.createForTarget (PointName.ofSingle("textwriter")) | |
]) | |
|> Hopac.TopLevel.run | |
let webConfig = | |
{ defaultConfig with | |
bindings = [ HttpBinding.mk HTTP IPAddress.Loopback (uint16 8084)] | |
logger = SuaveAdapter(logary.getLogger (PointName.ofSingle "Suave"))} | |
let logRequest s = | |
log logName s | |
let app = | |
choose | |
[ pathScan "/hello/%s" (fun s -> | |
// Log tHeaders requests! | |
logRequest s | |
OK s) | |
Suave.Filters.path "/" >=> (OK "Hello world!")] | |
startWebServer webConfig app | |
0 // return an integer exit code | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SuaveLogging.Utilities | |
open System.IO | |
open Logary | |
open Hopac | |
let deleteIfExists path = | |
if File.Exists path then | |
File.Delete path | |
let log name text = | |
let namedLogger = Logging.getLoggerByName name | |
let writeLog x = Logary.Logger.log namedLogger x |> Hopac.TopLevel.start | |
Logary.Message.eventInfo text |> writeLog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment