Skip to content

Commit 1f34198

Browse files
Merge pull request #54 from miguelgfierro/logger
Logger in red
2 parents 4470610 + b5a0f7d commit 1f34198

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

pretty_logger.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Script to display a log file with ERROR lines in red in the linux terminal.
2+
# It displays the latest 200 lines of the log file.
3+
# The log file has to exist.
4+
# It assumes that the errors are logged as for example:
5+
# 2025-03-25 15:10:15 ERROR 'int' object has no attribute 'split'
6+
#
7+
# Usage:
8+
# sh pretty_logger.sh
9+
# sh pretty_logger.sh my_log_file.txt
10+
11+
# Display help
12+
display_help() {
13+
echo "Usage: $(basename "$0") [LOG_FILE] [-h|--help]"
14+
echo
15+
echo "Display a log file with ERROR lines highlighted in red"
16+
echo
17+
echo "Options:"
18+
echo " LOG_FILE Path to log file (default: app.log)"
19+
echo " -h, --help Display this help message and exit"
20+
echo
21+
echo "Example:"
22+
echo " $(basename "$0") # Display app.log"
23+
echo " $(basename "$0") my_logs.log # Display my_logs.log"
24+
exit 0
25+
}
26+
27+
# Check for help flag
28+
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
29+
display_help
30+
fi
31+
32+
# Get the log file from command line argument or use default
33+
LOG_FILE="${1:-app.log}"
34+
35+
# Check if the log file exists
36+
if [ ! -f "$LOG_FILE" ]; then
37+
echo "\033[31mERROR: Log file '$LOG_FILE' does not exist.\033[0m"
38+
echo "Run '$(basename "$0") --help' for usage information."
39+
exit 1
40+
fi
41+
42+
tail -n 200 -f "$LOG_FILE" | awk '/ERROR/{print "\033[31m" $0 "\033[0m"; next} {print $0}'

0 commit comments

Comments
 (0)