Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithSeverity #14

Merged
merged 2 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` and `mapSeverity` 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
35 changes: 35 additions & 0 deletions src/Colog/Core/Severity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module Colog.Core.Severity
, pattern W
, pattern E
, filterBySeverity
, WithSeverity (..)
, mapSeverity
) where

import Data.Ix (Ix)
Expand Down Expand Up @@ -102,3 +104,36 @@ 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)

{- | Map the given function over the severity of a 'WithSeverity'.

This can be useful to operate generically over the severity, for example:
@
suppressErrors :: LogAction m (WithSeverity msg) -> LogAction m (WithSeverity msg)
suppressErrors = cmap (mapSeverity (\s -> if s == Error then Warning else s))
@
-}
mapSeverity :: (Severity -> Severity) -> WithSeverity msg -> WithSeverity msg
mapSeverity f (WithSeverity msg sev) = WithSeverity msg (f sev)