LuaLogging logo
LuaLogging
A simple API to use logging features in Lua
home · logger · example · console · email · file · socket · sql

Introduction

LuaLogging is a simple API to use logging features in Lua. Its design is based on log4j.

LuaLogging defines one single global variable, a table called logging which holds a function to create new logger objects.
This logger constructor receives a function that will be called on each call to log a message. This function is called an appender function.
An appender function receives three arguments:

The example below creates a logger that prints the level and message to the standard output (or whatever the print function do).

local logger = logging.new(function(self, level, message)
                        print(level, message)
                        return true
                     end)

LuaLogging provides some extensions that implement several appenders.

Logger objects

A logger object offers a few methods that writes log messages.

Methods

Levels

Example

Below is a small sample code displaying the basic use of the library.
require "logging"

local logger = logging.new(function(self, level, message)
                            io.stdout:write(logging.prepareLogMsg("%date %level %message\n", os.date(), level, message))
                            return true
                           end)
                           
logger:setLevel (logging.WARN)
logger:log(logging.INFO, "sending email")

logger:info("trying to contact server")
logger:warn("server did not responded yet")
logger:error("server unreachable")

Upon execution the following lines will show in the standart output.
Notice that the INFO log requests are not handled because the minimum level is set to WARN.
06/17/04 16:21:15 WARN server did not responded yet
06/17/04 16:21:20 ERROR server unreachable

Appenders

The following appenders are included in the standard distribution.

Upgrading from 1.0.0

Upgrading from LuaLogging 1.0.0 is very easy. The logger object is fully compatible. You just need to change the code that creates the object.

The logger constructor from 1.0.0 received a single argument which was a filename. To upgrade to 1.1.0 you should create a logging.file object instead, passing the filename as argument. As simple as this.


$Id: index.html,v 1.1 2004/11/16 16:33:10 tuler Exp $