Skip to main content
  1. Lumesh Document/
  2. Lumesh Libs/Modules/

Lumesh Log Module

694 words·4 mins
Table of Contents

The Log module provides structured logging capabilities, supporting various log levels, colored output, and thread-safe level management. All functions support pipeline operations and provide a unified error handling mechanism.

Function Overview
#

Function CategoryMain FunctionsPurpose
Log Level Managementset_level, get_level, disable, enabledControl log output levels
Logginginfo, warn, debug, error, traceLog output at different levels
Raw OutputechoDirect output without formatting
Level Constantslevel.none, level.error, level.warn, level.info, level.debug, level.traceLog level constants

Log Level Constants
#

The Log module provides a complete set of log level constants:

level - Log level constant mapping

  • Includes: none (0), error (1), warn (2), info (3), debug (4), trace (5)
  • Example:
    1Log.set_level Log.level.debug
    2# Set log level to DEBUG
    3
    4Log.set_level 3
    5# Directly set to INFO level using a number

Log Level Management Functions
#

These functions are used to control the log output level:

set_level <level> - Set the log level

  • Parameters: level (required): Integer - Log level (0-5)
  • Returns: None
  • Example:
    1Log.set_level Log.level.warn
    2# Only show WARN and above level logs
    3
    4Log.set_level 1
    5# Only show ERROR level logs

get_level - Get the current log level

  • Parameters: None
  • Returns: Integer - Current log level
  • Example:
    1Log.get_level
    2# Returns: 3 (default INFO level)
    3
    4let current_level = Log.get_level

disable - Disable all log output

  • Parameters: None
  • Returns: None
  • Example:
    1Log.disable
    2# Disable all log output
    3
    4Log.info "This message will not be displayed"

enabled <level> - Check if the specified level is enabled

  • Parameters: level (required): Integer - Log level to check
  • Returns: Boolean - Whether the level is enabled
  • Example:
    1Log.enabled Log.level.debug
    2# Returns: true/false
    3
    4if (Log.enabled 4) { Log.debug "Debug information" }

Logging Functions
#

These functions are used to output log messages at different levels:

info <message>... - Log an info level message

  • Parameters: message (required): Any - Message to log (supports multiple parameters)
  • Returns: None
  • Color: Green [INFO] prefix
  • Example:
    1Log.info "Application started successfully"
    2# Output: [INFO] Application started successfully
    3
    4Log.info "User" $username "logged in successfully"
    5# Output: [INFO] User alice logged in successfully

warn <message>... - Log a warning level message

  • Parameters: message (required): Any - Message to log (supports multiple parameters)
  • Returns: None
  • Color: Yellow [WARN] prefix
  • Example:
    1Log.warn "Disk space is low"
    2# Output: [WARN] Disk space is low
    3
    4Log.warn "Configuration file" $config_file "does not exist, using default configuration"

debug <message>... - Log a debug level message

  • Parameters: message (required): Any - Message to log (supports multiple parameters)
  • Returns: None
  • Color: Blue [DEBUG] prefix
  • Example:
    1Log.debug "Processing request" $request_id
    2# Output: [DEBUG] Processing request req-12345
    3
    4Log.debug "Variable value:" $var

error <message>... - Log an error level message

  • Parameters: message (required): Any - Message to log (supports multiple parameters)
  • Returns: None
  • Color: Red [ERROR] prefix
  • Example:
    1Log.error "Database connection failed"
    2# Output: [ERROR] Database connection failed
    3
    4Log.error "File" $filename "read failed:" $error_msg

trace <message>... - Log a trace level message

  • Parameters: message (required): Any - Message to log (supports multiple parameters)
  • Returns: None
  • Color: Magenta [TRACE] prefix
  • Example:
    1Log.trace "Entering function process_data"
    2# Output: [TRACE] Entering function process_data
    3
    4Log.trace "Iteration number" $i

Raw Output Functions
#

echo <message>... - Output without formatting

  • Parameters: message (required): Any - Message to output (supports multiple parameters)
  • Returns: None
  • Example:
    1Log.echo "Plain text output, no level prefix"
    2# Output: Plain text output, no level prefix
    3
    4Log.echo "Value1" "Value2" "Value3"
    5# Output: Value1 Value2 Value3

Multi-line Output Support
#

The Log module supports formatted output for multi-line messages:

Example:

1Log.info "Multi-line message:\nFirst line\nSecond line\nThird line"
2# Output:
3# [INFO] Multi-line message:
4#        First line
5#        Second line
6#        Third line

Rules:

  1. The first line displays a colored label.
  2. Subsequent lines are indented to align with the label length.
  3. A newline character is automatically appended at the end.

Notes
#

The Log module is an important component of the Lumesh built-in module system, registered as "Log" in the module registry. This module provides a complete logging solution, supporting colored output, level control, and multi-line formatting. The default log level is INFO (3), which can be dynamically adjusted using the set_level function.


Note: Color effects should be viewed in a terminal that supports ANSI.

Related