Skip to content

Commit

Permalink
Add WithSeverity
Browse files Browse the repository at this point in the history
Fixes co-log#13.
  • Loading branch information
michaelpj committed Jan 31, 2022
1 parent 0195720 commit ff6e510
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
`co-log-core` uses [PVP Versioning][1].
The change log is available [on GitHub][2].

## Unreleased

* Added `WithSeverity` to `Colog.Severity`.

## 🎃 0.3.0.0 — Oct 29, 2021

* [#223](https://github.com/co-log/co-log/pull/223):
Expand Down
2 changes: 2 additions & 0 deletions co-log-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ common common-options

default-language: Haskell2010
default-extensions: ConstraintKinds
DeriveFunctor
DeriveTraversable
DeriveGeneric
DerivingStrategies
GeneralizedNewtypeDeriving
Expand Down
23 changes: 23 additions & 0 deletions src/Colog/Core/Severity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Colog.Core.Severity
, pattern W
, pattern E
, filterBySeverity
, WithSeverity (..)
) where

import Data.Ix (Ix)
Expand Down Expand Up @@ -102,3 +103,25 @@ filterBySeverity
-> LogAction m a
filterBySeverity s fs = cfilter (\a -> fs a >= s)
{-# INLINE filterBySeverity #-}

-- Note: the order of the fields here is to allow the constructor to be used infix.
{-| A message tagged with a 'Severity'.
It is common to want to log various types of messages tagged with a severity.
'WithSeverity' provides a standard way to do so while allowing the messages to be processed independently of the severity.
It is easy to 'cmap' over a 'LogAction m (WithSeverity a)', or to filter based on the severity.
@
logSomething :: LogAction m (WithSeverity String) -> m ()
logSomething logger = logger <& "hello" `WithSeverity` Info
cmap' :: (b -> a) -> LogAction m (WithSeverity a) -> LogAction m (WithSeverity b)
cmap' f action = cmap (fmap f) action
filterBySeverity' :: (Applicative m) => Severity -> LogAction m (WithSeverity a) -> LogAction m (WithSeverity a)
filterBySeverity' threshold action = filterBySeverity threshold getSeverity action
@
-}
data WithSeverity msg = WithSeverity { getMsg :: msg , getSeverity :: Severity }
deriving stock (Show, Eq, Ord, Functor, Foldable, Traversable)

0 comments on commit ff6e510

Please sign in to comment.