Skip to content

Commit f07813c

Browse files
committed
Prettify library.
1 parent 85bbb61 commit f07813c

File tree

10 files changed

+81
-28
lines changed

10 files changed

+81
-28
lines changed

Readme

Lines changed: 0 additions & 7 deletions
This file was deleted.

Readme.markdown

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Cheops-logger
2+
3+
Logger library for structured logging. This library is a light extension of the
4+
[co-log-concurrent](https://hackage.haskell.org/packages/co-log-concurrent) package.
5+
That allows to have asynchronous structured logs writing.
6+
7+
8+
## Installation
9+
10+
## Tutorial
11+
12+
### Adding package
13+
14+
### Initialization
15+
16+
### Context
17+

cheops-logger.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ library
6363
Cheops.Logger
6464
Cheops.Logger.Internal.Structured
6565
Cheops.Logger.Internal.Writer
66+
Cheops.Logger.Internal.Metric
6667
default-extensions:
6768
BlockArguments
6869
DerivingStrategies

examples/Main

1.69 MB
Binary file not shown.

examples/Main.hi

3.53 KB
Binary file not shown.

examples/Main.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{-# LANGUAGE ImplicitParams #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
module Main
4+
where
5+
6+
import Cheops.Logger
7+
import Data.Function
8+
9+
main :: IO ()
10+
main = do
11+
withLogger config $ \ initial_logger -> do
12+
let ?logger = initial_logger
13+
logDebug ?logger "Hello!"
14+
logInfo ?logger "World!"
15+
let ?logger = ?logger
16+
& addContext (sl "host" ("127.0.0.1"::String))
17+
& addNamespace "www"
18+
logErr ?logger "Alert!"
19+
where
20+
config = LoggerConfig defCapacity Nothing

examples/Main.o

10.9 KB
Binary file not shown.

src/Cheops/Logger.hs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ module Cheops.Logger
5656
-- $setup
5757
) where
5858

59+
import Cheops.Logger.Internal.Metric
5960
import Cheops.Logger.Internal.Structured
6061
import Cheops.Logger.Internal.Writer
6162
import Colog.Concurrent
6263
import Colog.Concurrent.Internal
6364
import Colog.Core hiding (Severity, Info)
6465
import Control.Concurrent
6566
import Data.Aeson
66-
import Prometheus
67-
import Prometheus.Metric.WindowGauge as Window
6867
import System.IO
6968
import qualified Data.Sequence as Seq
7069
import qualified Data.Text as T
@@ -105,11 +104,11 @@ withLogger LoggerConfig{..} f
105104
| otherwise =
106105
withBackgroundLogger
107106
loggerConfigMessagesInFlight
108-
(LogAction $ \x -> feed x >> incCounter metric_written_total >> Window.decGauge metric_in_flight)
107+
(LogAction $ flushLog . feed)
109108
(hFlush stdout)
110109
$ \(LogAction log_action) ->
111110
f $ LoggerEnv
112-
(LogAction $ \x -> incCounter metric_submitted_total >> log_action x >> Window.incGauge metric_in_flight)
111+
(LogAction $ submitLog . log_action)
113112
Seq.Empty
114113
where
115114
LogAction feed = feedHandle stdout
@@ -211,18 +210,3 @@ logEmergency x = logSay x EmergencyS
211210
-- These metrics could tell if configuration is good and if the package is health. For example if
212211
-- @ghc_logger_max_in_flight@ is close to default limit then you may want to extend buffer size, give more CPU
213212
-- units to the application, or improve internal structures in the package.
214-
215-
-- | Metrics.
216-
metric_submitted_total :: Counter
217-
metric_submitted_total = unsafeRegister $
218-
counter $ Info "ghc_logger_submitted_total" "Total amount of submitted messages"
219-
220-
-- | Metrics.
221-
metric_written_total :: Counter
222-
metric_written_total = unsafeRegister $
223-
counter $ Info "ghc_logger_written_total" "Total amount of written messages"
224-
225-
-- | Metrics.
226-
metric_in_flight :: WinGauge
227-
metric_in_flight = unsafeRegister $
228-
Window.gauge $ Info "ghc_logger_max_in_flight" "Maximum number of in flight messages"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- |
2+
-- Metrics support.
3+
module Cheops.Logger.Internal.Metric
4+
( submitLog
5+
, flushLog
6+
) where
7+
8+
import Prometheus
9+
import Prometheus.Metric.WindowGauge as Window
10+
11+
-- | Write metrics when message is submitted.
12+
submitLog :: IO () -> IO ()
13+
submitLog f = do
14+
incCounter metric_submitted_total
15+
f
16+
Window.incGauge metric_in_flight
17+
18+
-- | Write metrics when message is flushed.
19+
flushLog :: IO () -> IO ()
20+
flushLog f = do
21+
f
22+
incCounter metric_written_total
23+
Window.decGauge metric_in_flight
24+
25+
-- | Metrics.
26+
metric_submitted_total :: Counter
27+
metric_submitted_total = unsafeRegister $
28+
counter $ Info "ghc_logger_submitted_total" "Total amount of submitted messages"
29+
30+
-- | Metrics.
31+
metric_written_total :: Counter
32+
metric_written_total = unsafeRegister $
33+
counter $ Info "ghc_logger_written_total" "Total amount of written messages"
34+
35+
-- | Metrics.
36+
metric_in_flight :: WinGauge
37+
metric_in_flight = unsafeRegister $
38+
Window.gauge $ Info "ghc_logger_max_in_flight" "Maximum number of in flight messages"

src/Cheops/Logger/Internal/Structured.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ data Message = Message
6767
}
6868

6969

70-
71-
-- | Katip compatibility.
70+
-- | Efficient message builder.
7271
newtype LogStr = LogStr TLB.Builder
7372
deriving newtype IsString
7473
deriving newtype Semigroup
@@ -106,6 +105,7 @@ newtype PushContext = PushContext (Seq Structured -> Seq Structured)
106105
-- @sl "foo" 123@
107106
--
108107
-- Will add @"foo":123@ key pair to the current list of the attributes.
108+
-- Submitted value is stored with json encoding.
109109
sl :: ToJSON a => T.Text -> a -> PushContext
110110
sl label msg = PushContext \x ->
111111
x |> Attr label (toEncoding msg)

0 commit comments

Comments
 (0)