Skip to content

Instantly share code, notes, and snippets.

@anmonteiro
Created February 12, 2020 01:37
Show Gist options
  • Save anmonteiro/3b3fd4810dc6481eb662b1e4675a5e36 to your computer and use it in GitHub Desktop.
Save anmonteiro/3b3fd4810dc6481eb662b1e4675a5e36 to your computer and use it in GitHub Desktop.
module type FILE = sig
val __FILE__ : string
end
module Setup (File : FILE) = struct
let src = Logs.Src.create File.__FILE__
module Logs = (val Logs.src_log src : Logs.LOG)
end
let file f =
let module File = struct
let __FILE__ = f
end
in
(module File : FILE)
let setup_logging ?style_renderer level =
let pp_header src ppf (l, h) =
let src_segment =
if Logs.Src.equal src Logs.default then
match Array.length Sys.argv with
| 0 ->
Filename.basename Sys.executable_name
| _n ->
Filename.basename Sys.argv.(0)
else
Logs.Src.name src
in
Format.fprintf
ppf
"[%a] %a [%s] "
Time.pp_hum
(Time.now ())
Logs_fmt.pp_header
(l, h)
src_segment
in
let format_reporter =
let report src =
let { Logs.report } = Logs_fmt.reporter ~pp_header:(pp_header src) () in
report src
in
{ Logs.report }
in
Fmt_tty.setup_std_outputs ?style_renderer ();
Logs.set_level level;
Logs.set_reporter format_reporter
@anmonteiro
Copy link
Author

anmonteiro commented May 15, 2020

Usage:

include Logging.Setup ((val Logging.file __FILE__))

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