Misc.Logger module

For logging debug information to files.

Usage

local Logger = require('__stdlib__/stdlib/misc/logger')
-- or to create a new logger directly:
local Log = require('__stdlib__/stdlib/misc/logger').new()
-- log files are saved to script-output/modname/log.log by default

Functions

get(...) Get a saved log or create a new one if there is no saved log.
new([log_name='log'][, debug_mode=false][, options={...}]) Creates a new logger object.
Log.log(msg) Logs a message.
Log.write() Writes out all buffered messages immediately.

Tables

Log.options Used in the new function for logging game ticks, specifying logfile extension, or forcing the logs to append to the end of the logfile.

Functions

# get(...)

Get a saved log or create a new one if there is no saved log.

Parameters:
  • ...
# new([log_name='log'][, debug_mode=false][, options={...}])

Creates a new logger object.

In debug mode, the logger writes to file immediately, otherwise the logger buffers the lines.

The logger flushes the logged messages every 60 seconds since the last message.

A table of options may be specified when creating a logger.

Parameters:
  • log_name : (string) the name of the logger (default: 'log')
  • debug_mode : (boolean) toggles the debug state of logger (default: false)
  • options : (options) a table with optional arguments (default: {...})
Returns:
  • (Logger) the logger instance
Usage:
Log = Logger.new()
Log("this msg will be logged in /script-output/YourModName/log.log!")
 -- Immediately Write everything buffered in the log file
 Log()
Log = Logger.new('test', true)
Log("this msg will be logged and written immediately in /script-output/YourModName/test.log!")
Log = Logger.new('cool_mod_name', 'test', true, { file_extension = data })
Log("this msg will be logged and written immediately in /script-output/YourModName/test.data!")
# Log.log(msg)

Logs a message.

Parameters:
  • msg : (string or table) the message to log. tables will be dumped using serpent which is included in the official Factorio Lualib
Returns:
  • (Logger) the logger instance
See also:
# Log.write()

Writes out all buffered messages immediately.

Returns:
  • (Logger) the logger instance

Tables

# Log.options

Used in the new function for logging game ticks, specifying logfile extension, or forcing the logs to append to the end of the logfile.

Fields:
  • log_ticks : (boolean) whether to include the game tick timestamp in the logs (default: false)
  • file_extension : (string) a string that overrides the default logfile extension (default: "log")
  • force_append : (boolean) if true, every new message appends to the current logfile instead of creating a new one (default: false)