aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-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