|
| LuaLogging |
| A simple API to use logging features in Lua |
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:
self: the logger objectlevel: the logging levelmessage: the message to be loggedThe 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.
A logger object offers a few methods that writes log messages.
message may be any lua value, not
just strings. The same applies to all the functions below. level is one of the variables enumerated
here. This method sets a minimum level for
messages to be logged.
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.06/17/04 16:21:15 WARN server did not responded yet 06/17/04 16:21:20 ERROR server unreachable
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.