Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(callback): Change log callback log level to KafkaLogLevel #138

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/Kafka/Callbacks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,48 @@ where

import Kafka.Internal.RdKafka (rdKafkaConfSetErrorCb, rdKafkaConfSetLogCb, rdKafkaConfSetStatsCb)
import Kafka.Internal.Setup (HasKafkaConf(..), getRdKafkaConf)
import Kafka.Types (KafkaError(..))
import Kafka.Types (KafkaError(..), KafkaLogLevel(..))

-- | Add a callback for errors.
--
-- ==== __Examples__
--
-- Basic usage:
--
-- > setCallback (errorCallback myErrorCallback)
-- >
-- > myErrorCallback :: KafkaError -> String -> IO ()
-- > myErrorCallback kafkaError message = print $ show kafkaError <> "|" <> message
errorCallback :: HasKafkaConf k => (KafkaError -> String -> IO ()) -> k -> IO ()
errorCallback callback k =
let realCb _ err = callback (KafkaResponseError err)
in rdKafkaConfSetErrorCb (getRdKafkaConf k) realCb

logCallback :: HasKafkaConf k => (Int -> String -> String -> IO ()) -> k -> IO ()
-- | Add a callback for logs.
--
-- ==== __Examples__
--
-- Basic usage:
--
-- > setCallback (logCallback myLogCallback)
-- >
-- > myLogCallback :: KafkaLogLevel -> String -> String -> IO ()
-- > myLogCallback level facility message = print $ show level <> "|" <> facility <> "|" <> message
logCallback :: HasKafkaConf k => (KafkaLogLevel -> String -> String -> IO ()) -> k -> IO ()
logCallback callback k =
let realCb _ = callback
let realCb _ = callback . toEnum
in rdKafkaConfSetLogCb (getRdKafkaConf k) realCb

-- | Add a callback for stats.
--
-- ==== __Examples__
--
-- Basic usage:
--
-- > setCallback (statsCallback myStatsCallback)
-- >
-- > myStatsCallback :: String -> IO ()
-- > myStatsCallback stats = print $ show stats
statsCallback :: HasKafkaConf k => (String -> IO ()) -> k -> IO ()
statsCallback callback k =
let realCb _ = callback
Expand Down