katip-scalyr-scribe (empty) → 0.1.0.0
raw patch · 7 files changed
+181/−0 lines, 7 filesdep +aesondep +basedep +katipsetup-changed
Dependencies added: aeson, base, katip, katip-scalyr-scribe, scientific, text, unordered-containers
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- katip-scalyr-scribe.cabal +64/−0
- src/Katip/Scribes/Scalyr.hs +79/−0
- test/Spec.hs +2/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for katip-json-scribe++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# katip-json-scribe
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ katip-scalyr-scribe.cabal view
@@ -0,0 +1,64 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: 3118765cd5574fb865a84b40e095a9b9c9ff2cdf4c656047fcf86f6b1fabf3a0++name: katip-scalyr-scribe+version: 0.1.0.0+synopsis: A katip scribe for logging to json+description: Please see the README on Github at <https://github.com/reactormonk/katip-scalyr-scribe#readme>+category: Logging+homepage: https://github.com/reactormonk/katip-scalyr-scribe#readme+bug-reports: https://github.com/reactormonk/katip-scalyr-scribe/issues+author: Simon Hafner+maintainer: hafnersimon@gmail.com+copyright: 2018 Simon Hafner+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ ChangeLog.md+ README.md++source-repository head+ type: git+ location: https://github.com/reactormonk/katip-scalyr-scribe++library+ hs-source-dirs:+ src+ default-extensions: OverloadedStrings+ build-depends:+ aeson+ , base >=4.7 && <5+ , katip+ , scientific+ , text+ , unordered-containers+ exposed-modules:+ Katip.Scribes.Scalyr+ other-modules:+ Paths_katip_scalyr_scribe+ default-language: Haskell2010++test-suite katip-json-scribe-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs:+ test+ default-extensions: OverloadedStrings+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ aeson+ , base >=4.7 && <5+ , katip+ , katip-scalyr-scribe+ , scientific+ , text+ , unordered-containers+ other-modules:+ Paths_katip_scalyr_scribe+ default-language: Haskell2010
+ src/Katip/Scribes/Scalyr.hs view
@@ -0,0 +1,79 @@+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE TupleSections #-}+module Katip.Scribes.Scalyr where++-------------------------------------------------------------------------------+import Control.Applicative as A+import Control.Concurrent+import Control.Exception (bracket_, finally)+import Control.Monad+import Data.Aeson+import Data.Aeson.Text+import qualified Data.HashMap.Strict as HM+import Data.Maybe (maybeToList)+import Data.Monoid+import Data.Scientific as S+import Data.Text (Text, pack)+import Data.Text.Internal.Builder+import qualified Data.Text.Lazy as LT+import Data.Text.Lazy.IO as T+import System.IO+-------------------------------------------------------------------------------+import Katip.Core+import Katip.Format.Time (formatAsLogTime)+-------------------------------------------------------------------------------+++-------------------------------------------------------------------------------+-- | Logs to a file handle such as stdout, stderr, or a file. Contexts+-- and other information will be flattened out into bracketed+-- fields. The flattening converts field names to uppercase, e.g.+--+-- > {"foo": {"bar": 42}}+--+-- is parsed as+--+-- > fooBar = 42+--+-- Naturally, collisions between flattened fields and actual values may+-- happen. There is currently no mitigation, and I'm not sure what Scalyr+-- does in that case. Keep your json snake_case only and you should be+-- golden.+--+-- Returns the newly-created `Scribe`. The finalizer flushes the+-- handle. Handle mode is set to 'LineBuffering' automatically.+mkScalyrScribe :: Handle -> Severity -> Verbosity -> IO Scribe+mkScalyrScribe h sev verb = do+ hSetBuffering h LineBuffering+ lock <- newMVar ()+ let logger i@Item{..} =+ when (permitItem sev i) $ bracket_ (takeMVar lock) (putMVar lock ()) $+ T.hPutStrLn h $ encodeToLazyText $ Object $ formatItem verb i+ pure $ Scribe logger (hFlush h)++-------------------------------------------------------------------------------+formatItem :: LogItem a => Verbosity -> Item a -> HM.HashMap Text Value+formatItem verb item@Item{..} =+ HM.fromList $ [+ ("timestamp", String $ formatAsLogTime _itemTime)+ , ("namespace", String $ mconcat $ intercalateNs _itemNamespace)+ , ("applicationName", String $ mconcat $ unNamespace _itemApp)+ , ("environment", String $ getEnvironment _itemEnv)+ , ("severity", String $ renderSeverity _itemSeverity)+ , ("hostname", String $ pack _itemHost)+ , ("processId", String $ pack $ show _itemProcess)+ , ("threadId", String $ getThreadIdText _itemThread)+ , ("payload", itemJson verb item)+ , ("message", String $ LT.toStrict $ toLazyText $ unLogStr _itemMessage)+ ] <>+ maybeToList (fmap (("sourceLocation",) . String . pack . locationToString) _itemLoc)+++-------------------------------------------------------------------------------+-- | Creates a scribe for scalyr. Pass the application name, and the environment,+-- e.g. staging or production.+scalyrLogEnv :: Text -> Environment -> Severity -> Verbosity -> IO LogEnv+scalyrLogEnv appName env sev verb = do+ le <- initLogEnv (Namespace [appName]) env+ lh <- mkScalyrScribe stdout sev verb+ registerScribe "scalyr" lh defaultScribeSettings le
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"