katip-kafka (empty) → 0.0.1
raw patch · 6 files changed
+123/−0 lines, 6 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, hw-kafka-client, katip
Files
- CHANGELOG.md +3/−0
- LICENSE +30/−0
- README.md +11/−0
- Setup.hs +2/−0
- katip-kafka.cabal +30/−0
- src/Katip/Scribes/Kafka.hs +47/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+# 0.0.1+## Added+* First working version
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,11 @@+# How to use++``` haskell+let+ toRecord = simpleRecord "myapp.logs" UnassignedPartition+ props = brokersList [ BrokerAddress "kafka" ] <> compression Lz4+kafka <- kafkaScribe toRecord props DebugS V3 >>= either throwIO return+env <- initLogEnv "myapp" (Environment "devel") >>=+ registerScribe "kafka" kafka defaultScribeSettings+finally (runMyApp env) $ closeScribes env+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ katip-kafka.cabal view
@@ -0,0 +1,30 @@+name: katip-kafka+version: 0.0.1+synopsis: Katip scribe to send logs to Kafka+homepage: https://github.com/s9gf4ult/katip-kafka+license: BSD3+license-file: LICENSE+author: Aleksey Uimanov+maintainer: s9gf4ult@gmail.com+copyright: 2018 Aleksey Uimanov+category: Logging+build-type: Simple+extra-source-files: CHANGELOG.md+ , README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ ghc-options: -Wall+ default-extensions: LambdaCase+ exposed-modules: Katip.Scribes.Kafka+ build-depends: base >= 4.7 && < 5+ , aeson+ , bytestring+ , hw-kafka-client+ , katip+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/s9gf4ult/katip-kafka
+ src/Katip/Scribes/Kafka.hs view
@@ -0,0 +1,47 @@+module Katip.Scribes.Kafka where++import Control.Exception+import Control.Monad+import Data.Aeson+import Data.ByteString.Lazy+import Kafka.Producer+import Katip++-- | Created scribe uses hw-kafka-client library, which is binding+-- over librdkafka which in turn implements buffering. Beware to close+-- this scribe properly+kafkaScribe+ :: (Value -> ProducerRecord)+ -- ^ Kafka record creator+ -> ProducerProperties+ -- ^ Producer properties to create Kafka producer+ -> Severity+ -> Verbosity+ -> IO (Either KafkaError Scribe)+kafkaScribe itemToRecord pp sev verb = newProducer pp >>= \case+ Left e -> return $ Left e+ Right producer -> do+ let+ push item = when (permitItem sev item) $ do+ let+ obj = itemJson verb item+ record = itemToRecord obj+ produceMessage producer record >>= \case+ Nothing -> return ()+ Just err -> throwIO err+ fin = do+ flushProducer producer+ closeProducer producer+ return $ Right $ Scribe+ { liPush = push+ , scribeFinalizer = fin+ }++-- | Kafka record creator function. The key of resulting record is+-- 'Nothing' by default, you can override it if needed+simpleRecord :: TopicName -> ProducePartition -> Value -> ProducerRecord+simpleRecord topic part val = ProducerRecord+ { prTopic = topic+ , prPartition = part+ , prKey = Nothing+ , prValue = Just $ toStrict $ encode val }