File /tmp/svntemp/hscale/src/optivo/common/log4lua/logger.lua
Logging facility. A logger consists of a category (to distinct different modules etc.) and an appender which actually writes the message (to console, file etc.)
Configuration
A sample configuration file looks like this: local logger = require("optivo.common.log4lua.logger")
local console = require("optivo.common.log4lua.appenders.console")
local file = require("optivo.common.log4lua.appenders.file")
local config = {}
-- ROOT category must be configured.
config["ROOT"] = logger.Logger.new(console.new(), "ROOT", logger.FATAL)
config["foo"] = logger.Logger.new(file.new("foo-%s.log", "%Y-%m-%d"), "foo", logger.INFO)
config["bar"] = logger.Logger.new(file.new("bar.log", nil, "%LEVEL: %MESSAGE\n"), "bar", logger.INFO)
-- The config table must be returned.
return config Then you can load this configuration file "by hand" calling
loadConfig(fileName) or you can set a default configuration file using the environment variable LOG4LUA_CONFIG_FILE. Patterns
Patterns may contain the following placeholders:- %DATE
- %LEVEL
- %MESSAGE
- %FILE - the source filename w/o path
- %PATH - the source filename including the path
- %LINE - the position in the source file
- %FUNCTION - the function name
- %STACKTRACE - the complete stack trace
- %ERROR - an exception string like the one you get using pcall(...)
%FILE, %PATH, %LINE, %FUNCTION, %STACKTRACE implies a quite huge performance hit because debug.traceback() has to be called for every message logged. Note that the default pattern uses these placeholders. Default pattern for all appenders is [%DATE] [%LEVEL] at %FILE:%LINE(%METHOD): %MESSAGE\n
Release: $Date: 2008-09-21 03:11:45 +0200 (So, 21 Sep 2008) $ $Rev: 87 $
Functions
| Logger.new (appenders, category, level) | Constructor. |
| Logger:debug (message, exception) | Log message at DEBUG level. |
| Logger:error (message, exception) | Log message at ERROR level. |
| Logger:fatal (message, exception) | Log message at FATAL level. |
| Logger:info (message, exception) | Log message at INFO level. |
| Logger:isLevel (level) | Test whether the given level is enabled. |
| Logger:log (level, message, exception) | Log the given message at the given level. |
| Logger:setLevel (level) | Set the log level threshold. |
| Logger:warn (message, exception) | Log message at WARN level. |
| getLogger (category) | Main method that returns a fully configured logger for the given category. The correct logger is found as follows:
|
| loadConfig (fileName) | Load a configuration file. |
Functions
- Logger.new (appenders, category, level)
-
Constructor.
Parameters
- appenders: a single function or a table of functions taking a string as parameter that is responsible for writing the log message.
- category: the category (== name) of this logger
- level: the threshold level. Only messages for equal or higher levels will be logged.
- Logger:debug (message, exception)
-
Log message at DEBUG level.
Parameters
- message:
- exception:
- Logger:error (message, exception)
-
Log message at ERROR level.
Parameters
- message:
- exception:
- Logger:fatal (message, exception)
-
Log message at FATAL level.
Parameters
- message:
- exception:
- Logger:info (message, exception)
-
Log message at INFO level.
Parameters
- message:
- exception:
- Logger:isLevel (level)
-
Test whether the given level is enabled.
Parameters
- level:
Return value:
true if messages of the given level will be logged. - Logger:log (level, message, exception)
-
Log the given message at the given level.
Parameters
- level:
- message:
- exception:
- Logger:setLevel (level)
-
Set the log level threshold.
Parameters
- level:
- Logger:warn (message, exception)
-
Log message at WARN level.
Parameters
- message:
- exception:
- getLogger (category)
-
Main method that returns a fully configured logger for the given category.
The correct logger is found as follows:- If there is a configured logger with the exact category then use this.
- Otherwise search for loggers with matching category. Example: If there is a configured logger for category "test" then it is used for category "test.whatever", "testinger" etc.
- Otherwise use the root category.
Parameters
- category: the category of the desired logger.
- loadConfig (fileName)
-
Load a configuration file.
Parameters
- fileName: path to a configuration file written in lua. The lua code must return a map (table) with loggers configured for each category.