diff options
author | Thomas Crevoisier <thomas.crevoisier@fretlink.com> | 2018-11-26 08:58:27 +0100 |
---|---|---|
committer | Thomas Crevoisier <thomas.crevoisier@fretlink.com> | 2018-11-26 16:13:26 +0100 |
commit | 8c9d965d06998aeb9474742c0923feb36a6fc636 (patch) | |
tree | 02ec7329968dc8a0e80bc3534000f78b090ec8a8 | |
parent | 19681eb84ad5224f8923b1853a98727b0a8cc77a (diff) | |
download | haskell-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.cabal | 5 | ||||
-rw-r--r-- | src/Graylog/Gelf.hs | 24 |
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 | ||
20 | library | 20 | library |
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 | ||
43 | test-suite test-state | 44 | test-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 |
7 | module Graylog.Gelf where | 8 | module Graylog.Gelf where |
8 | 9 | ||
9 | import Data.Aeson (ToJSON (..), Value (..), genericToJSON, | 10 | import Data.Aeson (ToJSON (..), Value (..), object, |
10 | toJSON) | 11 | toJSON, (.=)) |
11 | import Data.Aeson.Casing | 12 | import Data.HashMap.Strict (HashMap) |
12 | import Data.Text (Text) | 13 | import Data.Semigroup ((<>)) |
14 | import Data.Text (Text) | ||
13 | import Data.Time | 15 | import Data.Time |
14 | import Data.Typeable | 16 | import Data.Typeable |
17 | import GHC.Exts (toList) | ||
15 | import GHC.Generics | 18 | import GHC.Generics |
16 | 19 | ||
17 | data GELF | 20 | data 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 | ||
30 | instance ToJSON GELF where | 34 | instance 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 |
71 | simpleGelf host short = | 83 | simpleGelf host short = |
72 | GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing | 84 | GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing mempty |