package easy_logging

  1. Overview
  2. Docs
Module to log messages. Aimed at being both powerful and easy to use

Install

Dune Dependency

Authors

Maintainers

Sources

v0.8.2.tar.gz
md5=467a966433b97693e0c226a90b9b833e
sha512=d4b97a29225c454a2d8ed04495aa7ecd3b012d7258372be2450e9b672498ba24ce9a3079307e762e12948661bc1e104a90f167fabf60c212fe67b13d6c4edf51

doc/src/easy_logging/logging.ml.html

Source file logging.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
(*
    This file is part of easy_logging.

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at https://mozilla.org/MPL/2.0/.
*)


include Logging_types
open CalendarLib


let debug = ref false

class logger
    ?parent:(parent=None)
    (name: string)
  =
  object(self)

    val name = name

    val mutable level : level = NoLevel

    val mutable handlers : Handlers.t list = []

    val parent : logger option = parent

    val mutable propagate = true

    val mutable tag_generators : (unit -> string) list = []

    method name = name
    method set_level new_level = level <- new_level
    method add_handler h = handlers <- h::handlers

    method get_handlers = handlers
    method set_handlers hs = handlers <- hs

    method set_propagate p = propagate <- p

    method effective_level : level =
      match level, parent  with
      | NoLevel, None  -> NoLevel
      | NoLevel, Some p -> p#effective_level
      | l,_ -> l

    method internal_level = level

    method get_handlers_propagate =
      if !debug
      then
        print_endline (Printf.sprintf "[%s] returning (%i) handlers" name
                         (List.length handlers));
      match propagate, parent with
      | true, Some p -> handlers @ p#get_handlers_propagate
      | _ -> handlers

    method add_tag_generator t  =
      tag_generators <- t :: tag_generators

    method private treat_msg : 'a. ('a -> string) -> string list -> level -> 'a -> unit
      = fun unwrap_fun tags msg_level msg ->

        if !debug
        then
          print_endline ( Printf.sprintf "[%s]/%s -- Treating msg \"%s\" at level %s"
                            name (show_level level)
                            (unwrap_fun msg) (show_level msg_level));

        let generated_tags = List.map (fun x -> x ()) tag_generators in
        let item : log_item= {
          level = msg_level;
          logger_name = name;
          msg = unwrap_fun msg;
          tags=generated_tags @ tags;
          timestamp = Fcalendar.to_unixfloat @@ Fcalendar.now ()
        } in
        List.iter (fun handler ->
            Handlers.apply handler item)
          self#get_handlers_propagate

    method private _log_msg : 'a. ('a -> string) -> string list -> level -> 'a -> unit
      = fun unwrap_fun tags msg_level msg ->
        if msg_level >= self#effective_level
        then
          self#treat_msg unwrap_fun tags msg_level msg
        else
          ()

    method private _flog_msg : 'a. string list -> level -> ('a, unit, string, unit) format4 -> 'a
      =  fun tags msg_level ->
        if msg_level >= self#effective_level
        then
          Printf.ksprintf (
            self#treat_msg (fun x -> x) tags msg_level)
        else Printf.ifprintf ()


    method flash : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Flash
    method error : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Error
    method warning : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Warning
    method info : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Info
    method trace : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Trace
    method debug : 'a. ?tags:string list -> ('a, unit, string, unit) format4 -> 'a
      = fun ?tags:(tags=[]) -> self#_flog_msg tags Debug


    method lflash ?tags:(tags=[]) = self#_log_msg Lazy.force tags Flash
    method lerror ?tags:(tags=[]) = self#_log_msg Lazy.force tags Error
    method lwarning ?tags:(tags=[]) = self#_log_msg Lazy.force tags Warning
    method linfo ?tags:(tags=[]) =  self#_log_msg Lazy.force tags Info
    method ltrace ?tags:(tags=[]) =  self#_log_msg Lazy.force tags Trace
    method ldebug ?tags:(tags=[]) = self#_log_msg Lazy.force tags Debug


    method sflash ?tags:(tags=[]) = self#_log_msg (fun x -> x) tags Flash
    method serror ?tags:(tags=[]) = self#_log_msg (fun x -> x) tags Error
    method swarning ?tags:(tags=[]) = self#_log_msg (fun x -> x) tags Warning
    method sinfo ?tags:(tags=[]) =  self#_log_msg (fun x -> x) tags Info
    method strace ?tags:(tags=[]) =  self#_log_msg (fun x -> x) tags Trace
    method sdebug ?tags:(tags=[]) = self#_log_msg (fun x -> x) tags Debug
  end

let root_logger = new logger "root"

module Infra =
  Logging_infra.MakeTree(
  struct
    type t = logger
    let make (n:string) parent = new logger ~parent n
    let root = root_logger
  end)


let handlers_config = ref Handlers.default_config
let set_handlers_config c = handlers_config := c

let get_logger name =
  Infra.get name

let make_logger ?propagate:(propagate=true) name lvl hdescs  =
  let l = Infra.get name in
  l#set_level lvl;
  l#set_propagate propagate;
  List.iter (fun hdesc -> l#add_handler (Handlers.make ~config:(!handlers_config) hdesc)) hdescs;
  l


let rec _tree_to_yojson tree =
  match tree with
  | Infra.Node (logger, children_map) ->
    let children_json = Infra.SMap.to_seq children_map
                        |> Seq.map (fun (a,b) -> _tree_to_yojson b)
                        |> List.of_seq in

    `Assoc ["name", `String logger#name;
            "level", `String (show_level logger#internal_level);
            "children", `List children_json]
let tree_to_yojson () =
  _tree_to_yojson Infra.internal.data

OCaml

Innovation. Community. Security.