bash-logger/syslog.sh

45 lines
983 B
Bash
Raw Permalink Normal View History

2020-05-21 12:22:23 +00:00
#!/usr/bin/env bash
# Set syslog_local="TRUE" to only log locally.
syslog_host="syslog.adamonet"
syslog_port="514"
syslog_type="tcp"
syslog_facility="local7"
if [[ -z ${syslog_facility} ]]; then
syslog_facility="local7";
fi
message="${*:2}"
if [[ -z $2 ]]; then
priority="${syslog_facility}.info"
message="$1"
else
case $1 in
0|e*) priority="${syslog_facility}.emerg" ;;
1|a*) priority="${syslog_facility}.alert" ;;
2|c*) priority="${syslog_facility}.crit" ;;
3|e*) priority="${syslog_facility}.err" ;;
4|w*) priority="${syslog_facility}.warning" ;;
5|n*) priority="${syslog_facility}.notice" ;;
6|i*) priority="${syslog_facility}.info" ;;
7|d*) priority="${syslog_facility}.debug" ;;
*)
echo "Exit: invalid severity level. No log sent."
exit 1
;;
esac
fi
if [[ $syslog_local == "TRUE" ]]; then
logger -i \
-p "${priority}" \
"${message}"
else
logger -i --tcp \
-n $syslog_host \
-P $syslog_port \
-p "${priority}" \
"${message}"
fi