co-log: Composable Contravariant Comonadic Logging Library

[ comonad, contravariant, library, logging, mpl, program ] [ Propose Tags ]

The default implementation of logging based on co-log-core.

The ideas behind this package are described in the following blog post:


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
tutorial

Controls if tutorials get build (mainly to avoid building them on hackage).

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.0, 0.1.0, 0.2.0, 0.3.0.0, 0.4.0.0, 0.4.0.1, 0.5.0.0, 0.6.0.0, 0.6.0.1, 0.6.0.2, 0.6.1.0
Change log CHANGELOG.md
Dependencies ansi-terminal (>=1.0 && <1.2), base (>=4.14 && <4.20), bytestring (>=0.10.8 && <0.13), chronos (>=1.1 && <1.2), co-log, co-log-core (>=0.3 && <0.4), containers (>=0.5.7 && <0.8), contravariant (>=1.5 && <1.6), dependent-map (>=0.4 && <0.5), dependent-sum (>=0.7 && <0.8), directory (>=1.3.0 && <1.4), exceptions (>=0.8.3 && <0.11), filepath (>=1.4.1 && <1.6), mtl (>=2.2.2 && <2.4), run-st (<=0.1.3.0), text (>=1.2.3 && <2.2), transformers (>=0.5 && <0.7), unliftio-core (>=0.2 && <0.3), vector (>=0.12.0.3 && <0.14) [details]
License MPL-2.0
Copyright 2018-2022 Kowainik, 2023-2024 Co-Log
Author Dmitrii Kovanikov
Maintainer Kowainik <xrom.xkov@gmail.com>
Category Logging, Contravariant, Comonad
Home page https://github.com/co-log/co-log
Bug tracker https://github.com/co-log/co-log/issues
Source repo head: git clone https://github.com/co-log/co-log.git
Uploaded by alaendle at 2024-03-01T09:53:18Z
Distributions LTSHaskell:0.6.1.0, NixOS:0.6.0.2, Stackage:0.6.1.0
Reverse Dependencies 7 direct, 7 indirect [details]
Executables tutorial-custom, tutorial-loggert, tutorial-loggert-simple, tutorial-intro, readme, concurrent-playground, play-colog
Downloads 5288 total (67 in the last 30 days)
Rating 2.5 (votes: 3) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-03-01 [all 1 reports]

Readme for co-log-0.6.1.0

[back to package description]

co-log

GitHub CI MPL-2.0 license

co-log is a composable and configurable logging framework. It combines all the benefits of Haskell idioms to provide a reasonable and convenient interface. Although the library design uses some advanced concepts in its core, we are striving to provide beginner-friendly API. The library also provides the complete documentation with a lot of beginner-friendly examples, explanations and tutorials to guide users. The combination of a pragmatic approach to logging and fundamental Haskell abstractions allows us to create a highly composable and configurable logging framework.


If you're interested in how different Haskell typeclasses are used to implement core functions of co-log, you can read the following blog post which goes into detail about the internal implementation specifics:

Co-Log Family

Co-Log is a family of repositories for a composable and configurable logging framework co-log.

Here is the list of currently available repositories and libraries that you can check out:

co-log-core lightweight package with basic data types and general idea which depends only on base Hackage
co-log taggless final implementation of logging library based on co-log-core Hackage
co-log-polysemy implementation of logging library based on co-log-core and the polysemy extensible effects library. Hackage
co-log-benchmarks benchmarks of the co-log library -

co-log library

Logging library based on co-log-core package. Provides ready-to-go implementation of logging. This README contains How to tutorial on using this library. This tutorial explains step by step how to integrate co-log into small basic project, specifically how to replace putStrLn used for logging with library provided logging.

All code below can be compiled and run with the following commands:

$ cabal build
$ cabal exec readme

Preamble: imports and language extensions

Since this is a literate haskell file, we need to specify all our language extensions and imports up front.

{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}

import Control.Monad.IO.Class (MonadIO, liftIO)

import Colog (Message, WithLog, cmap, fmtMessage, logDebug, logInfo, logTextStdout, logWarning,
              usingLoggerT)

import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO

Simple IO function example

Consider the following function that reads lines from stdin and outputs different feedback depending on the line size.

processLinesBasic :: IO ()
processLinesBasic = do
    line <- TextIO.getLine
    case Text.length line of
        0 -> do
            -- here goes logging
            TextIO.putStrLn ">>>> Empty input"
            processLinesBasic
        n -> do
            TextIO.putStrLn ">>>> Correct input"
            TextIO.putStrLn $ "Line length: " <> Text.pack (show n)

This code mixes application logic with logging of the steps. It's convenient to have logging to observe behavior of the application. But putStrLn is very simple and primitive way to log things.

Using co-log library

In order to use co-log library, we need to refactor processLinesBasic function in the following way:

processLinesLog :: (WithLog env Message m, MonadIO m) => m ()
processLinesLog = do
    line <- liftIO TextIO.getLine
    case Text.length line of
        0 -> do
            -- here goes logging
            logWarning "Empty input"
            processLinesLog
        n -> do
            logDebug "Correct line"
            logInfo $ "Line length: " <> Text.pack (show n)

Let's summarize required changes:

  1. Make type more polymorphic: (WithLog env Message m, MonadIO m) => m ()
  2. Add liftIO to all IO functions.
  3. Replace putStrLn with proper log* function.

Running actions

Let's run both functions:

main :: IO ()
main = do
    processLinesBasic

    let action = cmap fmtMessage logTextStdout
    usingLoggerT action processLinesLog

And here is how output looks like:

screenshot from 2018-09-17 20-52-01

More Tutorials

To provide a more user-friendly introduction to the library, we've created the tutorial series which introduces the main concepts behind co-log smoothly, please check more details here.