Last active
March 27, 2025 07:58
-
-
Save chriskyfung/fbb306311ddfae207664b7570c1f65ef to your computer and use it in GitHub Desktop.
Bash logging tool with quick color-coded log viewing, sanitization, man support, and auto-rotation for Ubuntu.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Comprehensive Log Helper v2.3.0 - Quick View & Man Page Support | |
# Generated by Qwen2.5-Max | |
LOG_FILE="$HOME/admin.log" | |
VALID_LEVELS=("INFO" "WARN" "ERROR") | |
RED='\033[0;31m' | |
YELLOW='\033[1;33m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
VERSION="2.3.0" | |
# Help message function | |
show_help() { | |
echo -e " | |
${GREEN}log_helper v$VERSION - Enhanced system logging utility${NC} | |
Usage: $0 [OPTIONS] LEVEL \"MESSAGE\" | |
${YELLOW}OPTIONS:${NC} | |
-h, --help Display this help message | |
--install-man Install manual page (requires sudo) | |
--version Display version information | |
--view [COUNT] Show last COUNT log entries (default: 10) | |
${YELLOW}LEVELS:${NC} | |
INFO - Informational messages | |
WARN - Warning messages | |
ERROR - Error messages | |
${YELLOW}EXAMPLES:${NC} | |
$0 INFO \"System maintenance complete\" | |
$0 ERROR \"Backup process failed\" | |
$0 --view 5 # Show last 5 entries | |
$0 --install-man # Install manual page | |
Logs are stored in: $LOG_FILE | |
" | |
} | |
# Process special options first | |
case "$1" in | |
-h|--help) | |
show_help | |
exit 0 | |
;; | |
--view) | |
COUNT=${2:-10} | |
if ! [[ "$COUNT" =~ ^[0-9]+$ ]]; then | |
echo -e "${YELLOW}Warning: Invalid count '$2' - Using default 10${NC}" | |
COUNT=10 | |
fi | |
if [ ! -f "$LOG_FILE" ]; then | |
echo -e "${RED}Error: Log file $LOG_FILE does not exist${NC}" | |
exit 1 | |
fi | |
echo -e "${GREEN}Last $COUNT log entries (v$VERSION):${NC}" | |
tail -n "$COUNT" "$LOG_FILE" | while IFS= read -r line; do | |
if [[ "$line" == *"[ERROR]"* ]]; then | |
echo -e "${RED}$line${NC}" | |
elif [[ "$line" == *"[WARN]"* ]]; then | |
echo -e "${YELLOW}$line${NC}" | |
else | |
echo -e "${GREEN}$line${NC}" | |
fi | |
done | |
exit 0 | |
;; | |
--install-man) | |
echo -e "${YELLOW}Installing manual page...${NC}" | |
TMP_MAN=$(mktemp) | |
cat > "$TMP_MAN" << 'EOF' | |
.\" Manpage for log_helper | |
.TH log_helper 1 "March 2024" "v2.3.0" "User Commands" | |
.SH NAME | |
log_helper \- Enhanced system logging utility with validation | |
.SH SYNOPSIS | |
.B log_helper | |
[\fIOPTIONS\fR] \fILEVEL\fR \"\fIMESSAGE\fR\" | |
.SH DESCRIPTION | |
log_helper provides enhanced logging with: | |
.TP | |
- Timestamped entries (ISO-8601) | |
- Log level validation (INFO/WARN/ERROR) | |
- Input sanitization | |
- Color-coded output | |
- Automatic log rotation | |
.SH OPTIONS | |
.TP | |
\fB\-h, \-\-help\fR | |
Display help information | |
.TP | |
\fB\-\-install\-man\fR | |
Install manual page system-wide | |
.TP | |
\fB\-\-version\fR | |
Show version information | |
.TP | |
\fB\-\-view [COUNT]\fR | |
Show last COUNT log entries (default: 10) | |
.SH LEVELS | |
\fBINFO\fR - Regular operational messages | |
\fBWARN\fR - Non-critical issues | |
\fBERROR\fR - Critical errors | |
.SH EXAMPLES | |
.log_helper INFO \"System started\" | |
.log_helper ERROR \"Disk space critical\" | |
.SH FILES | |
$HOME/admin.log - Default log file location | |
.SH AUTHOR | |
Maintained by DevOps Team <[email protected]> | |
.SH REPORTING BUGS | |
Report issues at https://github.com/example/log-helper | |
.SH VERSION | |
2.3.0 | |
EOF | |
gzip -c "$TMP_MAN" | sudo sh -c "cat > /usr/local/share/man/man1/log_helper.1.gz" | |
rm "$TMP_MAN" | |
sudo mandb &>/dev/null | |
echo -e "${GREEN}Manual page installed. Use 'man log_helper'${NC}" | |
exit 0 | |
;; | |
--version) | |
echo "log_helper v$VERSION" | |
exit 0 | |
;; | |
esac | |
# Validate input arguments | |
if [ $# -lt 2 ]; then | |
echo -e "${RED}Error: Invalid arguments${NC}" | |
echo "Usage: $0 [${VALID_LEVELS[*]}] \"Log message\"" | |
echo "Example: $0 INFO \"System maintenance complete\"" | |
echo "Use '$0 --help' for more information." | |
exit 1 | |
fi | |
LOG_LEVEL="$1" | |
ORIGINAL_MESSAGE="$2" | |
TIMESTAMP=$(date --iso-8601=seconds) | |
# Validate log level | |
if [[ ! " ${VALID_LEVELS[*]} " =~ " ${LOG_LEVEL} " ]]; then | |
echo -e "${RED}Error: Invalid log level '${LOG_LEVEL}'${NC}" | |
echo "Valid levels: ${VALID_LEVELS[*]}" | |
exit 1 | |
fi | |
# Message validation and sanitization | |
safe_message() { | |
local input="$1" | |
# 1. Escape backslashes first | |
local sanitized="${input//\\/\\\\}" | |
# 2. Escape backticks | |
sanitized="${sanitized//\`/\\\`}" | |
# 3. Escape double quotes | |
sanitized="${sanitized//\"/\\\"}" | |
# 4. Replace control characters (ASCII 0-31 except \t, \n, \r) | |
sanitized=$(echo "$sanitized" | sed 's/[\x00-\x08\x0B\x0C\x0E-\x1F]//g') | |
# 5. Check for empty message | |
if [ -z "$sanitized" ]; then | |
echo -e "${RED}Error: Log message cannot be empty${NC}" | |
exit 1 | |
fi | |
# Notify if changes were made | |
if [ "$input" != "$sanitized" ]; then | |
echo -e "${YELLOW}Notice: Invalid characters detected and sanitized in message${NC}" | |
fi | |
echo "$sanitized" | |
} | |
MESSAGE=$(safe_message "$ORIGINAL_MESSAGE") | |
# Verify log file accessibility | |
if [ ! -f "${LOG_FILE}" ]; then | |
echo -e "${YELLOW}Notice: Creating new log file at ${LOG_FILE}${NC}" | |
touch "${LOG_FILE}" || { | |
echo -e "${RED}Error: Failed to create log file in home directory${NC}" | |
exit 1 | |
} | |
elif [ ! -w "${LOG_FILE}" ]; then | |
echo -e "${RED}Error: No write permission for ${LOG_FILE}${NC}" | |
exit 1 | |
fi | |
# Format log entry | |
LOG_ENTRY="${TIMESTAMP} [${LOG_LEVEL}] ${MESSAGE}" | |
# Write to log file | |
echo "${LOG_ENTRY}" >> "${LOG_FILE}" || { | |
echo -e "${RED}Error: Failed to write to ${LOG_FILE}${NC}" | |
exit 1 | |
} | |
# Color-coded console output | |
case ${LOG_LEVEL} in | |
INFO) | |
echo -e "${GREEN}${LOG_ENTRY}${NC}" | |
;; | |
WARN) | |
echo -e "${YELLOW}${LOG_ENTRY}${NC}" | |
;; | |
ERROR) | |
echo -e "${RED}${LOG_ENTRY}${NC}" | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
log_helper.sh v2.3.0
- A simple Bash logging utility for Ubuntu--view [COUNT]
to display color-coded recent entries (default: 10)--install-man
New in v2.3.0:
--view
command to display recent logs with color highlightingUsage: