From 8c69c28e5ab5ef5a5ed64411e1213e504a6a309e Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 21 Dec 2013 15:57:22 -0500 Subject: [PATCH] Attempt to implement syslog logging; fail at least with metalog. --- src/Logging.hs | 26 ++++++++++++++++---------- src/Main.hs | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Logging.hs b/src/Logging.hs index db7260a..94f4f7a 100644 --- a/src/Logging.hs +++ b/src/Logging.hs @@ -6,7 +6,11 @@ module Logging ( log_warning ) where -import System.Log.Handler.Simple ( GenericHandler ) +import System.Log.Handler.Simple ( fileHandler ) +import System.Log.Handler.Syslog ( + Facility ( USER ), + Option ( PID ), + openlog ) import System.Log.Logger ( Priority ( DEBUG, INFO ), debugM, @@ -30,12 +34,14 @@ log_info = infoM rootLoggerName log_warning :: String -> IO () log_warning = warningM rootLoggerName -init_logging :: Bool -> IO () -init_logging use_syslog = do - let max_level = if use_syslog then INFO else DEBUG - -- We need to specify the type here; otherwise, setHandlers won't - -- accept the empty list as an instance of [LogHandler a]. - let no_handlers = [] :: [GenericHandler a] - -- Removes the default "echo to stdout" handler. - updateGlobalLogger rootLoggerName (setLevel max_level - . setHandlers no_handlers) +init_logging :: FilePath -> Priority -> Bool -> IO () +init_logging log_file log_level syslog + | syslog == True = do + handler <- openlog rootLoggerName [PID] USER level + updateGlobalLogger rootLoggerName (setLevel level . setHandlers [handler]) + | otherwise = do + handler <- fileHandler log_file level + updateGlobalLogger rootLoggerName (setLevel level . setHandlers [handler]) + where + min_level = if syslog then INFO else DEBUG + level = if log_level < min_level then min_level else log_level diff --git a/src/Main.hs b/src/Main.hs index f2febcb..0182e38 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -247,7 +247,7 @@ main = do -- logging before the missing parameter checks below so that we can -- log the errors. let cfg = (def :: Configuration) `merge_optional` opt_config - init_logging (syslog cfg) + init_logging (log_file cfg) (log_level cfg) (syslog cfg) -- Check the optional config for missing required options. This is -- necessary because if the user specifies an empty list of -- 2.43.2