Home

Awesome

Log4p

A simple logging library for Prolog, inspired by log4j.

Installation

This is a package for SWI-Prolog, installable using the built-in package manager with the package name log4p.

?- pack_install(log4p).

Use

The basic model for logging involves a few simple concepts.

The log levels understood by this library, in ascending order of priority, are: trace, debug, info, warn, error, fatal. If the current log level is set to info, for example, then message of debug or to further left in that list will not be given to a handler.

Generating messages is usually a matter of using a number of predicates named for each level: info/1, info/2, warn/1, warn/2 etc. The /1 variant logs a constant string (or term rendered as a string). The /2 variant takes a format string and an array of arguments, then calls swritef to generate a constant string which is then passed onto log handlers. If the currently effective log level for the calling thread is not equal to or "higher" (e.g., more to the right in the above list of log levels), then the message will be filtered out and not delivered to any handlers.

The effective log level is a layered per-thread choice, depending on which settings are specified:

The current level can be set using the any of the following techniques:

Log handlers disseminate messages to an output desintation, such as stdout or stderr or a log file. To add or remove log handlers, use add_log_handler/1 and remove_log_handler/1 respectively. A log handler is a simple function that simply takes 2 arguments: the level of the message, and the message etc. (after formatting).