aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Crevoisier <thomas.crevoisier@fretlink.com>2018-11-26 08:58:27 +0100
committerThomas Crevoisier <thomas.crevoisier@fretlink.com>2018-11-26 16:13:26 +0100
commit8c9d965d06998aeb9474742c0923feb36a6fc636 (patch)
tree02ec7329968dc8a0e80bc3534000f78b090ec8a8
parent19681eb84ad5224f8923b1853a98727b0a8cc77a (diff)
downloadhaskell-graylog-8c9d965d06998aeb9474742c0923feb36a6fc636.tar.gz
haskell-graylog-8c9d965d06998aeb9474742c0923feb36a6fc636.tar.zst
haskell-graylog-8c9d965d06998aeb9474742c0923feb36a6fc636.zip
Allow to send metadata in a Gelf message
-rw-r--r--graylog.cabal5
-rw-r--r--src/Graylog/Gelf.hs24
2 files changed, 21 insertions, 8 deletions
diff --git a/graylog.cabal b/graylog.cabal
index 5618d7c..dc84862 100644
--- a/graylog.cabal
+++ b/graylog.cabal
@@ -17,7 +17,7 @@ source-repository head
17 type: git 17 type: git
18 location: https://github.com/AndrewRademacher/haskell-graylog.git 18 location: https://github.com/AndrewRademacher/haskell-graylog.git
19 19
20library 20library
21 hs-source-dirs: src 21 hs-source-dirs: src
22 default-language: Haskell2010 22 default-language: Haskell2010
23 23
@@ -29,7 +29,7 @@ library
29 ghc-options: -Wall -rtsopts 29 ghc-options: -Wall -rtsopts
30 30
31 build-depends: base ==4.* 31 build-depends: base ==4.*
32 32
33 , aeson 33 , aeson
34 , aeson-casing 34 , aeson-casing
35 , bytestring 35 , bytestring
@@ -38,6 +38,7 @@ library
38 , scientific 38 , scientific
39 , text 39 , text
40 , time 40 , time
41 , unordered-containers
41 , vector 42 , vector
42 43
43test-suite test-state 44test-suite test-state
diff --git a/src/Graylog/Gelf.hs b/src/Graylog/Gelf.hs
index ee17e3d..b6e7ec0 100644
--- a/src/Graylog/Gelf.hs
+++ b/src/Graylog/Gelf.hs
@@ -1,17 +1,20 @@
1{-# LANGUAGE DeriveDataTypeable #-} 1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveGeneric #-} 2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE OverloadedStrings #-} 3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RecordWildCards #-}
4 5
5-- | Default formatting for Graylog messages, 6-- | Default formatting for Graylog messages,
6-- see http://docs.graylog.org/en/latest/pages/gelf.html 7-- see http://docs.graylog.org/en/latest/pages/gelf.html
7module Graylog.Gelf where 8module Graylog.Gelf where
8 9
9import Data.Aeson (ToJSON (..), Value (..), genericToJSON, 10import Data.Aeson (ToJSON (..), Value (..), object,
10 toJSON) 11 toJSON, (.=))
11import Data.Aeson.Casing 12import Data.HashMap.Strict (HashMap)
12import Data.Text (Text) 13import Data.Semigroup ((<>))
14import Data.Text (Text)
13import Data.Time 15import Data.Time
14import Data.Typeable 16import Data.Typeable
17import GHC.Exts (toList)
15import GHC.Generics 18import GHC.Generics
16 19
17data GELF 20data GELF
@@ -24,11 +27,20 @@ data GELF
24 , _gelfLevel :: Maybe SyslogLevel 27 , _gelfLevel :: Maybe SyslogLevel
25 , _gelfLine :: Maybe Word 28 , _gelfLine :: Maybe Word
26 , _gelfFile :: Maybe Text 29 , _gelfFile :: Maybe Text
30 , _gelfMeta :: HashMap Text Text
27 } 31 }
28 deriving (Show, Typeable, Generic) 32 deriving (Show, Typeable, Generic)
29 33
30instance ToJSON GELF where 34instance ToJSON GELF where
31 toJSON = genericToJSON $ aesonPrefix snakeCase 35 toJSON GELF{..} = object $ [ "version" .= _gelfVersion
36 , "host" .= _gelfHost
37 , "short_message" .= _gelfShortMessage
38 , "full_message" .= _gelfFullMessage
39 , "timestamp" .= _gelfTimestamp
40 , "level" .= _gelfLevel
41 , "line" .= _gelfLine
42 , "file" .= _gelfFile
43 ] <> toList (String <$> _gelfMeta)
32 44
33-- 45--
34 46
@@ -69,4 +81,4 @@ simpleGelf
69 -> Text -- ^ Short message 81 -> Text -- ^ Short message
70 -> GELF 82 -> GELF
71simpleGelf host short = 83simpleGelf host short =
72 GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing 84 GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing mempty