Skip to content

Instantly share code, notes, and snippets.

@pimbrouwers
Last active April 18, 2022 20:48
Show Gist options
  • Save pimbrouwers/0308fff598f98bbfe5453d423759cd50 to your computer and use it in GitHub Desktop.
Save pimbrouwers/0308fff598f98bbfe5453d423759cd50 to your computer and use it in GitHub Desktop.
Simple F# Logging Module

Simple F# Logging Module

Self-contained module for scripting or small console programs.

Log.info "Hello world!"    

Log.fail "I'm meltinggggggg"
module Log =
open System
open System.IO
let private logDir = "logs"
let private log kind fmt =
Printf.kprintf (fun s ->
let now = DateTime.Now
let msg = sprintf "[%s] [%s] %s" (now.ToString("s")) kind s
let filename =
Path.Combine (
__SOURCE_DIRECTORY__,
logDir,
sprintf "%s.log" (now.ToString("yyyy-MM-dd")))
Directory.CreateDirectory (Path.GetDirectoryName(filename)) |> ignore
File.AppendAllLines (filename, [|msg|])
printfn "%s" msg) fmt
let fail fmt = log "Fail" fmt
let info fmt = log "Info" fmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment