From 526966434f2248c1200427677dc3882668714693 Mon Sep 17 00:00:00 2001 From: AndrewRademacher Date: Wed, 24 Feb 2016 16:52:14 -0600 Subject: Project init; added Gelf format. --- src/Graylog/Gelf.hs | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Graylog/Gelf.hs (limited to 'src') diff --git a/src/Graylog/Gelf.hs b/src/Graylog/Gelf.hs new file mode 100644 index 0000000..67e0de7 --- /dev/null +++ b/src/Graylog/Gelf.hs @@ -0,0 +1,76 @@ +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} + +module Graylog.Gelf where + +import Data.Aeson (ToJSON (..), Value (..), genericToJSON, + toJSON) +import Data.Aeson.Casing +import Data.Scientific +import Data.Text (Text) +import Data.Time +import Data.Typeable +import Data.Vector +import GHC.Generics + +data GELF + = GELF + { _gelfVersion :: Version + , _gelfHost :: Text + , _gelfShortMessage :: Text + , _gelfFullMessage :: Maybe Text + , _gelfTimestamp :: Maybe UTCTime + , _gelfLevel :: Maybe SyslogLevel + , _gelfLine :: Maybe Word + , _gelfFile :: Maybe Text + , _gelfAdditionalFields :: Vector Field + } + deriving (Show, Typeable, Generic) + +instance ToJSON GELF where + toJSON = genericToJSON $ aesonPrefix snakeCase + +-- + +data Version + = Version1x1 + deriving (Eq, Show, Typeable, Generic) + +instance ToJSON Version where + toJSON Version1x1 = String "1.1" + +-- + +data SyslogLevel + = Emergency + | Alert + | Critical + | Error + | Warning + | Notice + | Informational + | Debug + deriving (Eq, Ord, Show, Typeable, Generic) + +instance ToJSON SyslogLevel where + toJSON Emergency = Number 0 + toJSON Alert = Number 1 + toJSON Critical = Number 2 + toJSON Error = Number 3 + toJSON Warning = Number 4 + toJSON Notice = Number 5 + toJSON Informational = Number 6 + toJSON Debug = Number 7 + +-- + +data Field + = FieldString Text + | FieldNumber Scientific + deriving (Eq, Show, Typeable, Generic) + +instance ToJSON Field where + toJSON (FieldString s) = String s + toJSON (FieldNumber n) = Number n + -- cgit v1.2.3