POSIX.1-2017 - 2001 & CLI guidelines compliant logger
- semantic log coloring to
stderr
- follows NO_COLOR, FORCE_COLOR,
DEBUG
,QUIET
,JSON
andLOG_LEVEL
conventions.
. ./logsh/log.sh
log_info "bootstrapping..."
log_warn "fallback default"
log_error "failed to update"
log_success "backup completed"
get log.sh
curl -s "https://raw.githubusercontent.com/nicholaswmin/logsh/main/log.sh" -o log.sh
and source it:
. ./logsh/log.sh
# Log different types of messages
log_info "an informational message"
log_debug "a debug message (hidden by default)"
log_warn "a warning message"
log_error "an error message"
log_success "a success message"
# Or use a custom level
log "CUSTOM" "a custom level message"
use environment variables for configuration:
Variable | Description | Default |
---|---|---|
LOG_LEVEL |
Filter by level (ERROR, WARN, INFO) | INFO |
DEBUG=1 |
Show debug output with level prefixes | - |
QUIET=1 |
Suppress all non-error messages | - |
NO_COLOR=1 |
Disable colored output | - |
FORCE_COLOR=1 |
Force colors in non-terminal envs. | - |
JSON=1 |
Output logs in JSON format | - |
colors can be customised for each log level:
# Example: Use custom colors
LOGCOL_INFO=$(tput setaf 7) # White
LOGCOL_DEBUG=$(tput dim) # Dim
LOGCOL_WARN=$(tput setaf 3) # Yellow
LOGCOL_ERROR=$(tput setaf 1) # Red
LOGCOL_SUCCESS=$(tput setaf 2) # Green
LOGCOL_RESET=$(tput sgr0) # Reset
note: this increases performance by avoiding
tput
calls
. ./logsh/log.sh
log_info "bootstrapping..."
log_warn "config. file not found, fallback to defaults"
log_error "failed to update"
log_success "backup completed"
JSON=1 ./script-using-log.sh
Outputs logs in JSON format suitable for parsing:
{ "level":"info","message":"bootstrapping..." }
{ "level":"warn","message":"fallback default" }
See cli.sh for a complete CLI example
While not built-in, you can wrap it like so:
logt_info() { log_info "$(date '+%Y-%m-%d %H:%M:%S') - $*"; }
# also wrap: debug, warn ...
requires bats-core:
brew install bats-core
run the tests:
bats test
# install: brew install shellcheck
shellcheck --shell=sh log.sh
MIT-0 License
Copyright (c) 2025 @nicholaswmin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.