| Copyright | (c) Dong Han 2017-2018 |
|---|---|
| License | BSD |
| Maintainer | winterland1989@gmail.com |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Z.IO.Logger
Contents
Description
Simple, high performance logger. The design choice of this logger is biased towards simplicity instead of generlization:
- All log functions live in
IO. - By default this logger is connected to stderr, use
setDefaultLoggerto customize. When logging each thread will build log
Builders into a smallByteswith line buffer instead of leaving allBuilders to the flushing thread:- Logger won't keep heap data for too long simply because they're referenced by log's
Builder. - Each logging thread only need perform a CAS to prepend log
Bytesinto a list, which reduces contention. - Each log call is atomic, Logging order is preserved under concurrent settings.
- Logger won't keep heap data for too long simply because they're referenced by log's
Flushing is automatic and throttled for debug, info, warn to boost performance, but a fatal log will always flush logger's buffer. This could lead to a problem that if main thread exits too early logs may missed, to add a flushing when program exits, use withDefaultLogger like:
import Z.IO.Logger
main :: IO ()
main = withDefaultLogger $ do
....
debug "..." -- So that this log won't be missed
...
Synopsis
- data Logger = Logger {
- loggerPushBuilder :: Builder () -> IO ()
- flushLogger :: IO ()
- flushLoggerThrottled :: IO ()
- loggerTSCache :: IO (Builder ())
- loggerFmt :: LogFormatter
- data LoggerConfig = LoggerConfig {}
- defaultLoggerConfig :: LoggerConfig
- setDefaultLogger :: Logger -> IO ()
- getDefaultLogger :: IO Logger
- flushDefaultLogger :: IO ()
- withDefaultLogger :: IO () -> IO ()
- newLogger :: Output o => LoggerConfig -> MVar (BufferedOutput o) -> IO Logger
- newColoredLogger :: LoggerConfig -> IO Logger
- debug :: HasCallStack => Builder () -> IO ()
- info :: HasCallStack => Builder () -> IO ()
- warn :: HasCallStack => Builder () -> IO ()
- fatal :: HasCallStack => Builder () -> IO ()
- otherLevel :: HasCallStack => Level -> Bool -> Builder () -> IO ()
- type Level = CBytes
- debugTo :: HasCallStack => Logger -> Builder () -> IO ()
- infoTo :: HasCallStack => Logger -> Builder () -> IO ()
- warnTo :: HasCallStack => Logger -> Builder () -> IO ()
- fatalTo :: HasCallStack => Logger -> Builder () -> IO ()
- otherLevelTo :: HasCallStack => Logger -> Level -> Bool -> Builder () -> IO ()
- defaultTSCache :: IO (Builder ())
- defaultFmtCallStack :: CallStack -> Builder ()
- type LogFormatter = Builder () -> Level -> Builder () -> CallStack -> Builder ()
- defaultFmt :: Bool -> LogFormatter
- coloredFmt :: Bool -> LogFormatter
- flushLog :: (HasCallStack, Output o) => MVar (BufferedOutput o) -> IORef [Bytes] -> IO ()
A simple Logger type
Constructors
| Logger | |
Fields
| |
data LoggerConfig Source #
Constructors
| LoggerConfig | |
Fields
| |
defaultLoggerConfig :: LoggerConfig Source #
A default logger config with
- 0.1s minimal flush interval
- line buffer size 240 bytes
- show debug True
setDefaultLogger :: Logger -> IO () Source #
Change the global logger.
getDefaultLogger :: IO Logger Source #
Get the global logger.
flushDefaultLogger :: IO () Source #
Manually flush stderr logger.
withDefaultLogger :: IO () -> IO () Source #
Flush stderr logger when program exits.
newLogger :: Output o => LoggerConfig -> MVar (BufferedOutput o) -> IO Logger Source #
Make a new simple logger.
newColoredLogger :: LoggerConfig -> IO Logger Source #
Make a new colored logger connected to stderr.
This logger will output colorized log if stderr is connected to TTY.
logging functions
Arguments
| :: HasCallStack | |
| => Level | log level |
| -> Bool | flush immediately? |
| -> Builder () | log content |
| -> IO () |
logging functions with specific logger
Helper to write new logger
defaultTSCache :: IO (Builder ()) Source #
A default timestamp cache with format %Y-%m-%dT%H:%M:%S%Z
The timestamp will updated in 0.1s granularity to ensure a seconds level precision.
defaultFmtCallStack :: CallStack -> Builder () Source #
Default stack formatter which fetch the logging source and location.
type LogFormatter Source #
Arguments
| :: Bool | show DEGUG? |
| -> LogFormatter |
A default log formatter
[DEBUG][2020-10-09T07:44:14UTC][interactive:7:1]This a debug message\n
Arguments
| :: Bool | show DEBUG? |
| -> LogFormatter |