diff --git a/_posts/2024-08-01-bash-logging-journald-with-custom-fields.md b/_posts/2024-08-01-bash-logging-journald-with-custom-fields.md new file mode 100644 index 0000000..a3baa5c --- /dev/null +++ b/_posts/2024-08-01-bash-logging-journald-with-custom-fields.md @@ -0,0 +1,109 @@ +--- +title: Bash Logging to Journald with Custom Fields +layout: post +categories: + - linux + - automation +--- + +I'm working on building out various automation scripts and I want to be able to easily track and troubleshoot all of them. Enter structured logging via Journald! Logging to journald via bash is not as easy as python and takes a bit extra work to ensure that it has similar fields as my python logging. Here is a simple example that I've created with a function to make it reusable throughout the script: + +```bash +# Configure the logging +SCRIPT_NAME="journald-logging-example.sh" +CUSTOM_FIELDS="TAG=demo +CATEGORY=blog +LOGGER=${SCRIPT_NAME} +SYSLOG_IDENTIFIER=${SCRIPT_NAME}" + +# Function to log a message with custom fields +log_message() { + local message=$1 + logger --tag="${SCRIPT_NAME}" --journald <