|
LUA - Projects
|
What is Log4LUA?Log4LUA is a logging framework in the style of Log4J. It is simple and pretty much straightforward though.
See this blog post for an introduction and a discussion of the differences to other logging frameworks such as LuaLogging. Features
InstallationDownload and unpack to a directory of your choice. Make sure that InstallDirectory/src/?.lua is in your LUA_PATH. ConfigurationWrite your configuration just like this: log4lua.conf.lua local logger = require("optivo.common.log4lua.logger") local console = require("optivo.common.log4lua.appenders.console") local file = require("optivo.common.log4lua.appenders.file") local smtp = require("optivo.common.log4lua.appenders.smtp") local config = {} -- Create a default smtp appender sending message of level WARN or higher. local defaultSmtp = smtp.new( { headers = { from = "myserver@mydomain.com", to = "admin@mydomain.com", subject = "%LEVEL: %MESSAGE" }, body = "Hi, an error occurred at %FILE:%LINE.\n\nLevel: %LEVEL\nMessage: %MESSAGE\n" }, "mail.mydomain.com", logger.WARN ) -- ROOT category must be configured. -- For the root category we use console and smtp appender config["ROOT"] = logger.Logger.new({console.new(), defaultSmtp}, "ROOT", logger.INFO) -- Category "core" uses rotating file appender and the default message pattern config["core"] = logger.Logger.new(file.new("core-%s.log", "%Y-%m-%d"), "core", logger.INFO) -- The config table must be returned. return config More detailed information is available in the configuration reference. Usagemyapp.lua local logger = require("optivo.common.log4lua.logger") -- I would prefer calling the main logger LOG. -- This will return a logger for category "ROOT" since there is no category "mycode". local LOG = logger.getLogger("mycode") -- This will return a logger of the category "core" since the category name -- "core.moduleA" starts with "core" local LOG_CORE = logger.getLogger("core.moduleA") -- Log something LOG:info("My first log message") -- Tables are converted to string for you. LOG:warn({name = "Paul", age = "22"}) -- Log to different category. LOG_CORE:fatal("System error") -- Test if a level is enabled. if (LOG:isLevel(LOG.DEBUG)) then LOG:debug("Only execute this statement if DEBUG is really enabled.") end -- Load a configuration by hand (you shouldn't have to do this). logger.loadConfig("myOtherConfig.lua") RunningThe environment variable LOG4LUA_CONFIG_FILE contains the path to the logging configuration to be used. Run with specified logging configuration LOG4LUA_CONFIG_FILE = "log4lua.conf.lua" lua myapp.lua
ChangesVersion 0.2 2008-09-05
Version 0.1 2008-09-02
|