Advertisement
z1haze

Logger.lua

Feb 25th, 2025 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. -- logger.lua
  2. -- A simple logging module for ComputerCraft
  3.  
  4. local Logger = {}
  5.  
  6. -- Log levels
  7. local LEVELS = {
  8.     DEBUG = 1,
  9.     INFO = 2,
  10.     WARN = 3,
  11.     ERROR = 4,
  12.     FATAL = 5
  13. }
  14.  
  15. -- Level names for pretty printing
  16. local LEVEL_NAMES = {
  17.     [1] = "DEBUG",
  18.     [2] = "INFO",
  19.     [3] = "WARN",
  20.     [4] = "ERROR",
  21.     [5] = "FATAL"
  22. }
  23.  
  24. -- Terminal colors (using ComputerCraft color values)
  25. -- Define these directly to avoid relying on colors API which might not be available
  26. local COLORS = {
  27.     [1] = 8,  -- lightGray
  28.     [2] = 1,  -- white
  29.     [3] = 4,  -- yellow
  30.     [4] = 16, -- red
  31.     [5] = 16  -- red
  32. }
  33.  
  34. -- Create a new logger instance
  35. -- @param options table Configuration options
  36. --   - filename (string): Path to log file
  37. --   - level (number): Minimum log level to record (defaults to DEBUG)
  38. --   - console (boolean): Whether to output to console (defaults to true)
  39. --   - timestamps (boolean): Whether to include timestamps (defaults to true)
  40. -- @return Logger instance
  41. function Logger.create(options)
  42.     options = options or {}
  43.  
  44.     local instance = {}
  45.  
  46.     local filename = options.filename or "app.log"
  47.     local level = options.level or LEVELS.DEBUG
  48.     local console = options.console == true  -- Default to false
  49.     local timestamps = options.timestamps == true  -- Default to false
  50.  
  51.     -- Attempt to open the log file
  52.     local fileMode = options.clean and "w" or "a" -- Use "w" for fresh file, "a" for append
  53.     local file = fs.open(filename, fileMode)
  54.  
  55.     if file then
  56.         file.close()
  57.     else
  58.         print("Warning: Could not open log file: " .. filename)
  59.     end
  60.  
  61.     -- Format a log message
  62.     -- @param logLevel number Log level
  63.     -- @param message string The message to log
  64.     -- @param ... any Additional values to include in the log
  65.     -- @return string Formatted log message
  66.     local function format(logLevel, message, ...)
  67.         local args = {...}
  68.         local prefix = ""
  69.  
  70.         -- Add timestamp if enabled
  71.         if timestamps then
  72.             prefix = os.date("[%Y-%m-%d %H:%M:%S] ")
  73.         end
  74.  
  75.         -- Add level tag
  76.         prefix = prefix .. "[" .. LEVEL_NAMES[logLevel] .. "] "
  77.  
  78.         -- Format the message with any additional arguments
  79.         local result = prefix .. message
  80.         for i = 1, #args do
  81.             result = result .. " " .. tostring(args[i])
  82.         end
  83.  
  84.         return result
  85.     end
  86.  
  87.     -- Write a message to the log file
  88.     -- @param message string The message to write
  89.     local function writeToFile(message)
  90.         local file = fs.open(filename, "a")
  91.  
  92.         if file then
  93.             file.writeLine(message)
  94.             file.close()
  95.         end
  96.     end
  97.  
  98.     -- Log a message at a specific level
  99.     -- @param logLevel number Log level
  100.     -- @param message string The message to log
  101.     -- @param ... any Additional values to include in the log
  102.     instance.log = function(logLevel, message, ...)
  103.         if logLevel >= level then
  104.             local formatted = format(logLevel, message, ...)
  105.  
  106.             -- Write to file
  107.             writeToFile(formatted)
  108.  
  109.             -- Write to console if enabled
  110.             if console then
  111.                 -- Only attempt to use colors on advanced computers
  112.                 if term.isColor() then
  113.                     local oldColor = term.getTextColor()
  114.                     term.setTextColor(COLORS[logLevel])
  115.                     print(formatted)
  116.                     term.setTextColor(oldColor)
  117.                 else
  118.                     -- Regular output for non-advanced computers
  119.                     print(formatted)
  120.                 end
  121.             end
  122.         end
  123.     end
  124.  
  125.     -- Write a line directly to the log file without formatting
  126.     -- @param message string The line to write
  127.     instance.writeLine = function(message)
  128.         writeToFile(message)
  129.  
  130.         if console then
  131.             print(message)
  132.         end
  133.     end
  134.  
  135.     -- Close the logger (currently a no-op, but good for API consistency)
  136.     instance.close = function()
  137.         -- Nothing to do here for now, but could be used for cleanup
  138.     end
  139.  
  140.     -- Convenience methods for different log levels
  141.     instance.debug = function(message, ...)
  142.         return instance.log(LEVELS.DEBUG, message, ...)
  143.     end
  144.  
  145.     instance.info = function(message, ...)
  146.         return instance.log(LEVELS.INFO, message, ...)
  147.     end
  148.  
  149.     instance.warn = function(message, ...)
  150.         return instance.log(LEVELS.WARN, message, ...)
  151.     end
  152.  
  153.     instance.error = function(message, ...)
  154.         return instance.log(LEVELS.ERROR, message, ...)
  155.     end
  156.  
  157.     instance.fatal = function(message, ...)
  158.         return instance.log(LEVELS.FATAL, message, ...)
  159.     end
  160.  
  161.     -- Expose constants
  162.     instance.LEVELS = LEVELS
  163.  
  164.     return instance
  165. end
  166.  
  167. -- Expose constants at module level too
  168. Logger.LEVELS = LEVELS
  169.  
  170. return Logger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement