Easy_logging 0.2


feeling that there was some lack of logging librairies for OCaml (apart from the Logs module, which was a bit too complicated to use for me), I wrote a (hopefully) both easy to use and yet powerful logging library, taking inspiration from the excellent Python logging package.

Some defaults are provided that allow logging to the stdout and/or files, but it is also possible to redefine you own handlers.

Sources and Documentation are provided.

Any comment or suggestion would be appreciated.



I believe support for printf-style strings for creating log messages would be useful. Formatting strings even when they are not used because of the log priority can introduce a performance concern, though.



Hello lindig,
I think I will eventually add support for format strings, but for now you can use the logging methods that take a lazy string as an input, for example

 logging#ldebug ( lazy (Printf.sprintf "%s" mystring))

see lazy logging methods.



How does this compare to Async’s Log module, which is generally used as follows:

Log.Global.info "Message %d" count
Log.Global.warn_s [%message "Issue connecting to db" (db : string)]


Hello mc10,
I have never used Async, so I didn’t know about that module. It seems quite close to what I had in mind, and provides most of what easy_logging does, and much more.

Instead of instantiating modules (like Log.Global) as so called logging singletons, Easy_logging instantiates logger objects:

open Easy_logging
logger = Logging.make_logger "my_logger" (Some Debug) [Cli Debug];;
logger#info "log_message";; 

This is by design, since logger objects are registered and stored at creation (in a Logging module), to be able to modify the log level all logging singletons at runtime (see Logging module and example).
(Maybe I’ll switch to modules instead of classes as logging singletons, using first-class modules to registration, but for now it’s a bit easier for me to go with objects).

The second difference is that a Logging module can be created by a functor over a module of Handlers (things to write to stdout/files/etc), making it easy to define the handlers according to your needs (although Async’s log defaults seems quite enough).

Anyway Async’s Logging will be a great source of inspiration for me, thanks for pointing it out !



Version 0.4 is out (or should be soon) :

  • printf style logging is now the default
  • simplifed configuration in case of multiple loggers (and closer to the python module) :
    the loggers form a tree (based on their name, dots indicating descendence)
    • log items are passed to the handlers of a logger’s ancestors (so that few handlers need initialisation) (possible to override)
    • loggers inherit the level of their ancestor if not set explicitely
  • an additional package easy_logging_yojson provides initialisation of loggers from the json format (with ppx_deriving_yojson), so that configuration can be fully done outside the code.