aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Crevoisier <crevoisier.thomas@gmail.com>2019-01-15 15:30:19 +0100
committerGitHub <noreply@github.com>2019-01-15 15:30:19 +0100
commita48cebd4e30ae25777d1934f83e348a1affa214c (patch)
tree6f4bb1ef345f3db21c004bc11391da5a0c26767c
parent0edc7f5650ba94ba36f83ed410a412368dd1e561 (diff)
parent4b6637800bf7813570cf7a628794a0351835711b (diff)
downloadhaskell-graylog-a48cebd4e30ae25777d1934f83e348a1affa214c.tar.gz
haskell-graylog-a48cebd4e30ae25777d1934f83e348a1affa214c.tar.zst
haskell-graylog-a48cebd4e30ae25777d1934f83e348a1affa214c.zip
Merge pull request #1 from fretlink/support-additional-metav2019-01-15
Support additional meta in GELF messages
-rw-r--r--graylog.cabal5
-rw-r--r--src/Graylog/Gelf.hs49
-rw-r--r--stack.yaml2
3 files changed, 47 insertions, 9 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..8ffe24f 100644
--- a/src/Graylog/Gelf.hs
+++ b/src/Graylog/Gelf.hs
@@ -1,17 +1,21 @@
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, toJSON,
10 toJSON) 11 (.=))
11import Data.Aeson.Casing 12import Data.HashMap.Strict (HashMap)
12import Data.Text (Text) 13import Data.Scientific (Scientific)
14import Data.Semigroup ((<>))
15import Data.Text (Text)
13import Data.Time 16import Data.Time
14import Data.Typeable 17import Data.Typeable
18import GHC.Exts (toList)
15import GHC.Generics 19import GHC.Generics
16 20
17data GELF 21data GELF
@@ -24,11 +28,44 @@ data GELF
24 , _gelfLevel :: Maybe SyslogLevel 28 , _gelfLevel :: Maybe SyslogLevel
25 , _gelfLine :: Maybe Word 29 , _gelfLine :: Maybe Word
26 , _gelfFile :: Maybe Text 30 , _gelfFile :: Maybe Text
31 , _gelfMeta :: HashMap Text MetaValue
27 } 32 }
28 deriving (Show, Typeable, Generic) 33 deriving (Show, Typeable, Generic)
29 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
30instance ToJSON GELF where 59instance ToJSON GELF where
31 toJSON = genericToJSON $ aesonPrefix snakeCase 60 toJSON GELF{..} = object $ [ "version" .= _gelfVersion
61 , "host" .= _gelfHost
62 , "short_message" .= _gelfShortMessage
63 , "full_message" .= _gelfFullMessage
64 , "timestamp" .= _gelfTimestamp
65 , "level" .= _gelfLevel
66 , "line" .= _gelfLine
67 , "file" .= _gelfFile
68 ] <> toList (toJSON <$> _gelfMeta)
32 69
33-- 70--
34 71
@@ -69,4 +106,4 @@ simpleGelf
69 -> Text -- ^ Short message 106 -> Text -- ^ Short message
70 -> GELF 107 -> GELF
71simpleGelf host short = 108simpleGelf host short =
72 GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing 109 GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing mempty
diff --git a/stack.yaml b/stack.yaml
index 55ab633..2ccd75c 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -2,7 +2,7 @@
2# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration/ 2# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration/
3 3
4# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2) 4# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
5resolver: lts-5.4 5resolver: lts-11.5
6 6
7# Local packages, usually specified by relative directory name 7# Local packages, usually specified by relative directory name
8packages: 8packages: