Perfect 👍 You want everything possible with logging in Bash scripting. Here’s a complete breakdown of logging techniques, levels, destinations, and best practices for Bash.
-
Write to stdout
echo "This is a normal log"
-
Write to stderr
echo "This is an error log" >&2
-
Redirect output to a log file
echo "Script started at $(date)" >> script.log
You can define levels (like in programming languages):
DEBUG→ detailed info for debuggingINFO→ general infoWARNING→ non-critical issuesERROR→ failuresCRITICAL→ system-breaking issues
Example function:
log() {
level=$1
shift
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$timestamp] [$level] $*" >> script.log
}
log INFO "Script started"
log ERROR "File not found"
log DEBUG "Variable value is $x"-
Redirect stdout and stderr to a file:
./myscript.sh > script.log 2>&1
-
Append instead of overwrite:
./myscript.sh >> script.log 2>&1
Prevent logs from growing infinitely:
if [ -f script.log ]; then
mv script.log script.log.$(date +%F_%T)
fiMake logs readable:
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
echo -e "${GREEN}[INFO] Script started${NC}"
echo -e "${RED}[ERROR] Something failed${NC}" >&2Use the logger command:
logger "This is a log message from my script"
logger -p local0.error "This is an error log"- These logs go into
/var/log/syslogor/var/log/messagesdepending on the OS.
echo "$(date +"%F %T") [PID $$] INFO: Task started" >> script.loglog_file="script.log"
log() {
local level=$1
shift
echo "$(date +"%F %T") [$level] $*" >> "$log_file"
}
log INFO "Starting script"
log WARN "Low disk space"
log ERROR "Failed to copy file"trap 'echo "$(date +"%F %T") [ERROR] Command failed at line $LINENO" >> script.log' ERR
set -e # exit on errorEnable debug mode:
set -x # show commands before execution
set +x # disable debugRedirect debug logs:
exec 2> debug.log
set -x./myscript.sh >out.log 2>err.logYou can configure system log rotation using /etc/logrotate.d/myscript:
/path/to/script.log {
daily
rotate 7
compress
missingok
notifempty
}
Log both to console and file:
logfile="script.log"
exec > >(tee -a "$logfile") 2>&1
echo "This will go to both console and $logfile"log_json() {
level=$1
shift
echo "{\"timestamp\":\"$(date +%FT%T)\",\"level\":\"$level\",\"message\":\"$*\"}" >> script.json
}
log_json INFO "Starting service"
log_json ERROR "Service failed to start"Send logs to a remote server:
logger -n 192.168.1.100 -P 514 "Remote log test"Keep last 5 log files:
for i in 5 4 3 2 1; do
if [ -f script.log.$i ]; then
mv script.log.$i script.log.$((i+1))
fi
done
[ -f script.log ] && mv script.log script.log.1Capture execution time:
start=$(date +%s)
# some commands
end=$(date +%s)
echo "Execution time: $((end-start)) seconds" >> script.log#!/bin/bash
logfile="script.log"
exec > >(tee -a "$logfile") 2>&1
log() {
level=$1; shift
echo "$(date +"%F %T") [$level] $*"
}
trap 'log ERROR "Command failed at line $LINENO"' ERR
log INFO "Script started"
sleep 1
log WARN "This is a warning"
false # triggers error
log INFO "Script finished"✅ With these techniques, you can:
- Log to file, console, syslog, or remote server
- Use levels (INFO, ERROR, DEBUG)
- Rotate, timestamp, and color logs
- Capture stdout, stderr, and execution trace
Got it 👍 You want detailed knowledge about logger in Linux/Bash.
Here’s a complete breakdown of the logger command and how it’s used for logging.
loggeris a command-line utility in Linux/Unix that sends messages to the system log (syslogorjournalddepending on the system).- Instead of writing logs to a file manually,
loggerhands them off to the system’s logging daemon (rsyslog,syslog-ng, orsystemd-journald). - Useful for Bash scripts to record events in a central log system.
-
By default,
logger "Message"sends logs to syslog under the user facility at notice priority. -
Logs usually appear in:
/var/log/syslog(Debian/Ubuntu)/var/log/messages(RHEL/CentOS)journalctl(systemd systems)
Example:
logger "Backup script started"Check logs:
tail -f /var/log/syslog # Debian/Ubuntu
journalctl -f # systemd-based distroslogger [options] [message]-
-p facility.priority→ Set facility and priority levellogger -p local0.info "Service started" logger -p auth.error "Authentication failed"
Facilities:
auth,authpriv,cron,daemon,kern,lpr,mail,news,syslog,user,uucp,local0–local7Priorities:emerg,alert,crit,err,warning,notice,info,debug -
-t tag→ Add a tag (useful to identify which script logged the message)logger -t backup_script "Backup completed successfully" -
-i→ Include the process ID (PID) in the log entrylogger -i -t backup_script "Started" # Produces: backup_script[12345]: Started
-
-f file→ Log contents of a filelogger -f /var/log/custom_app.log
-
-s→ Also log to stderr (so it shows in terminal)logger -s "This will go to syslog and screen" -
-n hostand-P port→ Send log to a remote syslog serverlogger -n 192.168.1.100 -P 514 "Remote log test" -
-u socket→ Use Unix domain socket instead of networklogger -u /dev/log "Message"
logger "Hello, this is a log from bash"logger -p local0.warning -t myscript "Disk space is low"Log entry will look like:
Sep 13 10:30:22 server myscript: Disk space is low
command_that_might_fail || logger -p user.err -t error_handler "Command failed"#!/bin/bash
logger -t myscript -i "Script started"
if cp /tmp/file /backup/; then
logger -t myscript "Backup successful"
else
logger -p local0.err -t myscript "Backup failed"
fi-
systemd systems:
journalctl -t myscript
-
rsyslog systems:
grep myscript /var/log/syslog # or grep myscript /var/log/messages
-
Structured JSON logging:
logger -t myscript "$(jq -n --arg msg "Process started" --arg time "$(date)" '{message:$msg,timestamp:$time}')" -
Remote centralized logging (SIEM/Splunk/ELK stack):
logger -n logserver.example.com -P 514 -t myscript "User login attempt" -
Custom facility (local0–local7) for script separation:
-
In
/etc/rsyslog.conf:local0.* /var/log/myscript.log -
Restart rsyslog:
systemctl restart rsyslog -
Script:
logger -p local0.info -t myscript "Custom log file test"
-
- Logs go into centralized system logs → easier monitoring.
- Supports log levels, facilities, and tags.
- Works with log rotation automatically (via syslog/journald).
- Can log to remote servers for central monitoring.
- Adds timestamps and hostnames automatically.