I am trying to redirect logs from a certain process to the docker logs, and i am achieving the same using a redirection using > /proc/1/fd/1
and this works perfectly fine.
I then introduce a json logger to format my logs before sending them to the docker logs. The logs is getting printed but not in the format expected. It just prints the message as is without the formatting that was introduced by the json logger.
Here is a snippet:
#!/bin/bash
set -x
# Json logger
IFS= read -r -d '' __JQ_LOGGER_SCRIPT <<'JQSCRIPT'
{
"type": "log",
"host": $host,
"level": $level,
"systemid": $systemid,
"system": "connect-manager",
"time": $time,
"timezone": $timezone,
# "message": .
"log": .
}
JQSCRIPT
json_logger() {
jq
--raw-input
--compact-output
--arg level "$1"
--arg systemid "app-manager-`cat /etc/machine-id`"
--arg host `hostname`.${MY_POD_NAMESPACE}
--arg time `date '+%Y-%m-%dT%H:%M:%S.%3NZ'`
--arg timezone `date +%Z`
"$__JQ_LOGGER_SCRIPT"
}
echo "Starting App manager" | json_logger INFO > /proc/1/fd/1
(
echo "App manager terminated due to signal $?" | json_logger INFO > /proc/1/fd/1
)&
I also tried passing the log message as a second argument to the json logger but it didnt help.
echo "App manager terminated due to signal $?" | json_logger INFO, "App manager terminated due to signal $?" > /proc/1/fd/1
This the kind of log I see in the docker logs
+ jq --raw-input --compact-output --arg name INFO --arg systemid app-manager-868d754f49b140148f55d7aeb59d37d5 --arg host app-wm-app1-7bc64b546b-cgkj6.ns--arg time 2024-07-26T07:58:33.505Z --arg timezone UTC --arg log '' ''
"App manager terminated due to signal 137"
This script works fine when run as is, and as I introduce the redirection I see issues.