Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trallnag/logsh
Simple POSIX-compliant logging for Shell scripts
https://github.com/trallnag/logsh
bash logging posix shell
Last synced: about 2 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 (over 3 years ago)
- Default Branch: trunk
- Last Pushed: 2023-05-01T10:57:37.000Z (almost 2 years ago)
- Last Synced: 2024-12-16T01:36:23.479Z (about 2 months ago)
- Topics: bash, logging, posix, shell
- Language: Shell
- Homepage:
- Size: 618 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[![primary](https://github.com/trallnag/logsh/actions/workflows/primary.yaml/badge.svg)](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.![screenshot-console-logsh](images/screenshot-console-logsh.png)
## 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
- logexitTo 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'; }
```