packages feed

co-log-simple (empty) → 1.0.0

raw patch · 10 files changed

+397/−0 lines, 10 filesdep +ansi-terminaldep +basedep +co-log

Dependencies added: ansi-terminal, base, co-log, co-log-core, co-log-simple, mtl, text

Files

+ .gitignore view
@@ -0,0 +1,7 @@+# stack uses this directory for build artifacts+/.stack-work/++stack.yaml.lock++# Made by 'hasktags --ctags .'+tags
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+1.0.0 (2025-08-07)++  * Initial release
+ LICENSE view
@@ -0,0 +1,15 @@+Copyright (c) 2025, Dino Morelli <dino@ui3.info>++Permission to use, copy, modify, and/or distribute this software+for any purpose with or without fee is hereby granted, provided+that the above copyright notice and this permission notice appear+in all copies.++THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL+WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE+AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL+DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA+OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER+TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR+PERFORMANCE OF THIS SOFTWARE.
+ README.md view
@@ -0,0 +1,41 @@+# co-log-simple+++## Synopsis++Simple enhancements for logging with co-log+++## Description++A library built upon co-log that makes constructing and using `LogAction`s+simpler with some predefined formatters and a slightly enhanced Severity type++This library expresses some logging needs that recur in my work:++1. Often I want more than one type of "info" severity to differentiate between+   more- or less-chatty output that's not debug or warning/error related. This+   library includes a new Severity type that adds Notice between Info and+   Warning.+2. I've often wanted different colors for severities which is much easier with+   co-log than hslogger+3. This library also includes a variety of formatters that should prove useful+   for various types of projects++For usage examples, see `src/examples/Main.hs` and the API docs for+`Colog.Simple`+++## Development++Source code is available from Codeberg at the+[co-log-simple](https://codeberg.org/dinofp/co-log-simple) project page.++Generate Haddock docs during development if using stack:++    $ stack haddock --haddock --no-haddock-deps+++## Contact++Dino Morelli <dino@ui3.info>
+ co-log-simple.cabal view
@@ -0,0 +1,84 @@+cabal-version: 2.2++name: co-log-simple+version: 1.0.0+synopsis: Simple enhancements for logging with co-log+description: A library built upon co-log that makes constructing and using+  `LogAction`s simpler with some predefined formatters and a slightly enhanced+  Severity type+author: Dino Morelli+maintainer: dino@ui3.info+copyright: 2025 Dino Morelli+category: Unclassified+license: ISC+license-file: LICENSE+build-type: Simple+extra-source-files:+  .gitignore+  README.md+  stack.yaml+extra-doc-files:+  CHANGELOG.md++source-repository head+  type: git+  location: https://codeberg.org/dinofp/co-log-simple++common lang+  default-language: Haskell2010+  default-extensions:+    BangPatterns+    DeriveFoldable+    DeriveFunctor+    DeriveGeneric+    DeriveLift+    DeriveTraversable+    EmptyCase+    FlexibleContexts+    FlexibleInstances+    GeneralizedNewtypeDeriving+    ImportQualifiedPost+    InstanceSigs+    KindSignatures+    LambdaCase+    MultiParamTypeClasses+    MultiWayIf+    NamedFieldPuns+    NumericUnderscores+    OverloadedStrings+    ScopedTypeVariables+    StandaloneDeriving+    TupleSections+  ghc-options:+    -fwarn-tabs+    -Wall+    -Wcompat+    -Wincomplete-record-updates+    -Wincomplete-uni-patterns+    -Wpartial-fields+    -Wredundant-constraints+  build-depends:+      base >=3 && <5+    , co-log >=0.6.0.2 && <0.7++library+  import: lang+  exposed-modules:+      Colog.Simple+      Colog.Simple.Message+      Colog.Simple.Severity+  hs-source-dirs:+    src/lib+  build-depends:+      ansi-terminal >=1.0.2 && <2+    , co-log-core >=0.3.2.1 && <0.4+    , mtl >=2.3.1 && <3+    , text >=2.0.2 && <2.2++executable co-log-simple+  import: lang+  main-is: Main.hs+  hs-source-dirs:+    src/examples+  build-depends:+      co-log-simple
+ src/examples/Main.hs view
@@ -0,0 +1,29 @@+module Main+  ( main+  )+  where++import Colog.Monad (usingLoggerT)+import Colog.Simple (LogAction, Message, Severity (Debug, Notice), basicFmt,+  colorFmt, colorSevFmt, logger, logInfo, logNotice, logTest, logTextStderr,+  logTextStdout)+++main :: IO ()+main = do+  usingLoggerT (logger colorSevFmt logTextStdout Debug) $ do+    logNotice "Examples of all Severity values\n"+    logTest++  usingLoggerT (logger colorFmt logTextStderr Notice) $ do+    logNotice "\nExample of a different formatter with Severity set to Notice"+    logNotice "These are going to stderr\n"+    logTest++  usingLoggerT (logger basicFmt logTextStdout Debug) $ do+    logNotice "\nNo colors is possible of course\n"+    logTest++  -- Example of completely turning off logging+  usingLoggerT (mempty :: LogAction IO Message) $+    logInfo "This message should never be shown"
+ src/lib/Colog/Simple.hs view
@@ -0,0 +1,71 @@+{-|+This module is for creating 'LogAction' instances that use the logging and+formatting functions in this library.++@+import Colog.Monad (usingLoggerT)+import Colog.Simple (Severity (..), colorSevFmt, logger, logNotice, logTextStdout)++usingLoggerT (logger colorSevFmt logTextStdout Debug) $ logNotice "Take note of this!"+@++The 'LogAction' produced by 'logger' above is what you would put in your+'ReaderT' environment as well. See co-log documentation for more info on+integrating with transformers.++<https://github.com/co-log/co-log/blob/main/tutorials/custom/Custom.md>+-}+module Colog.Simple+  (+    -- * Types+    Target++    -- * Constructing LogAction loggers+  , logger++    -- * Utility functions+  , logTest++    -- * Re-exported from co-log-simple+  , module Colog.Simple.Message+  , module Colog.Simple.Severity++    -- * Re-exported from co-log+  , HasLog (..)+  , LogAction+  , logTextStderr, logTextStdout+  )+  where++import Colog (LogAction)+import Colog.Actions (logTextStderr, logTextStdout)+import Colog.Message (formatWith)+import Colog.Monad (HasLog (..), WithLog)+import Control.Monad.Trans (MonadIO)+import Data.Text (Text)++import Colog.Simple.Message+import Colog.Simple.Severity+++-- | A synonym for ordinary co-log 'LogAction' types parameterized with 'Text'+type Target m = LogAction m Text+++-- | Construct a logging action+logger :: MonadIO m+  => Formatter  -- ^ Format function+  -> Target m   -- ^ Where log message output will go+  -> Severity   -- ^ The severity level to filter at or above+  -> LogAction m Message+logger formatter target sev = setSeverity sev . formatWith formatter $ target+++-- | Test function to generate messages with every severity in this library+logTest :: WithLog env Message m => m ()+logTest = do+  logDebug "log test message Debug 1 of 5"+  logInfo "log test message Info 2 of 5"+  logNotice "log test message Notice 3 of 5"+  logWarning "log test message Warning 4 of 5"+  logError "log test message Error 5 of 5"
+ src/lib/Colog/Simple/Message.hs view
@@ -0,0 +1,118 @@+{-|+The workhorse module of this library. Here you will find types for working with+this logging library along with functions for issuing log messages of various+severities. Also in this module are a small set of basic log output formatters.+-}+module Colog.Simple.Message+  (+    -- * Types+    Formatter+  , Message++    -- * Functions for logging with a specific severity+  , logDebug, logInfo, logNotice, logWarning, logError++    -- * Message formatting functions+  , basicFmt, colorFmt, colorSevFmt, colorSevStackFmt++    -- * Other functions+  , colorize+  , setSeverity++    -- * Re-exported from ansi-terminal+  , Color (..)++    -- * Re-exported from co-log+  , Msg (..)+  )+  where++import Colog (LogAction, cfilter)+import Colog.Message (Msg (..), log, showSourceLoc)+import Colog.Monad (WithLog)+import Data.Text (Text, pack)+import GHC.Stack (withFrozenCallStack)+import Prelude hiding (log)+import System.Console.ANSI (Color (..), ColorIntensity (Vivid),+  ConsoleLayer (Foreground), SGR (..), setSGRCode)++import Colog.Simple.Severity (Severity (..))+++-- | The type of a 'Msg' parameterized with the custom severity type in this+--   library+type Message = Msg Severity++-- | The type of functions which format 'Message's into 'Text'+type Formatter = Message -> Text+++-- | Wrap some 'Text' in the ANSI codes to color it in a terminal+colorize :: Color -> Text -> Text+colorize c txt =+  pack (setSGRCode [SetColor Foreground Vivid c])+  <> txt+  <> pack (setSGRCode [Reset])+++showSeverity :: Severity -> Text+showSeverity Debug    = colorize Green  "[Debug]   "+showSeverity Info     = colorize Blue   "[Info]    "+showSeverity Notice   = colorize White  "[Notice]  "+showSeverity Warning  = colorize Yellow "[Warning] "+showSeverity Error    = colorize Red    "[Error]   "+++-- | Log a message with 'Debug' severity+logDebug :: WithLog env Message m => Text -> m ()+logDebug = withFrozenCallStack (log Debug)+++-- | Log a message with 'Info' severity+logInfo :: WithLog env Message m => Text -> m ()+logInfo = withFrozenCallStack (log Info)+++-- | Log a message with 'Notice' severity+logNotice :: WithLog env Message m => Text -> m ()+logNotice = withFrozenCallStack (log Notice)+++-- | Log a message with 'Warning' severity+logWarning :: WithLog env Message m => Text -> m ()+logWarning = withFrozenCallStack (log Warning)+++-- | Log a message with 'Error' severity+logError :: WithLog env Message m => Text -> m ()+logError = withFrozenCallStack (log Error)+++-- | No color formatting. Just the message, no severity label+basicFmt :: Formatter+basicFmt (Msg _ _ txt) = txt+++-- | Format with no severity labels but message colors for each severity+colorFmt :: Formatter+colorFmt (Msg Debug _ txt) = colorize Green txt+colorFmt (Msg Info _ txt) = colorize Blue txt+colorFmt (Msg Notice _ txt) = colorize White txt+colorFmt (Msg Warning _ txt) = colorize Yellow txt+colorFmt (Msg Error _ txt) = colorize Red txt+++-- | Format with colored severity labels and no color for the message+colorSevFmt :: Formatter+colorSevFmt (Msg sev _ txt) = showSeverity sev <> txt+++-- | Format with colored severity labels, call stack info, and no color for the+--   message+colorSevStackFmt :: Formatter+colorSevStackFmt (Msg sev cs txt) = showSeverity sev <> showSourceLoc cs <> txt+++-- | Filter log messages for greater than or equal to the given 'Severity'+setSeverity :: Applicative m => Severity -> LogAction m Message -> LogAction m Message+setSeverity targetSev = cfilter (\(Msg msgSev _ _) -> msgSev >= targetSev)
+ src/lib/Colog/Simple/Severity.hs view
@@ -0,0 +1,25 @@+{-|+Custom severity type very much like the one in co-log-core but with an added+severity of 'Notice' between 'Info' and 'Warning'++The motivation is sometimes more- or less-chatty "normal" messages are useful+that can be filtered by a verbosity setting. This is distinct from debugging or+logging warnings/errors.++The code in this module is parameterized with this Severity type, not the+"stock" one in co-log/co-log-core.+-}+module Colog.Simple.Severity+  (+    -- * Types+    Severity (..)+  )+  where++import Data.Ix (Ix)+++-- | A custom severity type that includes an additional 'Notice' severity+--   between 'Info' and 'Warning'+data Severity = Debug | Info | Notice | Warning | Error+  deriving (Bounded, Enum, Eq, Ix, Ord, Read, Show)
+ stack.yaml view
@@ -0,0 +1,4 @@+snapshot: lts-22.6++packages:+- .