https://github.com/trallnag/logsh
Simple POSIX-compliant logging for Shell scripts
https://github.com/trallnag/logsh
bash logging posix shell
Last synced: 4 months ago
JSON representation
Simple POSIX-compliant logging for Shell scripts
- Host: GitHub
- URL: https://github.com/trallnag/logsh
- Owner: trallnag
- License: isc
- Created: 2021-08-09T21:23:49.000Z (almost 5 years ago)
- Default Branch: trunk
- Last Pushed: 2023-05-01T10:57:37.000Z (about 3 years ago)
- Last Synced: 2025-10-08T20:30:47.968Z (9 months ago)
- Topics: bash, logging, posix, shell
- Language: Shell
- Homepage:
- Size: 618 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/trallnag/logsh/actions/workflows/primary.yaml)
# Logsh
Minimal POSIX compliant logging contained in a single Shell script made to be
sourced in every file you want to use the library's functions.

## Setup
Download [`log.sh`](log.sh) to whatever location you want to use the library. So
usually right next to the script from where `log.sh` is sourced from.
Alternatively, use [`update-log.sh`](update-log.sh) to download `log.sh`. It can
be used to download and place `log.sh` to the directory where `update-log.sh` is
located at. The script also allows downloading a specific commit reference.
Usually you will end up with many `log.sh` scattered around different places.
## Usage
Source `log.sh` from within scripts where you want to use it with a block like
this:
```sh
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
. "$script_dir/log.sh"
```
Now several functions for logging will be available.
- logdebug
- loginfo
- logsuccess
- logwarning
- logerror
- logexit
To find more information about the inner workings of `log.sh` or how to
configure it with environment variables, check out the script itself.
## Scope
In:
- Set of logging functions that are sourced from a single self-contained file.
- POSIX-compliant and only using features that are supported by `sh`.
- Configuration via environment variables.
Out:
- Being a "real" logging framework.
## Alternative
Just use this block of code:
```shell
_log() { printf '%b[%s] %s%b\n' "$3" "$2" "$1" '\033[0m' >&2; }
logexit() { _log "$1" "ERROR" '\033[31m'; exit 1; }
logerror() { _log "$1" "ERROR" '\033[31m'; }
logwarning() { _log "$1" "WARNING" '\033[33m'; }
logsuccess() { _log "$1" "SUCCESS" '\033[32m'; }
loginfo() { _log "$1" "INFO" '\033[34m'; }
logdebug() { _log "$1" "DEBUG" '\033[36m'; }
```