<commit_before>{-# LANGUAGE OverloadedStrings #-}

module ZoomHub.Log.Logger
  ( logDebug
  , logDebug_
  , logInfo
  , logInfo_
  , logWarning
  , logWarning_
  , logError
  , logError_
  ) where

import           Prelude              hiding (log)

import           Data.Aeson           (encode, object, (.=))
import           Data.Aeson.Types     (Pair)
import qualified Data.ByteString.Lazy as BL
import           Data.Text            (Text)
import           Data.Text.Encoding   (decodeUtf8)
import qualified Data.Text.IO         as TIO
import           Data.Time.Clock      (getCurrentTime)
import           System.IO            (stderr, stdout)

data Level = Debug | Info | Warning | Error deriving Eq

instance Show Level where
  show Debug = "debug"
  show Info = "info"
  show Warning = "warning"
  show Error = "error"

logDebug :: String -> [Pair] -> IO ()
logDebug = log Debug

logDebug_ :: String -> IO ()
logDebug_ = log_ Debug

logInfo :: String -> [Pair] -> IO ()
logInfo = log Info

logInfo_ :: String -> IO ()
logInfo_ = log_ Info

logWarning :: String -> [Pair] -> IO ()
logWarning = log Warning

logWarning_ :: String -> IO ()
logWarning_ = log_ Warning

logError :: String -> [Pair] -> IO ()
logError = log Error

logError_ :: String -> IO ()
logError_ = log_ Error

log_ :: Level -> String -> IO ()
log_ level message = log level message []

log :: Level -> String -> [Pair] -> IO ()
log level message meta = do
    now <- getCurrentTime
    TIO.hPutStrLn (handle level) $ decodeUtf8 . BL.toStrict . encode . object $
      [ "time" .= now
      , "type" .= ("app" :: Text)
      , "level" .= show level
      , "message" .= message
      ] ++ meta
  where
    handle Error = stderr
    handle _ = stdout<commit_msg>Add `logException` and `logException_` functions<commit_after>{-# LANGUAGE OverloadedStrings #-}

module ZoomHub.Log.Logger
  ( logDebug
  , logDebug_
  , logError
  , logError_
  , logException
  , logException_
  , logInfo
  , logInfo_
  , logWarning
  , logWarning_
  ) where

import           Prelude              hiding (log)

import           Control.Exception    (SomeException)
import           Data.Aeson           (encode, object, (.=))
import           Data.Aeson.Types     (Pair)
import qualified Data.ByteString.Lazy as BL
import           Data.Text            (Text)
import           Data.Text.Encoding   (decodeUtf8)
import qualified Data.Text.IO         as TIO
import           Data.Time.Clock      (getCurrentTime)
import           System.IO            (stderr, stdout)

data Level = Debug | Info | Warning | Error deriving Eq

instance Show Level where
  show Debug = "debug"
  show Info = "info"
  show Warning = "warning"
  show Error = "error"

logDebug :: String -> [Pair] -> IO ()
logDebug = log Debug

logDebug_ :: String -> IO ()
logDebug_ = log_ Debug

logInfo :: String -> [Pair] -> IO ()
logInfo = log Info

logInfo_ :: String -> IO ()
logInfo_ = log_ Info

logWarning :: String -> [Pair] -> IO ()
logWarning = log Warning

logWarning_ :: String -> IO ()
logWarning_ = log_ Warning

logError :: String -> [Pair] -> IO ()
logError = log Error

logError_ :: String -> IO ()
logError_ = log_ Error

logException :: String -> SomeException -> [Pair] -> IO ()
logException msg e meta = logError msg ( meta ++ [ "error" .= show e ])

logException_ :: String -> SomeException -> IO ()
logException_ msg e = logException msg e []

log_ :: Level -> String -> IO ()
log_ level message = log level message []

log :: Level -> String -> [Pair] -> IO ()
log level message meta = do
    now <- getCurrentTime
    TIO.hPutStrLn (handle level) $ decodeUtf8 . BL.toStrict . encode . object $
      [ "time" .= now
      , "type" .= ("app" :: Text)
      , "level" .= show level
      , "message" .= message
      ] ++ meta
  where
    handle Error = stderr
    handle _ = stdout
{'fraction_non_alphanumeric': 0.1199372876927097, 'fraction_numerical': 0.0010452051215050953, 'mean_word_length': 2.393617021276596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493029', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1168, 'n_words': 373}