diff --git a/bin/main.ml b/bin/main.ml index 80514ba..2e09a8d 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -14,12 +14,11 @@ let no_timestamp = Option.is_some (Sys.getenv_opt "LOG_NO_TIMESTAMP") let no_namespace = Option.is_some (Sys.getenv_opt "LOG_NO_NAMESPACE") let () = - match Sys.getenv_opt "JOURNAL_STREAM" with - | Some _ -> + if Logging.should_upgrade_to_journald () then Logging.init_journald_writer () ~min_level - | None -> - Logging.init_pretty_writer stderr + else + Logging.init_pretty_writer stdout ~min_level ~color:(not no_color) ~timestamp:(not no_timestamp) diff --git a/lib/logging/journald.ml b/lib/logging/journald.ml index ba08f8e..574d679 100644 --- a/lib/logging/journald.ml +++ b/lib/logging/journald.ml @@ -9,6 +9,11 @@ type t = { 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; diff --git a/lib/logging/logging.ml b/lib/logging/logging.ml index 340923d..aed6b18 100644 --- a/lib/logging/logging.ml +++ b/lib/logging/logging.ml @@ -107,3 +107,5 @@ let init_journald_writer Journald.make () ?path |> Journald.writer |> add_writer ?min_level + +let should_upgrade_to_journald = Journald.should_upgrade diff --git a/lib/logging/logging.mli b/lib/logging/logging.mli index 3c71cb9..4993eed 100644 --- a/lib/logging/logging.mli +++ b/lib/logging/logging.mli @@ -36,3 +36,5 @@ val init_journald_writer : ?min_level:level -> ?path:string -> unit -> unit + +val should_upgrade_to_journald : unit -> bool