Main Menu

Simple Logging API

Started by ComputerCrafter, May 14, 2025, 01:10 PM

Previous topic - Next topic

ComputerCrafter

After looking at other examples of programs in Minecraft, I decided to make a simple API to make recording data onto files easier and more straightforward.
This API works as a logging system, it saves data to files line by line with timestamps so you can keep records.

Pastebin Code: 4K1gQe1M

This API is not the most efficient or well implemented, however it IS easy to use.

By default, the API outputs this every time you call the function to log something:
[YYYY-MM-DD HH:MM] : EXAMPLE MESSAGE

The table for all the API's functions are here:
filePath(path)path: stringSets the path in which logs will be saved, defaults to ./logFile
setShowProgram(var)var: booleanSets whether program path is included along with timestamp
setShowClearedMessage(var) var: booleanSets whether message is displayed if log is cleared
setCurrentProgram(var) var: stringSets the program name that will be displayed if show program
is set to true using setShowProgram(var)
setClearedMessage(message) message:
string
Sets message that will be displayed if setShowClearedMessage(var)
is set to true
setProgramFormat(pFormat) pFormat:
string
Standard string format, defaults to [%s] where %s is name of the program
setTimestampFormat(tFormat) tFormat:
string
Standard string format, defaults to [%s] where %s is the time according to setDateTimeFormat()
setDateTimeFormat(dFormat) dFormat:
string
Time format for os.date() defaults to %F %R
reset()Reset all values to default values
output(statement)statement:
string
Writes a line to the log file following restrictions above
clear()Clears the log file, writes a message if specified

For example, if you want the timestamps to be: {HH:MM}, then you would call:
log.setTimestampFormat("{%s}")
log.setDateTimeFormat("%R")

With the programName, you can have multiple programs writing to a single log.
For now, the API only supports writing to files, so there aren't live logs or colors. There is no line-wrapping either.

Example file:
os.loadAPI("log.lua")
log.output("Test log sentence")
Example Output in ./logFile:
[2025-15-5 18:23]: Test log sentence


If you have any questions, please reply!
As stated before, this is not the most detailed or sophisticated API for this purpose, but it's easy to use!



"Code is read much more often than it is written."
– Guido Van Rossum, creator of Python

shorun

logging really does make bug hunting easyer, doesn't it :)