Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mharrisb1/logging.sh
A simple Bash script to handle logging with different log levels and colored output.
https://github.com/mharrisb1/logging.sh
bash bash-script logging
Last synced: 27 days ago
JSON representation
A simple Bash script to handle logging with different log levels and colored output.
- Host: GitHub
- URL: https://github.com/mharrisb1/logging.sh
- Owner: mharrisb1
- License: mit
- Created: 2024-12-30T18:45:00.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-12-31T18:06:43.000Z (about 1 month ago)
- Last Synced: 2024-12-31T19:18:14.462Z (about 1 month ago)
- Topics: bash, bash-script, logging
- Language: Shell
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# logging.sh
- [logging.sh](#loggingsh)
- [Usage](#usage)
- [1. Sourcing the Script](#1-sourcing-the-script)
- [2. Setting the Log Level](#2-setting-the-log-level)
- [3. Logging Messages](#3-logging-messages)
- [Examples](#examples)
- [Customization](#customization)
- [Colors](#colors)
- [Log Format](#log-format)A simple Bash script to handle logging with different log levels and colored output.
## Usage
### 1. Sourcing the Script
Before you can use the logging functions, source this script in your Bash script:
```bash
#!/bin/bash# Source the logging script
source /path/to/logging.sh# Now you can use log_debug, log_info, log_warn, log_error, etc.
```### 2. Setting the Log Level
By default, the log level is set to `INFO`. To change it, call:
```
log_set_level DEBUG
```Supported levels are `DEBUG`, `INFO`, `WARN`, and `ERROR`.
### 3. Logging Messages
Use the provided functions to log messages at different severities:
- Debug: `log_debug "This is a debug message"`
- Info: `log_info "This is an info message"`
- Warn: `log_warn "This is a warning message"`
- Error: `log_error "This is an error message"`## Examples
```sh
#!/bin/bash# Source the logging script
source logging.sh# Set log level to DEBUG to see all messages
log_set_level DEBUGlog_debug "This debug message will be shown because log level is DEBUG."
log_info "An informational message."
log_warn "Something might be wrong, but it's not critical yet."
log_error "This is a critical error message."
```Output might look like this:
```sh
[2024-12-30 12:34:56] [DEBUG] This debug message will be shown because log level is DEBUG.
[2024-12-30 12:34:56] [INFO] An informational message.
[2024-12-30 12:34:56] [WARN] Something might be wrong, but it's not critical yet.
[2024-12-30 12:34:56] [ERROR] This is a critical error message.
```## Customization
### Colors
Adjust the color variables if you need a different color scheme:
```
COLOR_DEBUG="\033[0;35m" # Purple, for example
COLOR_INFO="\033[0;36m" # Cyan, for example
# etc.
```### Log Format
Customize the log format by modifying the `log_message()` function. For example, you can change the output to JSON or a different timestamp format.