aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Crevoisier <thomas.crevoisier@fretlink.com>2018-11-30 17:01:18 +0100
committerThomas Crevoisier <thomas.crevoisier@fretlink.com>2018-11-30 17:04:41 +0100
commit4b6637800bf7813570cf7a628794a0351835711b (patch)
tree6f4bb1ef345f3db21c004bc11391da5a0c26767c
parent8c9d965d06998aeb9474742c0923feb36a6fc636 (diff)
downloadhaskell-graylog-support-additional-meta.tar.gz
haskell-graylog-support-additional-meta.tar.zst
haskell-graylog-support-additional-meta.zip
Allow sending numbers in additional fieldssupport-additional-meta
-rw-r--r--src/Graylog/Gelf.hs33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/Graylog/Gelf.hs b/src/Graylog/Gelf.hs
index b6e7ec0..8ffe24f 100644
--- a/src/Graylog/Gelf.hs
+++ b/src/Graylog/Gelf.hs
@@ -7,9 +7,10 @@
7-- see http://docs.graylog.org/en/latest/pages/gelf.html 7-- see http://docs.graylog.org/en/latest/pages/gelf.html
8module Graylog.Gelf where 8module Graylog.Gelf where
9 9
10import Data.Aeson (ToJSON (..), Value (..), object, 10import Data.Aeson (ToJSON (..), Value (..), object, toJSON,
11 toJSON, (.=)) 11 (.=))
12import Data.HashMap.Strict (HashMap) 12import Data.HashMap.Strict (HashMap)
13import Data.Scientific (Scientific)
13import Data.Semigroup ((<>)) 14import Data.Semigroup ((<>))
14import Data.Text (Text) 15import Data.Text (Text)
15import Data.Time 16import Data.Time
@@ -27,10 +28,34 @@ data GELF
27 , _gelfLevel :: Maybe SyslogLevel 28 , _gelfLevel :: Maybe SyslogLevel
28 , _gelfLine :: Maybe Word 29 , _gelfLine :: Maybe Word
29 , _gelfFile :: Maybe Text 30 , _gelfFile :: Maybe Text
30 , _gelfMeta :: HashMap Text Text 31 , _gelfMeta :: HashMap Text MetaValue
31 } 32 }
32 deriving (Show, Typeable, Generic) 33 deriving (Show, Typeable, Generic)
33 34
35data MetaValue
36 = TextValue Text
37 | NumberValue Scientific
38 deriving (Show)
39
40instance ToJSON MetaValue where
41 toJSON (TextValue txt) = toJSON txt
42 toJSON (NumberValue n) = toJSON n
43
44class ToMeta a where
45 toMeta :: a -> MetaValue
46
47instance ToMeta Text where
48 toMeta = TextValue
49
50instance ToMeta Scientific where
51 toMeta = NumberValue
52
53instance ToMeta Integer where
54 toMeta = NumberValue . fromInteger
55
56instance ToMeta Int where
57 toMeta = toMeta . toInteger
58
34instance ToJSON GELF where 59instance ToJSON GELF where
35 toJSON GELF{..} = object $ [ "version" .= _gelfVersion 60 toJSON GELF{..} = object $ [ "version" .= _gelfVersion
36 , "host" .= _gelfHost 61 , "host" .= _gelfHost
@@ -40,7 +65,7 @@ instance ToJSON GELF where
40 , "level" .= _gelfLevel 65 , "level" .= _gelfLevel
41 , "line" .= _gelfLine 66 , "line" .= _gelfLine
42 , "file" .= _gelfFile 67 , "file" .= _gelfFile
43 ] <> toList (String <$> _gelfMeta) 68 ] <> toList (toJSON <$> _gelfMeta)
44 69
45-- 70--
46 71