Add. Basic log class.

This commit is contained in:
Alexey Melnichuk 2015-09-09 10:45:49 +04:00
parent 029b806006
commit 8c2ea6e093
2 changed files with 75 additions and 4 deletions

View File

@ -25,15 +25,16 @@
-- POSSIBILITY OF SUCH DAMAGE.
local cache = require"resources.functions.cache"
local log = require"resources.functions.log"["xml_handler"]
--get the cache
XML_STRING, err = cache.get("dialplan:" .. call_context)
if debug['cache'] then
if XML_STRING then
freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: memcache\n");
log.notice("dialplan:"..call_context.." source: memcache");
elseif err ~= 'NOT FOUND' then
freeswitch.consoleLog("notice", "[xml_handler] error get element form cache: " .. err .. "\n");
log.notice("error get element form cache: " .. err);
end
end
@ -105,7 +106,7 @@
sql = sql .. "ELSE 100 END, ";
sql = sql .. "s.dialplan_detail_order asc ";
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n");
log.notice("SQL: " .. sql);
end
x = 0;
dbh:query(sql, function(row)
@ -306,7 +307,7 @@
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: database\n");
log.notice("dialplan:"..call_context.." source: database");
end
--close the database connection

View File

@ -0,0 +1,70 @@
-- @usage local log = require"resources.functions.log"["xml_handler"]
-- log.notice("hello world")
-- log.noticef("%s %s", "hello", "world")
-- -- log if debug.SQL or debug.xml_handler.SQL then
-- log.tracef("SQL", "SQL is %s", sql)
local function log(name, level, msg)
freeswitch.consoleLog(level, "[" .. name .. "] " .. msg .. "\n")
end
local function logf(name, level, ...)
return log(name, level, string.format(...))
end
local function trace(type, name, ...)
local t = debug[name]
if t and t[type] ~= nil then
if t[type] then
return log(name, ...)
end
end
if debug[type] then
log(name, ...)
end
end
local function tracef(type, name, level, ...)
local t = debug[name]
if t and t[type] ~= nil then
if t[type] then
return logf(name, ...)
end
end
if debug[type] then
logf(name, ...)
end
end
local LEVELS = {
'error',
'warning',
'notice',
'info',
}
local TRACE_LEVEL = 'notice'
local function make_log(name)
local logger = {}
for i = 1, #LEVELS do
logger[ LEVELS[i] ] = function(...) return log(name, LEVELS[i], ...) end;
logger[ LEVELS[i] .. "f" ] = function(...) return logf(name, LEVELS[i], ...) end;
end
logger.trace = function(type, ...)
trace(type, name, TRACE_LEVEL, ...)
end
logger.tracef = function(type, ...)
tracef(type, name, TRACE_LEVEL, ...)
end
return logger
end
return setmetatable({}, {__index = function(self, name)
local logger = make_log(name)
self[name] = logger
return logger
end})