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.5
md5=12fb8044e96ea1ace1cc465c766cde7e
sha512=17ab30169c54a13f3aff8605bd1acaffb911c6882fa3a0c7ad6c14b2dcbd3c08b0f2568fb0ec500ae6e741be926a49fb73954d2ccfedcd274463ffed20149b02

doc/src/easy_logging/handlers.ml.html

Source file handlers.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(** 
This is the [Handlers] module. It provides simple yet adaptable handlers implementation.

 *)


open Easy_logging_types

(** {1 Type definitions } *)

type tag = string
type log_item = {
    level : Easy_logging_types.level;
    logger_name : string;
    msg : string;
    tags : string list
  }
              
type log_formatter = log_item -> string
type filter= log_item -> bool

(** type of a handler *)
type t =
  {
    mutable fmt : log_formatter;
    mutable level : Easy_logging_types.level;
    mutable filters: filter list;
    output : out_channel;
  }

(**
A handler is made of:
 - a formatter that transforms a log item into a string.
 - a level used to filter out items.
 - an array of possible additional custom filters.
 - an [out_channel], where log strings are outputed by the function [Pervasives.output_string].

 *)
  
  
(** {1 Formatting functions} *)

let reduce (f: 'a -> 'a -> 'a) (l: 'a list) (d: 'a) =
  let rec aux l res =
    match l with
    | [] -> res
    | h::t ->
       let res' = f res h in
       aux t res'
  in
  match l with
  | [] -> d
  | h::t -> aux t h 
  
let format_tags (tags : string list) =
  match tags with
  | [] -> ""
  | _ -> 
     let elems_str = reduce (fun s e -> s ^ " | " ^ e) tags ""
     in "[" ^ elems_str ^ "] "

(** Auxiliary functions. *)

let format_default (item : log_item) =
  Printf.sprintf "%-6.3f %-10s %-20s %s%s" (Sys.time ())
    (show_level item.level)
    item.logger_name
    (format_tags item.tags)
    item.msg
(** Human readable log messages. *)
      
let format_color (item : log_item) =
  
  let level_to_color lvl =
    match lvl with
    | Flash -> Colorize.LMagenta
    | Error -> Colorize.LRed
    | Warning -> Colorize.LYellow
    | Info -> Colorize.LBlue
    | Trace -> Colorize.Cyan
    | Debug -> Colorize.Green
    | NoLevel -> Colorize.Default
  in
  
  let item_level_fmt = Colorize.format [ Fg (level_to_color item.level)]  (show_level item.level)
  and logger_name_fmt = Colorize.format [ Underline] item.logger_name
  and item_msg_fmt =
    match item.level with
    | Flash -> Colorize.format [ Fg Black; Bg LMagenta] item.msg
    | _ -> item.msg in

  Format.pp_set_max_indent Format.str_formatter 200;
  Format.sprintf "@[<hov 2>[%-6.3f %-20s %-30s] %s %s@]"
    (Sys.time ())
    item_level_fmt
    logger_name_fmt
    (format_tags item.tags)
    item_msg_fmt
(** Human readable log messages, with level depending colors.*)
  
let format_json (item: log_item) =
  let format_tags tags =
    match tags with
    | [] -> "[]"
    | _ -> 
       let elems_str = reduce (fun s e ->
                           s^", \""^(String.escaped e)^"\"") tags ""
       in "[" ^ elems_str ^ "] "
                              
  in
  
  Printf.sprintf
    "{\"level\": \"%s\", \"logger_name\": \"%s\", \"message\": \"%s\", \"tags\": %s}" 
    (show_level item.level)
    (String.escaped item.logger_name)
    (String.escaped item.msg)
    (format_tags item.tags)
(** JSON logs for software interoperability. *)

(** {1 Handlers creation helpers } *)
      
let make_cli_handler level =
  {fmt = format_color;
   level = level;
   output = stdout;
   filters = []}


  
type file_handlers_config = {
    logs_folder: string;
    truncate: bool;
    file_perms: int}

let file_handlers_defaults = {
    logs_folder = "logs/";
    truncate = true;
    file_perms = 0o660;
  }

type config =
  {mutable file_handlers: file_handlers_config}
let config = {file_handlers = file_handlers_defaults}

let set_config c = config.file_handlers <- c.file_handlers
(** Sets how log files are created when using make_file_handler *)

         
let make_cli_handler level =
  {fmt = format_color;
   level = level;
   output = stdout;
   filters = []}        
let make_file_handler level filename  =
  
  if not (Sys.file_exists config.file_handlers.logs_folder)
  then  
    Unix.mkdir config.file_handlers.logs_folder 0o775;

  let open_flags =
    if config.file_handlers.truncate
    then [Open_wronly; Open_creat;Open_trunc]
    else [Open_wronly; Open_creat]
  in
  let oc = 
    open_out_gen open_flags
      config.file_handlers.file_perms
      (config.file_handlers.logs_folder^filename)
      
  in
  {fmt = format_default;
   level = level;
   output = oc;
   filters = [];
  }
  
  
type desc = | Cli of level | File of string * level
   
let make d = match d with
  | Cli lvl -> make_cli_handler lvl
  | File (f, lvl) -> make_file_handler lvl f
(** Used for quick handler creation, e.g.


 - Cli handler: outputs colored messages to stdout 
   {[ let h = Handlers.make (Cli Debug) ]}
 - File handler : outputs messages to a given file
   {[ let h = Handlers.make (File ("filename", Debug)) ]}
 *)


                   
(** {1 Handlers setup } *)
                   

(** Sets the level of a handler. *)
let set_level (h:t) lvl =
  h.level <- lvl

(** Sets the formatter of a handler. *)
let set_formatter h fmt =
  h.fmt <- fmt

(** Adds a filter to a handler. *)
let add_filter h filter =
  h.filters <- filter::h.filters


(** Auxiliary function.*)
let apply (h : t) (item: log_item) =
  
  if item.level >= h.level && (reduce (&&) (List.map (fun f -> f item) h.filters) true)
  then
    (
      output_string h.output (Printf.sprintf "%s\n" (h.fmt item));
      flush h.output;
    )
OCaml

Innovation. Community. Security.