Skip to content

Commit 77a01a4

Browse files
authored
Add WithSeverity (#14)
* Add WithSeverity Fixes #13. * Add mapSeverity
1 parent 0195720 commit 77a01a4

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
`co-log-core` uses [PVP Versioning][1].
44
The change log is available [on GitHub][2].
55

6+
## Unreleased
7+
8+
* Added `WithSeverity` and `mapSeverity` to `Colog.Severity`.
9+
610
## 🎃 0.3.0.0 — Oct 29, 2021
711

812
* [#223](https://github.com/co-log/co-log/pull/223):

co-log-core.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ common common-options
6666

6767
default-language: Haskell2010
6868
default-extensions: ConstraintKinds
69+
DeriveFunctor
70+
DeriveTraversable
6971
DeriveGeneric
7072
DerivingStrategies
7173
GeneralizedNewtypeDeriving

src/Colog/Core/Severity.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module Colog.Core.Severity
3434
, pattern W
3535
, pattern E
3636
, filterBySeverity
37+
, WithSeverity (..)
38+
, mapSeverity
3739
) where
3840

3941
import Data.Ix (Ix)
@@ -102,3 +104,36 @@ filterBySeverity
102104
-> LogAction m a
103105
filterBySeverity s fs = cfilter (\a -> fs a >= s)
104106
{-# INLINE filterBySeverity #-}
107+
108+
-- Note: the order of the fields here is to allow the constructor to be used infix.
109+
{-| A message tagged with a 'Severity'.
110+
111+
It is common to want to log various types of messages tagged with a severity.
112+
'WithSeverity' provides a standard way to do so while allowing the messages to be processed independently of the severity.
113+
114+
It is easy to 'cmap' over a 'LogAction m (WithSeverity a)', or to filter based on the severity.
115+
116+
@
117+
logSomething :: LogAction m (WithSeverity String) -> m ()
118+
logSomething logger = logger <& "hello" `WithSeverity` Info
119+
120+
cmap' :: (b -> a) -> LogAction m (WithSeverity a) -> LogAction m (WithSeverity b)
121+
cmap' f action = cmap (fmap f) action
122+
123+
filterBySeverity' :: (Applicative m) => Severity -> LogAction m (WithSeverity a) -> LogAction m (WithSeverity a)
124+
filterBySeverity' threshold action = filterBySeverity threshold getSeverity action
125+
@
126+
-}
127+
data WithSeverity msg = WithSeverity { getMsg :: msg , getSeverity :: Severity }
128+
deriving stock (Show, Eq, Ord, Functor, Foldable, Traversable)
129+
130+
{- | Map the given function over the severity of a 'WithSeverity'.
131+
132+
This can be useful to operate generically over the severity, for example:
133+
@
134+
suppressErrors :: LogAction m (WithSeverity msg) -> LogAction m (WithSeverity msg)
135+
suppressErrors = cmap (mapSeverity (\s -> if s == Error then Warning else s))
136+
@
137+
-}
138+
mapSeverity :: (Severity -> Severity) -> WithSeverity msg -> WithSeverity msg
139+
mapSeverity f (WithSeverity msg sev) = WithSeverity msg (f sev)

0 commit comments

Comments
 (0)