A Bash library for basic log handling
You can clone this repository or download it as a zip file. To clone:
git clone https://github.com/david-moreno/bash-liblog.git
cd bash-liblog
sudo make install
Using the cloned/unzipped directory:
cd bash-liblog
sudo make uninstall
Returns the name of the current log file
Sets the name of the current log file
Returns the path of the log file directory The default is /var/log
Sets the path of the log file directory
Returns the level separator character The default is :
Sets the level separator character
Creates a new log file
Determines if the current log file exists and has write permission This function returns 1 on success or 0 otherwise
Clears the current log file
Deletes the current log file
Writes a message on the current log file
Writes a DEBUG level message on the current log file
Writes a INFO level message on the current log file
Writes a NOTICE level message on the current log file
Writes a WARNING level message on the current log file
Writes a ERROR level message on the current log file
Writes a CRITICAL level message on the current log file
Returns the last line of the current log file
If our script is myscript, we can:
#!/bin/bash
#Includes the library into the script
source "/usr/lib/bash/liblog"
#Sets log file name
log_set_name "myscript.log"
#Optionally (if we have root access), we can create a directory for the log file,
log_set_dir "/var/log/myscrypt"
log_create
#Checks if all was ok
if (( ! `log_check`)); then
echo "Cannot create myscript.log"
exit
fi
#Writes out some stuff to the log
log_write "Starting myscript"
log_write_info "All is fine here"The output on myscript.log is:
sep 4 03:34:06 Starting myscript
sep 4 03:34:06 INFO: All is fine here