-- see http://docs.graylog.org/en/latest/pages/gelf.html
module Graylog.Gelf where
-import Data.Aeson (ToJSON (..), Value (..), object,
- toJSON, (.=))
+import Data.Aeson (ToJSON (..), Value (..), object, toJSON,
+ (.=))
import Data.HashMap.Strict (HashMap)
+import Data.Scientific (Scientific)
import Data.Semigroup ((<>))
import Data.Text (Text)
import Data.Time
, _gelfLevel :: Maybe SyslogLevel
, _gelfLine :: Maybe Word
, _gelfFile :: Maybe Text
- , _gelfMeta :: HashMap Text Text
+ , _gelfMeta :: HashMap Text MetaValue
}
deriving (Show, Typeable, Generic)
+data MetaValue
+ = TextValue Text
+ | NumberValue Scientific
+ deriving (Show)
+
+instance ToJSON MetaValue where
+ toJSON (TextValue txt) = toJSON txt
+ toJSON (NumberValue n) = toJSON n
+
+class ToMeta a where
+ toMeta :: a -> MetaValue
+
+instance ToMeta Text where
+ toMeta = TextValue
+
+instance ToMeta Scientific where
+ toMeta = NumberValue
+
+instance ToMeta Integer where
+ toMeta = NumberValue . fromInteger
+
+instance ToMeta Int where
+ toMeta = toMeta . toInteger
+
instance ToJSON GELF where
toJSON GELF{..} = object $ [ "version" .= _gelfVersion
, "host" .= _gelfHost
, "level" .= _gelfLevel
, "line" .= _gelfLine
, "file" .= _gelfFile
- ] <> toList (String <$> _gelfMeta)
+ ] <> toList (toJSON <$> _gelfMeta)
--