|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Bash Library - Logging Module |
| 4 | +# Version: 1.0.0 |
| 5 | +# Description: Provides logging functions for bash scripts |
| 6 | + |
| 7 | +# Log levels |
| 8 | +readonly LOG_LEVEL_DEBUG=0 |
| 9 | +readonly LOG_LEVEL_INFO=1 |
| 10 | +readonly LOG_LEVEL_WARN=2 |
| 11 | +readonly LOG_LEVEL_ERROR=3 |
| 12 | + |
| 13 | +# Default log level |
| 14 | +LOG_LEVEL=${LOG_LEVEL:-$LOG_LEVEL_INFO} |
| 15 | + |
| 16 | +# Function: lib_log_header_starting |
| 17 | +# Description: Logs the start of a process with a header |
| 18 | +# Usage: lib_log_header_starting "Process Name" |
| 19 | +# Returns: 0 on success |
| 20 | +lib_log_header_starting() { |
| 21 | + if [ $# -ne 1 ]; then |
| 22 | + echo "Error: lib_log_header_starting requires exactly one argument" >&2 |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + |
| 26 | + echo "============================================================================" |
| 27 | + echo "$1 starting..." |
| 28 | + echo "============================================================================" |
| 29 | + return 0 |
| 30 | +} |
| 31 | + |
| 32 | +# Function: lib_log_header_done |
| 33 | +# Description: Logs the completion of a process with a header |
| 34 | +# Usage: lib_log_header_done "Process Name" |
| 35 | +# Returns: 0 on success |
| 36 | +lib_log_header_done() { |
| 37 | + if [ $# -ne 1 ]; then |
| 38 | + echo "Error: lib_log_header_done requires exactly one argument" >&2 |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + echo "============================================================================" |
| 43 | + echo "done. ($1)" |
| 44 | + echo "============================================================================" |
| 45 | + return 0 |
| 46 | +} |
| 47 | + |
| 48 | +# Function: lib_log_info |
| 49 | +# Description: Logs an info message |
| 50 | +# Usage: lib_log_info "Message" |
| 51 | +# Returns: 0 on success |
| 52 | +lib_log_info() { |
| 53 | + if [ $# -ne 1 ]; then |
| 54 | + echo "Error: lib_log_info requires exactly one argument" >&2 |
| 55 | + return 1 |
| 56 | + fi |
| 57 | + |
| 58 | + if [ $LOG_LEVEL -le $LOG_LEVEL_INFO ]; then |
| 59 | + echo "[INFO] $1" |
| 60 | + fi |
| 61 | + return 0 |
| 62 | +} |
| 63 | + |
| 64 | +# Function: lib_log_error |
| 65 | +# Description: Logs an error message |
| 66 | +# Usage: lib_log_error "Message" |
| 67 | +# Returns: 0 on success |
| 68 | +lib_log_error() { |
| 69 | + if [ $# -ne 1 ]; then |
| 70 | + echo "Error: lib_log_error requires exactly one argument" >&2 |
| 71 | + return 1 |
| 72 | + fi |
| 73 | + |
| 74 | + echo "[ERROR] $1" >&2 |
| 75 | + return 0 |
| 76 | +} |
0 commit comments