package easy_logging
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=12fb8044e96ea1ace1cc465c766cde7e
sha512=17ab30169c54a13f3aff8605bd1acaffb911c6882fa3a0c7ad6c14b2dcbd3c08b0f2568fb0ec500ae6e741be926a49fb73954d2ccfedcd274463ffed20149b02
doc/index.html
Easy logging
Logging infrastructure inspired by the Python logging module. The aim of this module is to provide a quick and easy to use logging infrastructure.
It has the following features :
- one line logger creation
- log messages printf style, or provide
stringorstring lazy_tmessage - tree architecture for easy configuration
- handlers associated to each logger will format, filter and treat the message independantly
- use the infrastructure with your own handlers with the
MakeLoggingfunctor. - use tags to add contextual information to log messages
Basic example
open Easy_logging
logger = Logging.make_logger "my_logger" (Some Debug) [Cli Debug];;
logger#info "log_message";; will output to the stdout a message of the form
1.306 my_logger Info log_message
Index
- Overall description (see also
Easy_logging) - Handlers (see also
Easy_logging__Handlers) - Loggers (see also
Easy_logging.Logging.logger) - The Logging module (see also
Easy_logging.Logging)
- Handlers (see also
- The MakeLogging functor (see also
Easy_logging.MakeLogging) - More examples
Overall description
The logging infrastructure is based on four concepts:
loggers, handlers, log items and logging tree.
A call to logger will create a log item, which it will pass to its handlers. Each handler will treats the item (e.g. transform it to a string, and then outputs to stdout or to a file).
___________
| handler 1 |
_______________ |-----------|
| logger | ==> | ( * ) |
|---------------| |___________|
(message) ==> | -> log item | ___________
[_______________| ==> | handler 2 |
| ... |Logging tree
Loggers are stored in a tree structure, with the following properties :
- messages handled by a logger (after level filtering) are passed to its ancestor's handlers (this can be overriden with the
set_propagatemethod). - if not set, the level of a logger is that of its closest ancestor whose level is set (or NoLevel if there is no such ancestor)
Example
A (Info, Cli Debug)
/ \
A.B A.C
/ \
A.B.D (Warning) A.C.E (Debug, (File "f", Debug),
propagate=false)For example, with the above logging tree
- logs made to
A,A.BorA.Cof level Info or more are written to the stdout. - logs made to
A.B.Dof levelWarningor above are written the stdout. - logs made to
A.C.Eare only written to the file"f"
Levels
To each logger and log message are associated a level, which will be used to filter the messages going through the logging infrastructure.
The predefined levels are, in increasing order of precedence :
- Debug : used for debugging.
- Trace : used between info and debug
- Info : used to trace program execution.
- Warning : used for warnings.
- Error : used for errors.
- Flash : used for one-shot debugging: displays an easy to spot message.
- NoLevel : used to filter out all messages.
Log items
A log item has type
type log_item = {
level : level;
logger_name : string;
msg : string;
tags : tag list
} where the tag type is defined by the Handlers module.
Defaults
Handlers
Two handler creation helper function are provided. They instantiate handlers with a level of their own to filter messages :
Cli handler: outputs colored messages to stdout.
let h = Default_handlers.make (Cli Debug)File handler : outputs messages to a given file.
let h = Default_handlers.make (File ("filename", Debug))
Note that file handlers will write to files in the logs folder in the current path, creating it if it doesn't exist. See more about default handlers at Easy_logging__Default_handlers.
See more at Easy_logging.Handlers
Loggers
See complete class documentation at Easy_logging.Logging.logger
Creation
A logger object can be created directly (in which case it will not be part of the logging tree)
let logger1 = new Logging.logger "my_logger1" or through helper functions of the The Logging module module.
Usage
A logger object has three methods for each of the log levels:
one that takes a formatting string and parameters (printf like)
logger1#debug "Myvar : %s" (to_string myvar);one that takes a
string lazy_tlogger1#ldebug (lazy (heavy_calculation ()));one that takes a
stringlogger1#sdebug (to_string myvar);
The Logging module
The Easy_logging.Logging module is that application of MakeLogging over DefaultHandlers. It provides two functions :
val make_logger : ?propagate:bool -> string -> log_level -> Default_handlers.desc listInstantiates a logger with some paramters, and adds it to the logging tree.
val get_logger : string -> loggerReturns a registered logger, or creates a new one if it doesn't exist.
The MakeLogging functor
The MakeLogging functor takes a Easy_logging__.Easy_logging_types.HandlersT typed module, and creates a Logging module.
WARNING
When declaring your Handlers module, do not coerce it the type HandlersT, because then its internal types t and desc won't be accessible.
Examples
See the examples page for more examples.