Logger module

For logging debug information to files.

Usage

local Logger = require('stdlib/log/logger')
-- or to create a logger directly:
local LOGGER = require('stdlib/log/logger').new(...)
-- and to use the same LOGGER in multiple require files make it global by removing 'local'.

Functions

log (msg) Logs a message.
new (mod_name[, log_name='main'][, debug_mode=false][, options={...}]) Creates a new logger object.
write () Writes out all buffered messages immediately.

Tables

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

# 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:
  • (boolean) true if the message was written, false if it was queued for a later write
See also:
# new (mod_name[, log_name='main'][, 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:
  • mod_name : (string) [required] the name of the mod to create the logger for
  • log_name : (string) the name of the logger (default: 'main')
  • 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:
LOGGER = Logger.new('cool_mod_name')
LOGGER.log("this msg will be logged!")
LOGGER = Logger.new('cool_mod_name', 'test', true)
LOGGER.log("this msg will be logged and written immediately in test.log!")
LOGGER = Logger.new('cool_mod_name', 'test', true, { file_extension = data })
LOGGER.log("this msg will be logged and written immediately in test.data!")
# write ()

Writes out all buffered messages immediately.

Returns:
  • (boolean) true if write was successful, false otherwise

Tables

# 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)