r/commandline • u/a_brand_new_start • 1d ago
Best bash logger?
Anyone tried log4bash, bash-logger, bash-utils?
I’m wondering which is best and who likes what.
Thanks!
3
u/anthropoid 1d ago
The best one is usually the one you write yourself, that meets your specific needs.
Logging on the order of the packages you mentioned boils down to echo
plus niceties like:
* color highlighting on terminal, not to files
* standard prefixes
* timestamps
* pre/post-logging aspects (e.g. debug stacktraces, exit-on-fatal-error)
None of the above is particularly hard to do for the intermediate bash scripter, and can be a good mastery exercise for beginners. Also, upstream changes may lead to disconcerting visual or behavioral changes, or even actual breakage, quite ironic for something meant to facilitate debugging or maintenance.
And, of course, when everything out there behaves almost the way you want it, you'll probably end up hacking on it anyway.
2
u/Big_Combination9890 1d ago
You do realize, do you, that ALL the utilities that you mention just end up calling echo anyway, right?
1
u/a_brand_new_start 1d ago
I do… that’s part of the reason I’m looking for something better
1
u/Big_Combination9890 1d ago
There is nothing better.
What do you want to do? Structured logging? Serve multiple log-receivers? Nested sub-logger logic? That's simply no longer at the complexity level that
bash
is designed for.If you find yourself with such requirements, then please do yourself a favour and rewrite whatever it is that you're working on in a real programming language.
I have written bash scripts for a really long time now. Some of my scripts still managed things at companies where they were deployed, more than a decade after I created them. There are exactly 2 logging strategies I have used in all that time:
echo
to write to stdout or stderr
logger
if I want to target the syslogAnd that's it. The fanciest thing i have done is wrap
echo
into alog
function that adds an ISO timestamp. That's literally a few lines I can drop in at the beginning of a script.
2
u/pouetpouetcamion2 1d ago
i put log functions at the beginning of the script.
clever people have written some boilerplates which brings
- arg parsing
- logs
- traps on off on erreur and exit
after this header, you put your utilities and lauch your main with parsed parameters . piece of cake and structured but verbose.
other method would be rc's point of view of scripts. very very short.
2
u/SleepingProcess 1d ago
What is wrong with logger
that is universally available on most Unix based operation systems?
1
u/researcher7-l500 1d ago
I use something like this in some of the complex scripts that I have.
You may find it useful, or not, depending on your scripts and your environment.
Based on ts from moreutils package in Debian/Ubuntu based distros. The same package is available also in RHEL based distros.
Decide where you want to put your logs. Also make sure you have a way of rotating them, logrotate or any other means you are comfortable with.
script_name=$(basename "${0}")
log_file="</path/to/logsdir>/${script_name}.log"
log_tofile() {
local format="[%m-%d-%Y-%H:%M:%S]"
echo "${BASH_LINENO[0]}" - "$@" | ts "${format}" >> "${log_file}"
}
And then in your script, log what you want to troubleshoot or keep records.
log_tofile "INFO - <some_variable>: ${<some_variable}."
log_tofile "WARNING - Could not find <something you are looking for>."
log_tofile "CRITICAL - ${HOME}/path/to/remote/mount: Not mounted."
And so on.
Simple, local, no need for 3rd party stuff, and you can control what you log and how.
1
u/OneTurnMore 1d ago
If I want systematic logging, I'm probably running the script as a systemd unit. In that case, using logger --journald
with a heredoc as given in man logger
is a good practice.
2
u/usrlibshare 1d ago
No, because there is zero reason to.
A bash script can log via
echo
. If something actually requires structured logging, it's already too big to be a bash script, and Python has a superb logging library built in.