-- |
-- Module      : Amazonka.Logger
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
--
-- Types and functions for constructing loggers and emitting log messages.
module Amazonka.Logger
  ( -- * Constructing a Logger
    Logger,
    newLogger,

    -- * Levels
    LogLevel (..),
    logError,
    logInfo,
    logDebug,
    logTrace,

    -- * Building Messages
    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

-- | This is a primitive logger which can be used to log builds to a 'Handle'.
--
-- /Note:/ A more sophisticated logging library such as
-- <http://hackage.haskell.org/package/tinylog tinylog> or
-- <http://hackage.haskell.org/package/fast-logger fast-logger>
-- should be used in production code.
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