logging-effect-colors (empty) → 0.1.0.0
raw patch · 5 files changed
+136/−0 lines, 5 filesdep +ansi-terminaldep +basedep +logging-effect
Dependencies added: ansi-terminal, base, logging-effect, prettyprinter, text
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- README.md +4/−0
- logging-effect-colors.cabal +32/−0
- src/Control/Monad/Log/Colors.hs +65/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for logging-effect-colors++## 0.1.0.0++* Provides `colorize`, `colorizeWith`, and `renderWithColor`.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2021, Obsidian Systems LLC++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 Obsidian Systems LLC 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,4 @@+# logging-effect-colors+[](https://haskell.org) [](https://hackage.haskell.org/package/logging-effect-colors) [](https://github.com/obsidiansystems/logging-effect-colors/actions) [](https://github.com/obsidiansystems/logging-effect-colors/blob/master/LICENSE)++Add color to your [logging-effect](https://hackage.haskell.org/package/logging-effect) log messages.
+ logging-effect-colors.cabal view
@@ -0,0 +1,32 @@+cabal-version: >=1.10+name: logging-effect-colors+version: 0.1.0.0+synopsis: Log messages in color+description: ANSI color coding for logging-effect log messages+homepage: https://github.com/obsidiansystems/logging-effect-colors+bug-reports:+ https://github.com/obsidiansystems/logging-effect-colors/issues++license: BSD3+license-file: LICENSE+author: Obsidian Systems LLC+maintainer: maintainer@obsidian.systems+copyright: 2021 Obsidian Systems LLC+category: System+build-type: Simple+extra-source-files:+ CHANGELOG.md+ README.md++library+ exposed-modules: Control.Monad.Log.Colors+ build-depends:+ ansi-terminal >=0.9 && <1.1+ , base >= 4.12 && < 5+ , logging-effect >=1.3 && <1.5+ , prettyprinter >=1.2 && <1.8+ , text >=1.2 && <2.1++ hs-source-dirs: src+ default-language: Haskell2010+ default-extensions: LambdaCase
+ src/Control/Monad/Log/Colors.hs view
@@ -0,0 +1,65 @@+{-# options_ghc -Wall #-}+module Control.Monad.Log.Colors where++import Control.Monad.Log+import Data.String+import System.Console.ANSI+import Data.Text.Prettyprint.Doc++-- | Apply 'SGR' codes to a string to modify its display attributes, resetting+-- SGR codes afterward.+wrapSGRCode :: (IsString a, Monoid a) => [SGR] -> a -> a+wrapSGRCode codes t = mconcat+ [ fromString $ setSGRCode codes+ , t+ , fromString $ setSGRCode [Reset]+ ]++-- | Mapping of 'Severity' levels to SGR styles+severitySgr :: Severity -> [SGR]+severitySgr = \case+ Emergency ->+ [ SetColor Background Vivid Red+ , SetColor Foreground Vivid Black+ , SetConsoleIntensity BoldIntensity+ ]+ Alert ->+ [ SetColor Foreground Vivid Red+ , SetConsoleIntensity BoldIntensity+ ]+ Critical ->+ [ SetColor Foreground Vivid Red+ , SetConsoleIntensity BoldIntensity+ ]+ Error ->+ [ SetColor Foreground Dull Red+ ]+ Warning ->+ [ SetColor Foreground Vivid Yellow+ ]+ Notice ->+ [ SetColor Foreground Vivid Blue+ ]+ Informational -> []+ Debug -> + [ SetColor Foreground Dull Green+ ]++-- | Color based on severity with a custom mapping of severity to SGR styles+colorizeWith+ :: (Monoid msg, IsString msg)+ => (Severity -> [SGR])+ -> WithSeverity msg+ -> WithSeverity msg+colorizeWith f (WithSeverity sev msg) =+ WithSeverity sev $ wrapSGRCode (f sev) msg++-- | Color based on severity with the default mapping of severity to SGR styles+colorize + :: (Monoid msg, IsString msg)+ => WithSeverity msg+ -> WithSeverity msg+colorize = colorizeWith severitySgr++renderWithColor :: (Monoid msg, IsString msg, Pretty msg) => WithSeverity msg -> Doc ann+renderWithColor = renderWithSeverity pretty . colorize