logger.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. local skynet = require "skynet"
  2. local skynet_error = skynet.error
  3. local string_format = string.format
  4. local table_insert = table.insert
  5. local os_date = os.date
  6. local os_time = os.time
  7. local const_header = { "(T)|", "(D)|", "(I)|", "(W)|", "(E)|", "(F)|", "(TS)" }
  8. local header = const_header
  9. local function print(level, fmt, ...)
  10. skynet_error(
  11. os_date("[%m/%d %X]", os_time()),
  12. header[level],
  13. string_format(fmt, ...))
  14. end
  15. local logger = {}
  16. function logger.label(name)
  17. if name then
  18. header = {}
  19. for _, v in ipairs(const_header) do
  20. table_insert(header, v..name)
  21. end
  22. else
  23. header = const_header
  24. end
  25. end
  26. function logger.test(fmt, ...) print(7, fmt, ...) end
  27. -- function logger.test(fmt, ...) end
  28. function logger.trace(fmt, ...) print(1, fmt, ...) end
  29. function logger.debug(fmt, ...) print(2, fmt, ...) end
  30. function logger.info(fmt, ...) print(3, fmt, ...) end
  31. function logger.warn(fmt, ...) print(4, fmt, ...) end
  32. function logger.error(fmt, ...) print(5, fmt, ...) end
  33. function logger.fatal(fmt, ...) print(6, fmt, ...) end
  34. return logger