module Amazonka.Logger
(
Logger,
newLogger,
LogLevel (..),
logError,
logInfo,
logDebug,
logTrace,
ToLog (..),
buildLines,
)
where
import Amazonka.Data
import Amazonka.Prelude
import Amazonka.Types
import qualified Control.Monad as Monad
import qualified Data.ByteString.Builder as Build
import qualified System.IO as IO
newLogger :: MonadIO m => LogLevel -> IO.Handle -> m Logger
newLogger :: LogLevel -> Handle -> m Logger
newLogger LogLevel
x Handle
hd =
IO Logger -> m Logger
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Logger -> m Logger) -> IO Logger -> m Logger
forall a b. (a -> b) -> a -> b
$ do
Handle -> BufferMode -> IO ()
IO.hSetBuffering Handle
hd BufferMode
IO.LineBuffering
Logger -> IO Logger
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Logger -> IO Logger) -> Logger -> IO Logger
forall a b. (a -> b) -> a -> b
$ \LogLevel
y Builder
b ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Monad.when (LogLevel
x LogLevel -> LogLevel -> Bool
forall a. Ord a => a -> a -> Bool
>= LogLevel
y) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Handle -> Builder -> IO ()
Build.hPutBuilder Handle
hd (Builder
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"\n")
logError, logInfo, logDebug, logTrace :: (MonadIO m, ToLog a) => Logger -> a -> m ()
logError :: Logger -> a -> m ()
logError Logger
f = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logger
f LogLevel
Error (Builder -> IO ()) -> (a -> Builder) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall a. ToLog a => a -> Builder
build
logInfo :: Logger -> a -> m ()
logInfo Logger
f = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logger
f LogLevel
Info (Builder -> IO ()) -> (a -> Builder) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall a. ToLog a => a -> Builder
build
logDebug :: Logger -> a -> m ()
logDebug Logger
f = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logger
f LogLevel
Debug (Builder -> IO ()) -> (a -> Builder) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall a. ToLog a => a -> Builder
build
logTrace :: Logger -> a -> m ()
logTrace Logger
f = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logger
f LogLevel
Trace (Builder -> IO ()) -> (a -> Builder) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall a. ToLog a => a -> Builder
build