summaryrefslogtreecommitdiff
path: root/Com/MusicMeetsComics/Server/Logger.hs
blob: eb37ef60d0c82882e7e280fe2ab50b9a5651e059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Com.MusicMeetsComics.Server.Logger
  ( adapt
  , defaultLogEnv
  , logMsg
  , runKatipT
  , KatipT(..)
  , Katip(..)
  , LogEnv
  , Severity(..)
  ) where

import           Control.Monad.Logger
import qualified Control.Monad.Logger as Logger
import           Katip
import           Protolude
import qualified System.IO as IO
import qualified System.Log.FastLogger as FastLogger

defaultLogEnv :: IO LogEnv
defaultLogEnv = do
  handleScribe <- mkHandleScribe ColorIfTerminal IO.stdout DebugS V2
  env <- initLogEnv "hero" "production"
  registerScribe "stdout" handleScribe defaultScribeSettings env

fromLevel :: LogLevel -> Severity
fromLevel LevelDebug = DebugS
fromLevel LevelInfo = InfoS
fromLevel LevelWarn = WarningS
fromLevel LevelError = ErrorS
fromLevel (LevelOther _) = NoticeS

-- | Transforms Katip logMsg into monadLoggerLog to be used inside
-- MonadLogger monad
adapt ::
     (ToLogStr msg, Applicative m, Katip m)
  => (Namespace -> Severity -> Katip.LogStr -> m ())
  -> Loc
  -> LogSource
  -> LogLevel
  -> msg
  -> m ()
adapt f _ src lvl msg = f ns (fromLevel lvl) $ logStr' msg
  where
    ns = Namespace [src]
    -- not sure how fast this is going to be
    logStr' = Katip.logStr . FastLogger.fromLogStr . Logger.toLogStr