logging
An logging library for Computer Craft
Usage
Example:
local logging = require("logging") logging:info("Message logged with logging library")
Example with logger:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) logger:info("Message logged with logging library using a logger")
debug(...) | logs a message at the debug level |
---|---|
info(...) | logs a message at the info level |
warn(...) | logs a message at the warn level |
error(...) | logs a message at the error level |
critical(...) | logs a message at the critical level |
log(level, ...) | logs a message |
levels | Hols the levels of the root logger |
Level | Holds information of an log level. |
Record | Holds information of the message that is to be logged. |
Formatter | Used to format a Record instance to a string. |
Logger | Holds all functions related to logging. |
TerminalHandler | An Handler that writes to the terminal. |
FileHandler | An FileHandler that writes to a file. |
WebsocketHandler | Will send the formatted record message to a websocket. |
ColordWebsocketHandler | Will send the formatted record message with 24-bit/true Colors in ansi escape codes to a websocket. |
RawWebsocketHandler | Will send the record with the used formatter to a websocket. |
RednetHandler | broadcast the formatted record message with rednet. |
RednetReciever | An Reciever that recives messages from Rednet. |
- debug(...)Source
logs a message at the debug level
Parameters
- ... string The arguments to log
- info(...)Source
logs a message at the info level
Parameters
- ... string The arguments to log
- warn(...)Source
logs a message at the warn level
Parameters
- ... string The arguments to log
- error(...)Source
logs a message at the error level
Parameters
- ... string The arguments to log
- critical(...)Source
logs a message at the critical level
Parameters
- ... string The arguments to log
- log(level, ...)Source
logs a message
Parameters
- levelsSource
Hols the levels of the root logger
- LevelSource
Holds information of an log level.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local level = logging.Level.new { name = "My Logger", textcolor = colors.orange, backgroundcolor = colors.blue } logger:log(level, "This message will be logged with your custom level")
- RecordSource
Holds information of the message that is to be logged.
- FormatterSource
Used to format a Record instance to a string.
Usage
Example:
local logging = require("logging") local formatter = logging.Formatter.new( "[%(time) %(name) %(levelname)] %(message)", "%Y-%m-%d %H:%M:%S" ) local logger = logging.Logger.new { name = shell.getRunningProgram(), formatter = formatter } logger:info("This message will be logged with your custom formatter")
- LoggerSource
Holds all functions related to logging.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) logger:info("This message will be logged")
- TerminalHandlerSource
An Handler that writes to the terminal.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) -- the logger will have a TerminalHandler by default -- but if you want to add another one you can do it like this: -- local terminalHandler = logging.TerminalHandler.new(logger.formatter) -- logger:addHandler(terminalHandler) logger:info("This message will be sent to the terminal")
- FileHandlerSource
An FileHandler that writes to a file.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local file = fs.open("example.log", "a") -- replace "example.log" with the path of your log file local fileHandler = logging.FileHandler.new(logger.formatter, file) logger:addHandler(fileHandler) logger:info("This message will be sent to the file") file.close() -- don't forget to close the file after your script has finished
- WebsocketHandlerSource
Will send the formatted record message to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local websocketHandler = logging.WebsocketHandler.new(logger.formatter, websocket) logger:addHandler(websocketHandler) logger:info("This message will be sent to the websocket") websocket.close() -- don't forget to close the websocket after your script has finished
- ColordWebsocketHandlerSource
Will send the formatted record message with 24-bit/true Colors in ansi escape codes to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local colordWebsocketHandler = logging.ColordWebsocketHandler.new(logger.formatter, websocket) logger:addHandler(colordWebsocketHandler) logger:info("This message will be sent to the websocket with its color in ansi escape codes") websocket.close() -- don't forget to close the websocket after your script has finished
- RawWebsocketHandlerSource
Will send the record with the used formatter to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local rawWebsocketHandler = logging.RawWebsocketHandler.new(logger.formatter, websocket) logger:addHandler(rawWebsocketHandler) logger:info("This message will be sent to the websocket in raw format") websocket.close() -- don't forget to close the websocket after your script has finished
- RednetHandlerSource
broadcast the formatted record message with rednet.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) rednet.open("right") -- replace "right" with the side your modem is connected to local rednetHandler = logging.RednetHandler.new(logger.formatter) logger:addHandler(rednetHandler) logger:info("This message will be send over rednet")
- RednetRecieverSource
An Reciever that recives messages from Rednet.
Usage
Example limited to a single channel and no background process:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local rednetReciever = logging.RednetReciever.new() while true do rednetReciever:receive(logger) end
Example with coroutine's:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local rednetReciever = logging.RednetReciever.new() logger:addReciever(rednetReciever) -- the main loop can be modified to do all kinds of stuff. local main_loop = coroutine.create(function() while true do logger:info("im very useful") sleep(5) end end) local coroutines = logger:getRecieverCoroutines() table.insert(coroutines, main_loop) while true do local tEventData = table.pack(os.pullEventRaw()) for _, c in pairs(coroutines) do coroutine.resume(c, table.unpack(tEventData)) end end
Types
ColordWebsocketHandler
Will send the formatted record message with 24-bit/true Colors in ansi escape codes to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local colordWebsocketHandler = logging.ColordWebsocketHandler.new(logger.formatter, websocket) logger:addHandler(colordWebsocketHandler) logger:info("This message will be sent to the websocket with its color in ansi escape codes") websocket.close() -- don't forget to close the websocket after your script has finished
- ColordWebsocketHandler.new(formatter, websocket)Source
Create's a new ColordWebsocketHandler instance.
Parameters
Returns
- ColordWebsocketHandler instance
- ColordWebsocketHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.
FileHandler
An FileHandler that writes to a file.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local file = fs.open("example.log", "a") -- replace "example.log" with the path of your log file local fileHandler = logging.FileHandler.new(logger.formatter, file) logger:addHandler(fileHandler) logger:info("This message will be sent to the file") file.close() -- don't forget to close the file after your script has finished
- FileHandler.new(formatter, file)Source
Create's a new FileHandler instance.
Parameters
- formatter Formatter The formatter to use.
- file table The file to write to. (use fs.open to open a file)
Returns
- FileHandler instance
- FileHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.
Formatter
Used to format a Record instance to a string.
Usage
Example:
local logging = require("logging") local formatter = logging.Formatter.new( "[%(time) %(name) %(levelname)] %(message)", "%Y-%m-%d %H:%M:%S" ) local logger = logging.Logger.new { name = shell.getRunningProgram(), formatter = formatter } logger:info("This message will be logged with your custom formatter")
- Formatter.fmtSource
The format string used to format the record. defaults to
[%(time) %(name) %(levelname)] %(message)
- placeholders:
%(name)
Name of the logger%(levelname)
Name of the Log level%(message)
The Message to log%(time)
The real live time formatted by datefmt%(localtime)
The minecraft time formatted by datefmt%(day)
The minecraft day (os.day())%(computerid)
The ID of the computer (os.getComputerID())%(computerlabel)
The label of the computer (os.getComputerLabel())%(thread)
The memory address of the current thread
- placeholders:
- Formatter.datefmt = "%H:%M:%S"Source
The date format, it works like the format in os.date()
- Formatter.new(fmt, datefmt)Source
Create's a new Formatter instance.
Parameters
Returns
- Formatter instance
- Formatter.format(record)Source
Formats a Record instance to a string.
Parameters
- record Record The record to format
Returns
- string The formatted string
Level
Holds information of an log level.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local level = logging.Level.new { name = "My Logger", textcolor = colors.orange, backgroundcolor = colors.blue } logger:log(level, "This message will be logged with your custom level")
- Level.level = 0Source
Holds the log level.
- Level.textcolorSource
Holds the textcolor of the level defaults to colors.white
- Level.backgroundcolorSource
Holds the backgroundcolor of the level defaults to colors.black
- Level.new(name, level, textcolor, backgroundcolor)Source
Create's a new Level instance.
Parameters
- name string The name of the level
- level number The level weight (optional, defaults to 0)
- textcolor number The textcolor of the level (optional, defaults to colors.white)
- backgroundcolor number The backgroundcolor of the level (optional, defaults to colors.black)
Returns
- Level instance
Logger
Holds all functions related to logging.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) logger:info("This message will be logged")
- Logger.level = 0Source
The level that the logger will log at.
- Logger.recieversSource
Holds all recievers
- Logger.new(name, level, formatter)Source
Create's a new Logger instance.
Parameters
Returns
- Logger instance
- Logger.addHandler(handler)Source
Adds a new handler to the logger.
Parameters
- handler table The handler to add.
- Logger.removeHandler(handler)Source
Removes the given handler from the logger. dont forget to close files, websockets, etc.
Parameters
- handler table The handler to remove.
- Logger.registerLevel(level)Source
This function adds a level to the internal levels list of the logger. currently this function is useless since you dont need to add levels for them to work.
Parameters
- level Level The level.
- Logger:removeDefaultHandler()Source
Removes the default TerminalHandler from the logger.
- Logger.handel(record)Source
Handels a record.
Parameters
- record Record The record to handle.
- Logger.log(level, ...)Source
logs a message
Parameters
- Logger.addReciever(reciever)Source
add a reciever to the logger
Parameters
- reciever table the reciever to add
- Logger.getRecieverCoroutines()Source
Returns coroutines for all recievers that recive messages.
Returns
- table coroutines
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) -- in this example we have a reciever that recives messages from rednet local rednetReciever = logging.RednetReciever.new() logger:addReciever(rednetReciever) -- the main loop can be modified to do all kinds of stuff. local main_loop = coroutine.create(function() while true do logger:info("im very useful") sleep(5) end end) local coroutines = logger:getRecieverCoroutines() table.insert(coroutines, main_loop) while true do local tEventData = table.pack(os.pullEventRaw()) for _, c in pairs(coroutines) do coroutine.resume(c, table.unpack(tEventData)) end end
- Logger.debug(...)Source
logs a message at the debug level
Parameters
- ... string The arguments to log
- Logger.info(...)Source
logs a message at the info level
Parameters
- ... string The arguments to log
- Logger.warn(...)Source
logs a message at the warn level
Parameters
- ... string The arguments to log
- Logger.error(...)Source
logs a message at the error level
Parameters
- ... string The arguments to log
- Logger.critical(...)Source
logs a message at the critical level
Parameters
- ... string The arguments to log
RawWebsocketHandler
Will send the record with the used formatter to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local rawWebsocketHandler = logging.RawWebsocketHandler.new(logger.formatter, websocket) logger:addHandler(rawWebsocketHandler) logger:info("This message will be sent to the websocket in raw format") websocket.close() -- don't forget to close the websocket after your script has finished
- RawWebsocketHandler.new(formatter, websocket)Source
Create's a new RawWebsocketHandler instance.
Parameters
Returns
- RawWebsocketHandler instance
- RawWebsocketHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.
Record
Holds information of the message that is to be logged.
- Record.new(level, message, name)Source
Create's a new Record instance.
Parameters
- level Level The level of the record
- message number The message to be logged
- name number The name of the used logger
Returns
- Record instance
RednetHandler
broadcast the formatted record message with rednet.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) rednet.open("right") -- replace "right" with the side your modem is connected to local rednetHandler = logging.RednetHandler.new(logger.formatter) logger:addHandler(rednetHandler) logger:info("This message will be send over rednet")
- RednetHandler.channelSource
Holds the channel where the message is to be send defaults to rednet.CHANNEL_BROADCAST
- RednetHandler.protocol = "logging"Source
Holds the rednet protocol that should be used
- RednetHandler.new(formatter, channel, protocol)Source
Create's a new RednetHandler instance.
Parameters
- formatter Formatter The formatter to use
- channel number or the computer ID to send the message to (optional, defaults to rednet.CHANNEL_BROADCAST)
- protocol string The protocol to use (optional, defaults to "logging")
Returns
- RednetHandler instance
- RednetHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.
RednetReciever
An Reciever that recives messages from Rednet.
Usage
Example limited to a single channel and no background process:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local rednetReciever = logging.RednetReciever.new() while true do rednetReciever:receive(logger) end
Example with coroutine's:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local rednetReciever = logging.RednetReciever.new() logger:addReciever(rednetReciever) -- the main loop can be modified to do all kinds of stuff. local main_loop = coroutine.create(function() while true do logger:info("im very useful") sleep(5) end end) local coroutines = logger:getRecieverCoroutines() table.insert(coroutines, main_loop) while true do local tEventData = table.pack(os.pullEventRaw()) for _, c in pairs(coroutines) do coroutine.resume(c, table.unpack(tEventData)) end end
- RednetReciever.protocol = "logging"Source
Holds the rednet protocol that should be used
- RednetReciever.new(protocol)Source
Create's a new RednetReciever instance.
Parameters
- protocol string The protocol to listen to. (optional, defaults to "logging")
Returns
- RednetReciever instance
- RednetReciever.receive(logger)Source
Recives message from rednet.
Parameters
- logger Logger The logger to use.
TerminalHandler
An Handler that writes to the terminal.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) -- the logger will have a TerminalHandler by default -- but if you want to add another one you can do it like this: -- local terminalHandler = logging.TerminalHandler.new(logger.formatter) -- logger:addHandler(terminalHandler) logger:info("This message will be sent to the terminal")
- TerminalHandler.new(formatter)Source
Create's a new TerminalHandler instance.
Parameters
- formatter Formatter The formatter to use.
Returns
- TerminalHandler instance
- TerminalHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.
WebsocketHandler
Will send the formatted record message to a websocket.
Usage
Example:
local logging = require("logging") local logger = logging.Logger.new(shell.getRunningProgram()) local websocket = http.websocket("ws://localhost:8080/") -- replace "localhost:8080" with the address of your websocket server local websocketHandler = logging.WebsocketHandler.new(logger.formatter, websocket) logger:addHandler(websocketHandler) logger:info("This message will be sent to the websocket") websocket.close() -- don't forget to close the websocket after your script has finished
- WebsocketHandler.new(formatter, websocket)Source
Create's a new WebsocketHandler instance.
Parameters
Returns
- WebsocketHandler instance
- WebsocketHandler.handle(record)Source
Handles a record.
Parameters
- record Record The record to handle.