Skip to content

Commit 39d1528

Browse files
committed
Migrate from Lwt_log to Logs
This replaces all uses of the Lwt_log library by Logs. Lwt_log is deprecated and its README suggest to use Logs instead so it seems like a good choice. The bulk of the change was done with the lwt-log-to-logs tool. This is almost a 1 to 1 translation as the two libraries are very similar but there are some differences: - The "Fatal" log level doesn't exist in Logs. "Error" is used instead. - Log messages are formatted with Format. - Logs has no equivalent of "broadcast" and "dispatch" available out of the box. These operations are easily supported by generating more code. - There is no equivalent for Lwt_log.add_rule in Logs. Luckily it can be avoided in this project. This might cause more work in Ocsigen applications that use their own log sections. - Syslog support is done by the "logs-syslog" library. - There is no equivalent for Lwt_log.close. Ocsigen_messages is still able to close its reporters, except for the syslog reporter, which doesn't support this operation.
1 parent 7ef68ad commit 39d1528

36 files changed

+421
-280
lines changed

src/baselib/ocsigen_loader.ml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open Ocsigen_lib
2323
exception Dynlink_error of string * exn
2424
exception Findlib_error of string * exn
2525

26-
let section = Lwt_log.Section.make "ocsigen:dynlink"
26+
let section = Logs.Src.create "ocsigen:dynlink"
2727

2828
(************************************************************************)
2929

@@ -68,22 +68,23 @@ let loadfile pre post force file =
6868
if force
6969
then (
7070
pre ();
71-
Lwt_log.ign_info_f ~section "Loading %s (will be reloaded every times)"
72-
file;
71+
Logs.info ~src:section (fun fmt ->
72+
fmt "Loading %s (will be reloaded every times)" file);
7373
try
7474
Dynlink_wrapper.loadfile file;
7575
post ()
7676
with e -> post (); raise e)
7777
else if not (isloaded file)
7878
then (
7979
pre ();
80-
Lwt_log.ign_info_f ~section "Loading extension %s" file;
80+
Logs.info ~src:section (fun fmt -> fmt "Loading extension %s" file);
8181
(try
8282
Dynlink_wrapper.loadfile file;
8383
post ()
8484
with e -> post (); raise e);
8585
addloaded file)
86-
else Lwt_log.ign_info_f ~section "Extension %s already loaded" file
86+
else
87+
Logs.info ~src:section (fun fmt -> fmt "Extension %s already loaded" file)
8788
with e -> raise (Dynlink_error (file, e))
8889

8990
let id () = ()
@@ -114,22 +115,25 @@ let init_module pre post force name =
114115
let l = List.rev @@ M.find name !init_functions in
115116
fun () -> List.iter (fun f -> f ()) l
116117
with Not_found ->
117-
Lwt_log.ign_info_f ~section "No init function for named module %s." name;
118+
Logs.info ~src:section (fun fmt ->
119+
fmt "No init function for named module %s." name);
118120
fun () -> ()
119121
in
120122
if force
121123
then (
122124
pre ();
123-
Lwt_log.ign_info_f ~section
124-
"Initializing %s (will be initialized every time)" name;
125+
Logs.info ~src:section (fun fmt ->
126+
fmt "Initializing %s (will be initialized every time)" name);
125127
try f (); post () with e -> post (); raise e)
126128
else if not (isloaded name)
127129
then (
128130
pre ();
129-
Lwt_log.ign_info_f ~section "Initializing module %s " name;
131+
Logs.info ~src:section (fun fmt -> fmt "Initializing module %s " name);
130132
(try f (); post () with e -> post (); raise e);
131133
addloaded name)
132-
else Lwt_log.ign_info_f ~section "Module %s already initialized." name
134+
else
135+
Logs.info ~src:section (fun fmt ->
136+
fmt "Module %s already initialized." name)
133137

134138
(************************************************************************)
135139
(* Manipulating Findlib's search path *)
@@ -170,8 +174,8 @@ let findfiles =
170174
not @@ String.Set.mem a Ocsigen_config_static.builtin_packages)
171175
(Findlib.package_deep_ancestors preds [package])
172176
in
173-
Lwt_log.ign_info_f ~section "Dependencies of %s: %s" package
174-
(String.concat ", " deps);
177+
Logs.info ~src:section (fun fmt ->
178+
fmt "Dependencies of %s: %s" package (String.concat ", " deps));
175179
let rec aux = function
176180
| [] -> []
177181
| a :: q ->
@@ -189,7 +193,8 @@ let findfiles =
189193
List.map (Findlib.resolve_path ~base) mods @ aux q
190194
in
191195
let res = aux deps in
192-
Lwt_log.ign_info_f ~section "Needed: %s" (String.concat ", " res);
196+
Logs.info ~src:section (fun fmt ->
197+
fmt "Needed: %s" (String.concat ", " res));
193198
res
194199
with e -> raise (Findlib_error (package, e))
195200

src/baselib/ocsigen_loader.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
exception Dynlink_error of string * exn
3333
exception Findlib_error of string * exn
3434

35-
val section : Lwt_log_core.section
35+
val section : Logs.src
3636
(** use Lwt_log.Section.set_level in order to debug *)
3737

3838
val translate : string -> string

src/extensions/accesscontrol.ml

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
open Ocsigen_lib
2424
open Xml
2525

26-
let section = Lwt_log.Section.make "ocsigen:ext:access-control"
26+
let section = Logs.Src.create "ocsigen:ext:access-control"
2727

2828
type condition = Ocsigen_request.t -> bool
2929

@@ -41,30 +41,28 @@ let ip s =
4141
let r = Ipaddr.Prefix.mem (Ocsigen_request.remote_ip_parsed ri) prefix in
4242
if r
4343
then
44-
Lwt_log.ign_info_f ~section "IP: %a matches %s"
45-
(fun () -> Ocsigen_request.remote_ip)
46-
ri s
44+
Logs.info ~src:section (fun fmt ->
45+
fmt "IP: %s matches %s" (Ocsigen_request.remote_ip ri) s)
4746
else
48-
Lwt_log.ign_info_f ~section "IP: %a does not match %s"
49-
(fun () -> Ocsigen_request.remote_ip)
50-
ri s;
47+
Logs.info ~src:section (fun fmt ->
48+
fmt "IP: %s does not match %s" (Ocsigen_request.remote_ip ri) s);
5149
r
5250

5351
let port port ri =
5452
let r = Ocsigen_request.port ri = port in
5553
if r
56-
then Lwt_log.ign_info_f ~section "PORT = %d: true" port
54+
then Logs.info ~src:section (fun fmt -> fmt "PORT = %d: true" port)
5755
else
58-
Lwt_log.ign_info_f ~section "PORT = %d: false (it is %a)" port
59-
(fun () ri -> string_of_int (Ocsigen_request.port ri))
60-
ri;
56+
Logs.info ~src:section (fun fmt ->
57+
fmt "PORT = %d: false (it is %s)" port
58+
(string_of_int (Ocsigen_request.port ri)));
6159
r
6260

6361
let ssl ri =
6462
let r = Ocsigen_request.ssl ri in
6563
if r
66-
then Lwt_log.ign_info ~section "SSL: true"
67-
else Lwt_log.ign_info ~section "SSL: false";
64+
then Logs.info ~src:section (fun fmt -> fmt "SSL: true")
65+
else Logs.info ~src:section (fun fmt -> fmt "SSL: false");
6866
r
6967

7068
let header ~name ~regexp:re =
@@ -79,12 +77,15 @@ let header ~name ~regexp:re =
7977
List.exists
8078
(fun a ->
8179
let r = Netstring_pcre.string_match regexp a 0 <> None in
82-
if r then Lwt_log.ign_info_f "HEADER: header %s matches %S" name re;
80+
if r
81+
then
82+
Logs.info (fun fmt -> fmt "HEADER: header %s matches %S" name re);
8383
r)
8484
(Ocsigen_request.header_multi ri (Ocsigen_header.Name.of_string name))
8585
in
8686
if not r
87-
then Lwt_log.ign_info_f "HEADER: header %s does not match %S" name re;
87+
then
88+
Logs.info (fun fmt -> fmt "HEADER: header %s does not match %S" name re);
8889
r
8990

9091
let method_ m ri =
@@ -93,8 +94,9 @@ let method_ m ri =
9394
let s' = Cohttp.Code.string_of_method m' in
9495
let r = m = m' in
9596
if r
96-
then Lwt_log.ign_info_f ~section "METHOD: %s matches %s" s' s
97-
else Lwt_log.ign_info_f ~section "METHOD: %s does not match %s" s' s;
97+
then Logs.info ~src:section (fun fmt -> fmt "METHOD: %s matches %s" s' s)
98+
else
99+
Logs.info ~src:section (fun fmt -> fmt "METHOD: %s does not match %s" s' s);
98100
r
99101

100102
let protocol v ri =
@@ -103,8 +105,10 @@ let protocol v ri =
103105
let s' = Cohttp.Code.string_of_version v' in
104106
let r = v = v' in
105107
if r
106-
then Lwt_log.ign_info_f ~section "PROTOCOL: %s matches %s" s' s
107-
else Lwt_log.ign_info_f ~section "PROTOCOL: %s does not match %s" s' s;
108+
then Logs.info ~src:section (fun fmt -> fmt "PROTOCOL: %s matches %s" s' s)
109+
else
110+
Logs.info ~src:section (fun fmt ->
111+
fmt "PROTOCOL: %s does not match %s" s' s);
108112
r
109113

110114
let path ~regexp:s =
@@ -118,8 +122,10 @@ let path ~regexp:s =
118122
let sps = Ocsigen_request.sub_path_string ri in
119123
let r = Netstring_pcre.string_match regexp sps 0 <> None in
120124
if r
121-
then Lwt_log.ign_info_f ~section "PATH: \"%s\" matches %S" sps s
122-
else Lwt_log.ign_info_f ~section "PATH: \"%s\" does not match %S" sps s;
125+
then Logs.info ~src:section (fun fmt -> fmt "PATH: \"%s\" matches %S" sps s)
126+
else
127+
Logs.info ~src:section (fun fmt ->
128+
fmt "PATH: \"%s\" does not match %S" sps s);
123129
r
124130

125131
let and_ sub ri = List.for_all (fun cond -> cond ri) sub
@@ -167,8 +173,12 @@ let rec parse_condition = function
167173
let sps = Ocsigen_request.sub_path_string ri in
168174
let r = Netstring_pcre.string_match regexp sps 0 <> None in
169175
if r
170-
then Lwt_log.ign_info_f ~section "PATH: \"%s\" matches %S" sps s
171-
else Lwt_log.ign_info_f ~section "PATH: \"%s\" does not match %S" sps s;
176+
then
177+
Logs.info ~src:section (fun fmt ->
178+
fmt "PATH: \"%s\" matches %S" sps s)
179+
else
180+
Logs.info ~src:section (fun fmt ->
181+
fmt "PATH: \"%s\" does not match %S" sps s);
172182
r
173183
| Element (("path" as s), _, _) ->
174184
Ocsigen_extensions.badconfig "Bad syntax for tag %s" s
@@ -192,11 +202,11 @@ let rec parse_condition = function
192202
(*****************************************************************************)
193203
(* Parsing filters *)
194204

195-
let comma_space_regexp = Netstring_pcre.regexp "\ *,\ *"
205+
let comma_space_regexp = Netstring_pcre.regexp " *, *"
196206

197207
let allow_forward_for_handler ?(check_equal_ip = false) () =
198208
let apply ({Ocsigen_extensions.request_info; _} as request) code =
199-
Lwt_log.ign_info ~section "Allowed proxy";
209+
Logs.info ~src:section (fun fmt -> fmt "Allowed proxy");
200210
let request =
201211
let header =
202212
Ocsigen_request.header request_info Ocsigen_header.Name.x_forwarded_for
@@ -218,14 +228,15 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
218228
~remote_ip:original_ip request_info }
219229
else (
220230
(* the announced ip of the proxy is not its real ip *)
221-
Lwt_log.ign_warning_f ~section
222-
"X-Forwarded-For: host ip (%s) does not match the header (%s)"
223-
(Ocsigen_request.remote_ip request_info)
224-
header;
231+
Logs.warn ~src:section (fun fmt ->
232+
fmt
233+
"X-Forwarded-For: host ip (%s) does not match the header (%s)"
234+
(Ocsigen_request.remote_ip request_info)
235+
header);
225236
request)
226237
| _ ->
227-
Lwt_log.ign_info_f ~section "Malformed X-Forwarded-For field: %s"
228-
header;
238+
Logs.info ~src:section (fun fmt ->
239+
fmt "Malformed X-Forwarded-For field: %s" header);
229240
request)
230241
| None -> request
231242
in
@@ -240,7 +251,7 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
240251

241252
let allow_forward_proto_handler =
242253
let apply ({Ocsigen_extensions.request_info; _} as request) code =
243-
Lwt_log.ign_info ~section "Allowed proxy for ssl";
254+
Logs.info ~src:section (fun fmt -> fmt "Allowed proxy for ssl");
244255
let request_info =
245256
let header =
246257
Ocsigen_request.header request_info
@@ -252,8 +263,8 @@ let allow_forward_proto_handler =
252263
| "http" -> Ocsigen_request.update ~ssl:false request_info
253264
| "https" -> Ocsigen_request.update ~ssl:true request_info
254265
| _ ->
255-
Lwt_log.ign_info_f ~section "Malformed X-Forwarded-Proto field: %s"
256-
header;
266+
Logs.info ~src:section (fun fmt ->
267+
fmt "Malformed X-Forwarded-Proto field: %s" header);
257268
request_info)
258269
| None -> request_info
259270
in
@@ -292,17 +303,19 @@ let parse_config parse_fun = function
292303
Lwt.return
293304
(if condition ri.Ocsigen_extensions.request_info
294305
then (
295-
Lwt_log.ign_info ~section "COND: going into <then> branch";
306+
Logs.info ~src:section (fun fmt ->
307+
fmt "COND: going into <then> branch");
296308
Ocsigen_extensions.Ext_sub_result ithen)
297309
else (
298-
Lwt_log.ign_info ~section
299-
"COND: going into <else> branch, if any";
310+
Logs.info ~src:section (fun fmt ->
311+
fmt "COND: going into <else> branch, if any");
300312
Ocsigen_extensions.Ext_sub_result ielse)))
301313
| Element (("if" as s), _, _) ->
302314
Ocsigen_extensions.badconfig "Bad syntax for tag %s" s
303315
| Element ("notfound", [], []) ->
304316
fun _rs ->
305-
Lwt_log.ign_info ~section "NOT_FOUND: taking in charge 404";
317+
Logs.info ~src:section (fun fmt ->
318+
fmt "NOT_FOUND: taking in charge 404");
306319
Lwt.return
307320
(Ocsigen_extensions.Ext_stop_all (Ocsigen_cookie_map.empty, `Not_found))
308321
| Element (("notfound" as s), _, _) ->
@@ -340,7 +353,8 @@ let parse_config parse_fun = function
340353
Ocsigen_extensions.badconfig "Bad syntax for tag %s" s
341354
| Xml.Element ("forbidden", [], []) ->
342355
fun _rs ->
343-
Lwt_log.ign_info ~section "FORBIDDEN: taking in charge 403";
356+
Logs.info ~src:section (fun fmt ->
357+
fmt "FORBIDDEN: taking in charge 403");
344358
Lwt.return
345359
(Ocsigen_extensions.Ext_stop_all (Ocsigen_cookie_map.empty, `Forbidden))
346360
| Element (("forbidden" as s), _, _) ->

src/extensions/accesscontrol.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ val allow_forward_for :
8888

8989
val allow_forward_proto : unit -> Ocsigen_server.instruction
9090

91-
val section : Lwt_log_core.section
91+
val section : Logs.src
9292
(** Use Lwt_log.Section.set_level in order to change the log level *)

src/extensions/authbasic.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
open Lwt.Infix
2222

23-
let section = Lwt_log.Section.make "ocsigen:ext:access-control"
23+
let section = Logs.Src.create "ocsigen:ext:access-control"
2424

2525
type auth = string -> string -> bool Lwt.t
2626

@@ -56,10 +56,10 @@ let gen ~realm ~auth rs =
5656
Cohttp.Header.init_with "WWW-Authenticate"
5757
(Printf.sprintf "Basic realm=\"%s\"" realm)
5858
in
59-
Lwt_log.ign_info ~section "AUTH: invalid credentials!";
59+
Logs.info ~src:section (fun fmt -> fmt "AUTH: invalid credentials!");
6060
Lwt.fail (Ocsigen_cohttp.Ext_http_error (`Unauthorized, None, Some h))
6161
and invalid_header () =
62-
Lwt_log.ign_info ~section "AUTH: invalid Authorization header";
62+
Logs.info ~src:section (fun fmt -> fmt "AUTH: invalid Authorization header");
6363
Lwt.fail
6464
(Ocsigen_cohttp.Ocsigen_http_error (Ocsigen_cookie_map.empty, `Bad_request))
6565
in

src/extensions/authbasic.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let _ =
5656
very naive one (authentication with a single user/password, given
5757
in the configuration file) is provided. *)
5858

59-
val section : Lwt_log_core.section
59+
val section : Logs.src
6060
(** use [Lwt_log.Section.set_level] in order to set the log level *)
6161

6262
type auth = string -> string -> bool Lwt.t

src/extensions/cors.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
(** Handle Cross-Origin Resource Sharing (CORS) headers *)
2222

23-
let section = Lwt_log.Section.make "ocsigen:ext:cors"
23+
let section = Logs.Src.create "ocsigen:ext:cors"
2424

2525
(*** MAIN FUNCTION ***)
2626

@@ -40,7 +40,7 @@ let add_headers config r response =
4040
match Ocsigen_request.header r Ocsigen_header.Name.origin with
4141
| None -> Lwt.return Ocsigen_extensions.Ext_do_nothing
4242
| Some origin ->
43-
Lwt_log.ign_info_f ~section "request with origin: %s" origin;
43+
Logs.info ~src:section (fun fmt -> fmt "request with origin: %s" origin);
4444
let l = [Ocsigen_header.Name.access_control_allow_origin, origin] in
4545
let l =
4646
if config.credentials
@@ -65,7 +65,7 @@ let add_headers config r response =
6565
(Ocsigen_header.Name.access_control_allow_methods, request_method)
6666
:: l
6767
else (
68-
Lwt_log.ign_info ~section "Method refused";
68+
Logs.info ~src:section (fun fmt -> fmt "Method refused");
6969
raise Refused)
7070
| None -> l
7171
in
@@ -103,15 +103,15 @@ let main config = function
103103
-> (
104104
match Ocsigen_request.meth request_info with
105105
| `OPTIONS -> (
106-
Lwt_log.ign_info ~section "OPTIONS request";
106+
Logs.info ~src:section (fun fmt -> fmt "OPTIONS request");
107107
try add_headers config request_info (default_frame ())
108108
with Refused ->
109-
Lwt_log.ign_info ~section "Refused request";
109+
Logs.info ~src:section (fun fmt -> fmt "Refused request");
110110
Lwt.return Ocsigen_extensions.Ext_do_nothing)
111111
| _ -> Lwt.return Ocsigen_extensions.Ext_do_nothing)
112112
| Ocsigen_extensions.Req_found ({Ocsigen_extensions.request_info; _}, response)
113113
->
114-
Lwt_log.ign_info ~section "answered request";
114+
Logs.info ~src:section (fun fmt -> fmt "answered request");
115115
add_headers config request_info response
116116

117117
(* Register extension *)

0 commit comments

Comments
 (0)