A Script for formatting/structuring log files without the User having to worry about doing the formatting themselves.
A Script for formatting/structuring log files without the User having to worry about doing the formatting themselves. LogAppend formats the log file with new lines, line separators, as well as a feature to append time/date to the log.
Essentially written quickly in my own time for an old job where I needed log files for auditable trails, I made this script so I wouldn't have to worry about appending new lines manually, which often was left forgotten leading to irregular logging which annoyed me.
This has been newly refactored. At the time, it was really specific to me, which was sudo tee built-in, as well as timedatectl - a Systemd-only feature. I only really realized this recently when I was trying to use it in a container, thus decided to re-write entirely.
It is also important to know that LogAppend does not create files, therefore, you will have to manually create the log file yourself.
This does not create the log file, it only appends to log. There is a backup feature that will backup the current iteration of the log file with the time and date. But for primary logs, the User will have to create it.
Sudo tee is no longer baked-in, and now a user can decide that themselves by running ' | sudo log -a <log file>'.
Furthermore, added portability by using $(date) instead of timedatectl.
So the flow is essentially:
Begin the log file with the current date and time and create a separator for easy distinction:
log -b logfile.log
Append to the logfile the commands you wish to log formatting it with new lines:
echo "Hello World" | log -a logfile.log
-> Note: -a can only be piped, it is not meant to run as a primary command.
Formats the end of a log entry by appending the current time and date, followed by a separation line.
log -e logfile.log
Important: if you are on MacOS, there is a native command called 'log'. you may want to install it under another name, so just change
sudo ln -sf "$(pwd)/log_append.sh" /usr/local/bin/log
to
sudo ln -sf "$(pwd)/log_append.sh" /usr/local/bin/logap
or whatever you wish. It is recommended to do this, as log is already used by some systems.
cd to the directory where you wish to download to, then copy paste the following command:
git clone https://v14.next.forgejo.org/FMallon/LogAppend && cd LogAppend && chmod +x log_append.sh && sudo ln -sf "$(pwd)/log_append.sh" /usr/local/bin/log
git clone https://github.com/FMallon/LogAppend && cd LogAppend && chmod +x log_append.sh && sudo ln -sf "$(pwd)/log_append.sh" /usr/local/bin/log
This assumes it is installed under the name 'log':
[ -a | --append ] Appends to the specified log file
Usage:
-> <Command> | log -a <log file name>
Note:
This can only be piped from another command
[ -b | --begin ] Formats the beginning of a log entry by appending the new log entry with a separation line, as well as the Time & Date
Usage:
-> log -b <log file name>
[ -e | --end ] Formats the ending of a log entry by appending the current log entry with the Time & Date, as well as a separation line
Usage:
-> log -e <log file name>
[ -h | --help ] Brings up the Usage - this thing right here
Usage:
-> log -h
[ --edit ] Opens up the log file in a Terminal Editor towards the bottom of the file for longer log files for editing any minor changes that need to be made, or quick viewing of the log file
Usage:
-> log --edit <log file name>
[ --backup ] Backs-up a specified log file with the <log_file_name>_<date_time>.bk format
Usage:
-> log --backup <log file name>
-> log --backup <log file name> <destination directory>
Return Codes:
Return 1 - Error: Invalid Flag | Invalid number of args
Return 2 - Error: File doesn't exist or is empty
Return 3 - Error: Invalid File/Directory Permissions
Return 4 - Error: Invalid Usage - as it relates to piping the command
Return 5 - Error: Invalid args passed to function - this is internal-facing
Return 6 - Error: Appending to the log
Return 7 - Error: Backing-up the log
Return 8 - Error: Directory doesn't exist
Return 9 - Error: No Supported Editors Found
Return 10 - Error: Running Command to Open the Log with the Editor
Return 11 - Error: Failed to get Date & Time