Log Module
The log module provides logging functions for Lua scripts, allowing messages to be recorded with different severity levels. This module is useful for debugging, monitoring, and auditing scripts in production.
Key features:
-
5 log levels: debug, info, warn, error
-
Identification by execution context
-
Automatic formatting of Lua values
-
Support for multiple arguments with tab-separated output
Available Functions
Section titled “Available Functions”1. log.set_ident(identificador)
Section titled “1. log.set_ident(identificador)”Sets an identifier for logs generated by the current script.
Parameters:
Section titled “Parameters:”- identificador (string): Unique identifier for the execution context
Return:
Section titled “Return:”- nil: The function does not return a value
Behavior:
Section titled “Behavior:”-
The identifier is stored internally and persists for the duration of the script execution
-
All subsequent log messages will include this identifier
-
Useful to distinguish logs from different scripts or instances
Example Usage:
Section titled “Example Usage:”-- Set identifier for a specific scriptlog.set_ident("monitoramento-cpu")
-- Now all logs will include "[monitoramento-cpu]"log.info("Iniciando monitoramento")-- Output: [lua] [monitoramento-cpu] Starting monitoring
-- In another script or contextlog.set_ident("backup-automatico")log.info("Iniciando backup")-- Output: [lua] [backup-automatico] Starting backup2. log.debug(...)
Section titled “2. log.debug(...)”Logs DEBUG-level messages for detailed debugging information.
Parameters:
Section titled “Parameters:”- … (multiple values): Values to be logged, separated by tabs
Return:
Section titled “Return:”- nil: The function does not return a value
Example Usage:
Section titled “Example Usage:”-- Log variable values for debugginglocal temperatura = 45.6local uso_memoria = 78.3log.debug("Variáveis de sistema:", "Temp:", temperatura, "Mem:", uso_memoria)-- Output: [lua] [ident] System variables: Temp: 45.6 Mem: 78.3
-- Debug execution flowlog.debug("Entrando na função processar_dados")log.debug("Parâmetros recebidos:", parametros)log.debug("Configuração atual:", config)
-- Debug complex structureslocal dados = { usuario = "admin", acao = "login", timestamp = os.time()}log.debug("Dados da requisição:", dados)3. log.info(...)
Section titled “3. log.info(...)”Logs INFO-level messages for general execution information.
Parameters:
Section titled “Parameters:”- … (multiple values): Values to be logged, separated by tabs
Return:
Section titled “Return:”- nil: The function does not return a value
4. log.warn(...)
Section titled “4. log.warn(...)”Logs WARN-level messages for situations that require attention but are not errors.
Parameters:
Section titled “Parameters:”- … (multiple values): Values to be logged, separated by tabs
Return:
Section titled “Return:”- nil: The function does not return a value
5. log.error(...)
Section titled “5. log.error(...)”Logs ERROR-level messages for error conditions that require intervention.
Parameters:
Section titled “Parameters:”- … (multiple values): Values to be logged, separated by tabs
Return:
Section titled “Return:”- nil: The function does not return a value