function::log - Linux


Overview

function::log provides a concise and standardized syntax for logging messages from scripts, applications, or the command line. It offers customizable logging levels, automatic timestamps, and a consistent format, simplifying log management and analysis.

Syntax

function::log <LEVEL> <MESSAGE> [DATA]

Required Arguments

  • LEVEL: The logging level, case-insensitive, can be:

    • debug: Minimal impact, verbose details for debugging
    • info: Informational messages, generally useful for day-to-day operations
    • error: Error conditions, generally indicating problems that require attention
    • fatal: Critical failures or unrecoverable errors
  • MESSAGE: The log message to be recorded.

Optional Arguments

  • DATA: Additional data to include in the log message. This can be any string or complex data structure.

Options/Flags

None.

Examples

# Log a debug message
function::log debug "Starting script execution"

# Log an info message with additional data
function::log info "User logged in" "username=example"

# Log an error message
function::log error "Database connection failed"

Common Issues

  • No output: Ensure the specified LEVEL is among the supported values.
  • Incorrect format: Always use the specified syntax, with LEVEL, MESSAGE, and optional DATA in the correct order.

Integration

function::log can be integrated with other commands and tools as follows:

# Pipe log messages to a file
function::log info "Message to log" | tee -a log.txt

# Capture log messages to a variable
log_output=$(function::log debug "Command output")

Related Commands

  • logger: Traditional logging utility
  • syslog-ng: Advanced logging and monitoring tool