move journald functionality out of this module
This commit is contained in:
parent
4e76f70147
commit
9399b6858e
|
@ -37,14 +37,6 @@ end
|
||||||
Xlog.init_pretty_writer stdout
|
Xlog.init_pretty_writer stdout
|
||||||
~min_level:Xlog.DEBUG
|
~min_level:Xlog.DEBUG
|
||||||
(* other options *)
|
(* other options *)
|
||||||
|
|
||||||
(* if running as a daemon *)
|
|
||||||
if Xlog.should_upgrade_to_journald () then
|
|
||||||
Xlog.init_journald_writer ()
|
|
||||||
~min_level:Xlog.DEBUG
|
|
||||||
else
|
|
||||||
Xlog.init_pretty_writer stdout
|
|
||||||
~min_level:Xlog.DEBUG
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## usage with ppx
|
## usage with ppx
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(lang dune 3.15)
|
(lang dune 3.15)
|
||||||
|
|
||||||
(name xlog)
|
(name xlog)
|
||||||
(version 0.0.2)
|
(version 0.1.0)
|
||||||
|
|
||||||
(generate_opam_files true)
|
(generate_opam_files true)
|
||||||
|
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
open Core
|
|
||||||
|
|
||||||
let default_socket_path = "/run/systemd/journal/socket"
|
|
||||||
|
|
||||||
type t = {
|
|
||||||
mutex : Mutex.t;
|
|
||||||
sock_fd : Unix.file_descr;
|
|
||||||
dest : Unix.sockaddr;
|
|
||||||
buf : Buffer.t;
|
|
||||||
}
|
|
||||||
|
|
||||||
let should_upgrade () =
|
|
||||||
let stderr = Unix.fstat Unix.stderr in
|
|
||||||
let dev_ino = Printf.sprintf "%d:%d" stderr.st_dev stderr.st_ino in
|
|
||||||
Sys.getenv_opt "JOURNAL_STREAM" = Some dev_ino
|
|
||||||
|
|
||||||
let make ?(path = default_socket_path) () = {
|
|
||||||
mutex = Mutex.create ();
|
|
||||||
sock_fd = Unix.socket PF_UNIX SOCK_DGRAM 0 ~cloexec:true;
|
|
||||||
dest = ADDR_UNIX path;
|
|
||||||
buf = Buffer.create 256;
|
|
||||||
}
|
|
||||||
|
|
||||||
let add_field dgram key value =
|
|
||||||
if String.contains value '\n' then
|
|
||||||
begin
|
|
||||||
Buffer.add_string dgram key;
|
|
||||||
Buffer.add_char dgram '\n';
|
|
||||||
Buffer.add_int64_le dgram (Int64.of_int (String.length value));
|
|
||||||
Buffer.add_string dgram value;
|
|
||||||
Buffer.add_char dgram '\n';
|
|
||||||
end
|
|
||||||
else
|
|
||||||
Printf.bprintf dgram "%s=%s\n" key value
|
|
||||||
|
|
||||||
let syslog_priority = function
|
|
||||||
| TRACE
|
|
||||||
| DEBUG -> "7" (* LOG_DEBUG *)
|
|
||||||
| INFO -> "6" (* LOG_INFO *)
|
|
||||||
| WARN -> "4" (* LOG_WARNING *)
|
|
||||||
| ERROR -> "3" (* LOG_ERR *)
|
|
||||||
|
|
||||||
let writer t ~ts ~ns ~filename ~lineno ~func ~errno ~exn ~lvl msg =
|
|
||||||
Mutex.lock t.mutex;
|
|
||||||
let dgram =
|
|
||||||
Buffer.clear t.buf;
|
|
||||||
ignore ts;
|
|
||||||
let maybe_exn = match exn with
|
|
||||||
| Some (exn, bt) ->
|
|
||||||
Printf.sprintf "\nException: %s\n%s" (Printexc.to_string exn)
|
|
||||||
(Printexc.raw_backtrace_to_string bt)
|
|
||||||
| None -> ""
|
|
||||||
in
|
|
||||||
add_field t.buf "MESSAGE" (Printf.sprintf "%s: %s%s" ns msg maybe_exn);
|
|
||||||
add_field t.buf "PRIORITY" (syslog_priority lvl);
|
|
||||||
begin if not (filename = "") then
|
|
||||||
add_field t.buf "CODE_FILE" filename;
|
|
||||||
add_field t.buf "CODE_LINE" (Printf.sprintf "%d" lineno)
|
|
||||||
end;
|
|
||||||
begin if not (func = "") then
|
|
||||||
add_field t.buf "CODE_FUNC" func
|
|
||||||
end;
|
|
||||||
let errno = Util.get_errno errno exn in
|
|
||||||
begin if errno != 0 then
|
|
||||||
add_field t.buf "ERRNO" (Printf.sprintf "%d" errno)
|
|
||||||
end;
|
|
||||||
Buffer.to_bytes t.buf
|
|
||||||
in
|
|
||||||
Mutex.unlock t.mutex;
|
|
||||||
Unix.sendto t.sock_fd dgram 0 (Bytes.length dgram) [] t.dest |> ignore
|
|
11
lib/xlog.ml
11
lib/xlog.ml
|
@ -143,17 +143,6 @@ let init_pretty_writer
|
||||||
Pretty.writer |>
|
Pretty.writer |>
|
||||||
add_writer ?min_level
|
add_writer ?min_level
|
||||||
|
|
||||||
let init_journald_writer
|
|
||||||
?min_level
|
|
||||||
?path
|
|
||||||
()
|
|
||||||
=
|
|
||||||
Journald.make () ?path |>
|
|
||||||
Journald.writer |>
|
|
||||||
add_writer ?min_level
|
|
||||||
|
|
||||||
let should_upgrade_to_journald = Journald.should_upgrade
|
|
||||||
|
|
||||||
let dump_tree () =
|
let dump_tree () =
|
||||||
let rec dt (parent : logger) (child : logger) (ts : string) : unit =
|
let rec dt (parent : logger) (child : logger) (ts : string) : unit =
|
||||||
Printf.eprintf "%slogger: %s\n%!" ts child.namespace;
|
Printf.eprintf "%slogger: %s\n%!" ts child.namespace;
|
||||||
|
|
19
lib/xlog.mli
19
lib/xlog.mli
|
@ -41,9 +41,18 @@ val init_pretty_writer :
|
||||||
?backtrace:bool ->
|
?backtrace:bool ->
|
||||||
out_channel -> unit
|
out_channel -> unit
|
||||||
|
|
||||||
val init_journald_writer :
|
type writer =
|
||||||
?min_level:level ->
|
ts:Time.t ->
|
||||||
?path:string ->
|
ns:string ->
|
||||||
unit -> unit
|
filename:string ->
|
||||||
|
lineno:int ->
|
||||||
|
func:string ->
|
||||||
|
errno:int ->
|
||||||
|
exn:((exn * Printexc.raw_backtrace) option) ->
|
||||||
|
lvl:level ->
|
||||||
|
string ->
|
||||||
|
unit
|
||||||
|
|
||||||
val should_upgrade_to_journald : unit -> bool
|
val add_writer :
|
||||||
|
?min_level:level ->
|
||||||
|
writer -> unit
|
||||||
|
|
|
@ -24,7 +24,7 @@ buildDunePackage rec {
|
||||||
minimalOCamlVersion = "5.1";
|
minimalOCamlVersion = "5.1";
|
||||||
dontStrip = true;
|
dontStrip = true;
|
||||||
|
|
||||||
buildInputs = [ ppx_unicode ];
|
buildInputs = [ ppx_unicode ] ++ lib.optionals enableShell [ utop ];
|
||||||
propagatedBuildInputs = [ ptime ppxlib ];
|
propagatedBuildInputs = [ ptime ppxlib ];
|
||||||
nativeBuildInputs = [ ppxlib ppx_unicode ] ++ lib.optionals enableShell [
|
nativeBuildInputs = [ ppxlib ppx_unicode ] ++ lib.optionals enableShell [
|
||||||
ocaml dune_3 odoc utop
|
ocaml dune_3 odoc utop
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# This file is generated by dune, edit dune-project instead
|
# This file is generated by dune, edit dune-project instead
|
||||||
opam-version: "2.0"
|
opam-version: "2.0"
|
||||||
version: "0.0.2"
|
version: "0.1.0"
|
||||||
synopsis: "logging library"
|
synopsis: "logging library"
|
||||||
description: "logging library for cats written in ocaml"
|
description: "logging library for cats written in ocaml"
|
||||||
maintainer: ["xenia <xenia@awoo.systems>"]
|
maintainer: ["xenia <xenia@awoo.systems>"]
|
||||||
|
|
Loading…
Reference in New Issue