package sihl

  1. Overview
  2. Docs

Source file core_log.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
let get_log_level () =
  match Sys.getenv_opt "LOG_LEVEL" with
  | Some "debug" -> Some Logs.Debug
  | Some "error" -> Some Logs.Error
  | Some "warning" -> Some Logs.Warning
  | _ -> Some Logs.Info
;;

let logs_dir () =
  match
    Core_configuration.root_path (), Core_configuration.read_string "LOGS_DIR"
  with
  | _, Some logs_dir -> logs_dir
  | Some root, None -> root ^ "/logs"
  | None, None -> "logs"
;;

let lwt_file_reporter () =
  let logs_dir = logs_dir () in
  let buf () =
    let b = Buffer.create 512 in
    ( b
    , fun () ->
        let m = Buffer.contents b in
        Buffer.reset b;
        m )
  in
  let app, app_flush = buf () in
  let err, err_flush = buf () in
  let report src level ~over k msgf =
    let k _ = k () in
    let write () =
      let name =
        match level with
        | Logs.Error -> logs_dir ^ "/error.log"
        | _ -> logs_dir ^ "/app.log"
      in
      let%lwt log =
        Lwt_io.open_file
          ~flags:[ Unix.O_WRONLY; Unix.O_CREAT; Unix.O_APPEND ]
          ~perm:0o777
          ~mode:Lwt_io.Output
          name
      in
      let%lwt () =
        match level with
        | Logs.Error -> Lwt_io.write log (err_flush ())
        | _ -> Lwt_io.write log (app_flush ())
      in
      Lwt_io.close log
    in
    let unblock () =
      over ();
      Lwt.return_unit
    in
    Lwt.finalize write unblock |> Lwt.ignore_result;
    msgf
    @@ fun ?header:_ ?tags:_ fmt ->
    let now = Ptime_clock.now () |> Ptime.to_rfc3339 in
    match level with
    | Logs.Error ->
      let ppf = Format.formatter_of_buffer err in
      Format.kfprintf
        k
        ppf
        ("%s [%s]: @[" ^^ fmt ^^ "@]@.")
        now
        (Logs.Src.name src)
    | _ ->
      let ppf = Format.formatter_of_buffer app in
      Format.kfprintf
        k
        ppf
        ("%s [%a] [%s]: @[" ^^ fmt ^^ "@]@.")
        now
        Logs.pp_level
        level
        (Logs.Src.name src)
  in
  { Logs.report }
;;

let app_style = `Cyan
let err_style = `Red
let warn_style = `Yellow
let info_style = `Blue
let debug_style = `Green
let source_style = `Magenta

let pp_header ~pp_h ppf (l, h) =
  match l with
  | Logs.App ->
    (match h with
    | None -> ()
    | Some h -> Fmt.pf ppf "[%a] " Fmt.(styled app_style string) h)
  | Logs.Error ->
    pp_h
      ppf
      err_style
      (match h with
      | None -> "ERROR"
      | Some h -> h)
  | Logs.Warning ->
    pp_h
      ppf
      warn_style
      (match h with
      | None -> "WARNING"
      | Some h -> h)
  | Logs.Info ->
    pp_h
      ppf
      info_style
      (match h with
      | None -> "INFO"
      | Some h -> h)
  | Logs.Debug ->
    pp_h
      ppf
      debug_style
      (match h with
      | None -> "DEBUG"
      | Some h -> h)
;;

let pp_source = Fmt.(styled source_style string)

let pp_exec_header src =
  let pp_h ppf style h =
    let src = Logs.Src.name src in
    let now = Ptime_clock.now () |> Ptime.to_rfc3339 in
    Fmt.pf ppf "%s [%a] [%a]: " now Fmt.(styled style string) h pp_source src
  in
  pp_header ~pp_h
;;

let format_reporter
    ?(pp_header = pp_exec_header)
    ?(app = Format.std_formatter)
    ?(dst = Format.err_formatter)
    ()
  =
  let report src level ~over k msgf =
    let k _ =
      over ();
      k ()
    in
    msgf
    @@ fun ?header ?tags:_ fmt ->
    let ppf = if level = Logs.App then app else dst in
    Format.kfprintf
      k
      ppf
      ("%a@[" ^^ fmt ^^ "@]@.")
      (pp_header src)
      (level, header)
  in
  { Logs.report }
;;

let cli_reporter ?(pp_header = pp_exec_header) ?app ?dst () =
  Fmt_tty.setup_std_outputs ();
  format_reporter ~pp_header ?app ?dst ()
;;

let combine r1 r2 =
  let report src level ~over k msgf =
    let v = r1.Logs.report src level ~over:(fun () -> ()) k msgf in
    r2.Logs.report src level ~over (fun () -> v) msgf
  in
  { Logs.report }
;;

let default_reporter =
  Logs.set_level (get_log_level ());
  let r1 = lwt_file_reporter () in
  let r2 = cli_reporter () in
  combine r1 r2
;;
OCaml

Innovation. Community. Security.