--[[ lua中日志模块,可以设置日志过滤等级,在日志后附加Lua调用栈等 debug/log/warn/crit/error 各个等级的日志打印方法 debug 开发期调试类信息 log 一般提示性信息 warn 需要引起重视的预警类信息 crit 特别关键的信息,可用于记录运行过程中的关键步骤 error 错误信息 setLevel 设置当前日志等级,用来控制只有大于或等于当前日志等级的内容会被输出 logging.setLevel(logging.LL_WARN)则只会打印warn, error, crit,而不输出debug, log LL_DISABLED关闭所有日志输出 setTraceLevel 设置输出调用栈的日志等级,只有大于等于该等级的日志会输出调用栈,默认等级为LL_ERROR setTablePrintDepth 所有的日志打印接口,对于参数中的table默认会遍历其中元素并进行打印,此接口用于设置打印table时遍历的最大层数,默认为5 setTagStatus 设置日志Tag的状态 setTagCheckMode 设置日志Tag过滤模式 --]] local logging = { --LogLevel defines LL_DEBUG = 0, LL_LOG = 1, LL_WARN = 2, LL_CRIT = 3, LL_ERROR = 4, LL_Exception = 5, LL_DISABLED = 100, LT_DEBUG = "[Debug] ", LT_LOG = "[Log] ", LT_WARN = "[Warn] ", LT_CRIT = "[Crit] ", LT_ERROR = "[Error] ", } local _logLevel = logging.LL_LOG local _traceLevel = logging.LL_ERROR local _tagStatus = {} local _tagCheckInclude = true local _raw_print = print local _logFunc = LogExt.logFunction function logging.setLevel(level) _logLevel = level end function logging.getLevel() return _logLevel end function logging.setTraceLevel(level) _traceLevel = level end function logging.setTagCheckMode(includeMode) _tagCheckInclude = includeMode end function logging.setTagStatus(tags, status) if tags then if type(tags) == "table" then for _,v in ipairs(tags) do _tagStatus[v] = status end elseif type(tags) == "string" then _tagStatus[tags] = status else error("invalid argument type") end end end logging.setTablePrintDepth = LogExt.setTablePrintDepth local _logHandler = function(level, tag, ...) if level >= _logLevel and tag ~= nil then if _tagCheckInclude then if _tagStatus[tag] then _logFunc(level, tag, level - _traceLevel, ...) end else if not _tagStatus[tag] then _logFunc(level, tag, level - _traceLevel, ...) end end end end function logging.debug(tag, ...) _logHandler(logging.LL_DEBUG, tag, ...) end function logging.log(tag, ...) _logHandler(logging.LL_LOG, tag, ...) end function logging.warn(tag, ...) _logHandler(logging.LL_WARN, tag, ...) end function logging.crit(tag, ...) _logHandler(logging.LL_CRIT, tag, ...) end function logging.error(tag, ...) _logHandler(logging.LL_ERROR, tag, ...) end function logging.getLogStatisticInfo() return LogExt.getLogCount(logging.LL_WARN) or 0, LogExt.getLogCount(logging.LL_Exception) or 0, LogExt.getLogCount(logging.LL_ERROR) or 0 end do --logging init rawset(_G, "raw_print", _raw_print) rawset(_G, "print", function(...) local level = logging.LL_LOG local tag = nil if level >= _logLevel then _logFunc(level, tag, level - _traceLevel, ...) end end) --移除日志打印函数相关的栈 --Print (C# level 1) -> _logHandler (Lua level 2) -> log API (Lua level 3) -> log caller (Lua level 4) LogExt.setTracebackLevel(4) end return logging --[[ local function test_cases() local v = { 1, key1 = 'val1', v = { 2, key2 = 'val2', v = { 3, key3 = 'val3', v = { 4, key4 = 'val4', v = { 5, key5 = 'val5', v = { 6, key6 = 'val6', } } } } } } logging.setTablePrintDepth(2) logging.crit(logging.LT_CRIT, 'test table print depth = 2', v) logging.setTablePrintDepth(10) logging.crit(logging.LT_CRIT, 'test table print depth = 10', v) local tags = {logging.LT_DEBUG, logging.LT_LOG, logging.LT_WARN, logging.LT_CRIT, logging.LT_ERROR} local logs = {logging.debug, logging.log, logging.warn, logging.crit, logging.error} for i, tag in ipairs(tags) do local logFunc = logs[i] logFunc(tag, "this is log function "..tostring(logFunc)) logFunc(tag, nil) logFunc(tag, 1) logFunc(tag, 0, 1, nil, 2) logFunc(tag, {x = 5, y = 6}) logFunc(tag, {7 , 8}) logFunc(tag, {m = 4, 8}) logFunc(tag, 5, {m = {4, 8}}, 6, {10, 11}) end end test_cases() --]]