packages feed

logging-facade-journald (empty) → 0.0.0

raw patch · 7 files changed

+165/−0 lines, 7 filesdep +basedep +hspecdep +libsystemd-journalsetup-changed

Dependencies added: base, hspec, libsystemd-journal, logging-facade, text, unordered-containers

Files

+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2014 Zalora South East Asia Pte Ltd++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ logging-facade-journald.cabal view
@@ -0,0 +1,48 @@+name:             logging-facade-journald+version:          0.0.0+description:      Journald back-end for logging-facade+synopsis:         Journald back-end for logging-facade+license:          MIT+license-file:     LICENSE+copyright:        (c) 2014 Zalora South East Asia Pte Ltd+author:           Sönke Hahn <SoenkeHahn@gmail.com>+maintainer:       Sönke Hahn <SoenkeHahn@gmail.com>+build-type:       Simple+cabal-version:    >= 1.10+category:         System++source-repository head+  type: git+  location: https://github.com/zalora/logging-facade-journald++library+  ghc-options: -Wall+  hs-source-dirs: src+  exposed-modules:+      System.Logging.Facade.Journald+  other-modules:+      System.Logging.Facade.Journald.Internal+  build-depends:+      base == 4.*+    , libsystemd-journal+    , logging-facade+    , unordered-containers+    , text+  default-language: Haskell2010++test-suite spec+  type: exitcode-stdio-1.0+  ghc-options: -Wall+  hs-source-dirs: test, src+  main-is: Spec.hs+  other-modules:+      System.Logging.Facade.Journald.InternalSpec+  build-depends:+      base == 4.*+    , logging-facade+    , libsystemd-journal+    , unordered-containers+    , text++    , hspec == 2.*+  default-language: Haskell2010
+ src/System/Logging/Facade/Journald.hs view
@@ -0,0 +1,16 @@++-- | Implements a journald back-end for+-- <https://hackage.haskell.org/package/logging-facade logging-facade>.++module System.Logging.Facade.Journald where+++import           System.Logging.Facade.Sink+import           Systemd.Journal++import           System.Logging.Facade.Journald.Internal+++-- | Use this with `setLogSink` to switch logging to journald.+journaldLogSink :: LogSink+journaldLogSink = sendJournalFields . logRecordToJournalFields
+ src/System/Logging/Facade/Journald/Internal.hs view
@@ -0,0 +1,35 @@+{-# LANGUAGE OverloadedStrings #-}++module System.Logging.Facade.Journald.Internal where+++import           Data.HashMap.Strict+import           Data.Monoid+import           Data.String+import qualified Data.Text.Encoding          as Text+import           System.Logging.Facade.Types+import           Systemd.Journal+++logRecordToJournalFields :: LogRecord -> JournalFields+logRecordToJournalFields record =+  locationFields <>+  priority (logLevelToPriority (logRecordLevel record)) <>+  message (fromString (logRecordMessage record))+ where+  locationFields =+    fromList $ maybe [] toLocationFields (logRecordLocation record)+  toLocationFields loc =+    ("CODE_FILE", encodeUtf8 (locationFile loc)) :+    ("CODE_LINE", fromString (show (locationLine loc))) :+    []++  encodeUtf8 = Text.encodeUtf8 . fromString++logLevelToPriority :: LogLevel -> Priority+logLevelToPriority l = case l of+  TRACE -> Debug+  DEBUG -> Debug+  INFO -> Info+  WARN -> Warning+  ERROR -> Error
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ test/System/Logging/Facade/Journald/InternalSpec.hs view
@@ -0,0 +1,43 @@+{-# LANGUAGE OverloadedStrings #-}++module System.Logging.Facade.Journald.InternalSpec where+++import           Data.HashMap.Strict+import           Data.Monoid+import           Test.Hspec++import           System.Logging.Facade.Journald.Internal+import           System.Logging.Facade.Types+++spec :: Spec+spec = do+  describe "logRecordToJournalFields" $ do+    let record = LogRecord {+          logRecordLevel = ERROR,+          logRecordLocation = Nothing,+          logRecordMessage = "foo"+        }+        expected = fromList $+          ("MESSAGE", "foo") :+          ("PRIORITY", "3") :+          []+    it "maps LogRecords to JournalFields" $ do+      logRecordToJournalFields record `shouldBe` expected++    context "when LogRecord has a location" $ do+      let location = Location {+            locationPackage = "foo",+            locationModule = "Main",+            locationFile = "./foo/Main.hs",+            locationLine = 42,+            locationColumn = 23+          }+          locationFields = fromList $+            ("CODE_FILE", "./foo/Main.hs") :+            ("CODE_LINE", "42") :+            []+      it "includes the location as user journal fields" $ do+        logRecordToJournalFields record{logRecordLocation = Just location} `shouldBe`+          expected <> locationFields