Skip to content

feat(builtins): add log command (non-standard, structured logging) #563

@chaliy

Description

@chaliy

Summary

A new, non-standard builtin for structured logging with levels, timestamps, and optional JSON output. Every serious script reinvents logging — making it a builtin standardizes the pattern.

Note: This is a bashkit-specific extension, not a standard Bash command.

Proposed Syntax

log <level> <message> [key=value...]

Proposed Levels

Level Description
`debug` Verbose diagnostic info (hidden by default)
`info` Normal operational messages
`warn` Warning conditions
`error` Error conditions

Proposed Flags / Environment

Config Description
`LOG_LEVEL` env var Minimum level to display (default: `info`)
`LOG_FORMAT` env var `text` (default) or `json`
`LOG_TIMESTAMP` env var `true`/`false` — include timestamp (default: true)

Use Cases

# Basic logging
log info "Starting deployment"
log warn "Config file not found, using defaults"
log error "Failed to connect to database"

# With structured key-value pairs
log info "Request completed" status=200 duration=1.5s endpoint=/api/users
log error "Query failed" table=users query="SELECT *" err="timeout"

# Control level via environment
export LOG_LEVEL=debug
log debug "Verbose tracing info"

# JSON output for machine parsing
export LOG_FORMAT=json
log info "Deploy complete" version=1.2.3 env=production
# {"level":"info","msg":"Deploy complete","version":"1.2.3","env":"production","ts":"2024-01-15T10:30:00Z"}

# In scripts
deploy() {
    log info "Deploying version $1" version="$1"
    if ! ./build.sh; then
        log error "Build failed" version="$1"
        return 1
    fi
    log info "Deploy successful" version="$1"
}

Text Output Format

2024-01-15 10:30:00 [INFO]  Starting deployment
2024-01-15 10:30:01 [WARN]  Config file not found, using defaults
2024-01-15 10:30:02 [ERROR] Failed to connect to database

Implementation Notes

  • All output goes to stderr (stdout is for data)
  • Level filtering: messages below LOG_LEVEL are silently dropped
  • Timestamps use interpreter's virtual time (not host time, per security spec)
  • Key-value pairs are appended after the message
  • JSON format uses serde_json for proper escaping
  • Color output when stderr is a terminal: debug=gray, info=green, warn=yellow, error=red
  • No color in bashkit's virtual environment (no real terminal), but the format supports it for realfs mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions