]> git.immae.eu Git - github/fretlink/haskell-graylog.git/commitdiff
Allow to send metadata in a Gelf message
authorThomas Crevoisier <thomas.crevoisier@fretlink.com>
Mon, 26 Nov 2018 07:58:27 +0000 (08:58 +0100)
committerThomas Crevoisier <thomas.crevoisier@fretlink.com>
Mon, 26 Nov 2018 15:13:26 +0000 (16:13 +0100)
graylog.cabal
src/Graylog/Gelf.hs

index 5618d7c60e95d1c2661c6f7cbf0585d8848b4210..dc848626bc558a11065b8fa89fcd4d63de64991e 100644 (file)
@@ -17,7 +17,7 @@ source-repository head
     type:               git
     location:           https://github.com/AndrewRademacher/haskell-graylog.git
 
-library 
+library
    hs-source-dirs:      src
    default-language:    Haskell2010
 
@@ -29,7 +29,7 @@ library
    ghc-options:         -Wall -rtsopts
 
    build-depends:       base            ==4.*
-   
+
                      ,  aeson
                      ,  aeson-casing
                      ,  bytestring
@@ -38,6 +38,7 @@ library
                      ,  scientific
                      ,  text
                      ,  time
+                     ,  unordered-containers
                      ,  vector
 
 test-suite test-state
index ee17e3d347919b8f7deda25d5f362a745740504a..b6e7ec09297175c44ebf8a92ca17b1da3e05c7ea 100644 (file)
@@ -1,17 +1,20 @@
 {-# LANGUAGE DeriveDataTypeable #-}
 {-# LANGUAGE DeriveGeneric      #-}
 {-# LANGUAGE OverloadedStrings  #-}
+{-# LANGUAGE RecordWildCards    #-}
 
 -- | Default formatting for Graylog messages,
 -- see http://docs.graylog.org/en/latest/pages/gelf.html
 module Graylog.Gelf where
 
-import           Data.Aeson        (ToJSON (..), Value (..), genericToJSON,
-                                    toJSON)
-import           Data.Aeson.Casing
-import           Data.Text         (Text)
+import           Data.Aeson          (ToJSON (..), Value (..), object,
+                                      toJSON, (.=))
+import           Data.HashMap.Strict (HashMap)
+import           Data.Semigroup      ((<>))
+import           Data.Text           (Text)
 import           Data.Time
 import           Data.Typeable
+import           GHC.Exts            (toList)
 import           GHC.Generics
 
 data GELF
@@ -24,11 +27,20 @@ data GELF
       , _gelfLevel        :: Maybe SyslogLevel
       , _gelfLine         :: Maybe Word
       , _gelfFile         :: Maybe Text
+      , _gelfMeta         :: HashMap Text Text
       }
    deriving (Show, Typeable, Generic)
 
 instance ToJSON GELF where
-   toJSON = genericToJSON $ aesonPrefix snakeCase
+  toJSON GELF{..} = object $ [ "version"        .= _gelfVersion
+                             , "host"           .= _gelfHost
+                             , "short_message"  .= _gelfShortMessage
+                             , "full_message"   .= _gelfFullMessage
+                             , "timestamp"      .= _gelfTimestamp
+                             , "level"          .= _gelfLevel
+                             , "line"           .= _gelfLine
+                             , "file"           .= _gelfFile
+                             ] <> toList (String <$> _gelfMeta)
 
 --
 
@@ -69,4 +81,4 @@ simpleGelf
    -> Text     -- ^ Short message
    -> GELF
 simpleGelf host short =
-   GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing
+  GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing mempty