diff options
Diffstat (limited to 'src/Graylog/Gelf.hs')
-rw-r--r-- | src/Graylog/Gelf.hs | 24 |
1 files changed, 18 insertions, 6 deletions
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 |