Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f3a9a1

Browse files
committed
Add property to enable human-readable timestamps in control jobs
- 'databases.logging.format.timestamp' defaults to 'rfc3339' which is the recommended format - 'deprecated' will result in all timestamps being in the format they were before this change [#172017763]
1 parent 325f760 commit 3f3a9a1

File tree

8 files changed

+71
-25
lines changed

8 files changed

+71
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ databases.hooks.pre-start | Script to run before starting PostgreSQL.
100100
databases.hooks.post-start | Script to run after PostgreSQL has started.
101101
databases.hooks.pre-stop | Script to run before stopping PostgreSQL.
102102
databases.hooks.post-stop | Script to run after PostgreSQL has stopped.
103+
databases.logging.format.timestamp | Format for timestamp in control jobs logs. By default it's set to `rfc3339`.
103104
janitor.script | If specified, this script would be run periodically. This would be useful for running house-keeping tasks.
104105
janitor.interval | Interval in seconds between two invocations of the janitor script. By default it's set to `1` day.
105106
janitor.timeout | Time limit in seconds for the janitor script. By default it's set to `0` that means no time limit.

jobs/postgres/spec

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ properties:
118118
databases.enable_trace:
119119
description: "Print additional traces in control scripts"
120120
default: false
121+
databases.logging.format.timestamp:
122+
description: |
123+
Format for timestamp in component logs.
124+
This includes pre-start, postgres_ctl, pg_janitor_ctl, janitor, and hooks; PostgreSQL logs are not included.
125+
Valid values are 'rfc3339', and 'deprecated'."
126+
'rfc3339' is the recommended format.ormat, which is human readable.
127+
'deprecated' will result in all timestamps being in the format they were before the rfc3339 flag was introduced.
128+
default: "rfc3339"
121129
janitor.script:
122130
description: "If specified, janitor would periodically run this script"
123131
default: ''

jobs/postgres/templates/pg_janitor.sh.erb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#!/bin/bash -e
22

3-
export PIDNUM=$$
4-
5-
exec > >(tee -a >(logger -p user.info -t vcap.$(basename $0).stdout) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/janitor.log)
6-
exec 2> >(tee -a >(logger -p user.error -t vcap.$(basename $0).stderr) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/janitor.err.log)
3+
pidnum=$$
4+
pname=$(basename $0)
75

6+
set +u
7+
source /var/vcap/packages/postgres-common/utils.sh
8+
set -u
89
source /var/vcap/jobs/postgres/bin/pgconfig.sh
10+
11+
tee_output_to_sys_log "${LOG_DIR}" "${pname}" "${LOG_FORMAT}" "janitor.log" "janitor.err.log" "${pidnum}"
12+
913
<% if p("janitor.script").empty? %>
1014
echo "Nothing to do. Going to sleep."
1115
sleep infinity

jobs/postgres/templates/pg_janitor_ctl.sh.erb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/bash -eu
22

3-
export PIDNUM=$$
3+
pidnum=$$
4+
pname=$(basename $0)
45

5-
exec > >(tee -a >(logger -p user.info -t vcap.$(basename $0).stdout) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/$(basename $0).log)
6-
exec 2> >(tee -a >(logger -p user.error -t vcap.$(basename $0).stderr) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/$(basename $0).err.log)
6+
set +u
7+
source /var/vcap/packages/postgres-common/utils.sh
8+
set -u
9+
source /var/vcap/jobs/postgres/bin/utils.sh
10+
11+
tee_output_to_sys_log "${LOG_DIR}" "${pname}" "${LOG_FORMAT}" "${pname}.log" "${pname}.err.log" "${pidnum}"
712

813
function main() {
914
local action
1015
action="${1}"
1116

12-
source /var/vcap/jobs/postgres/bin/utils.sh
13-
set +u
14-
source /var/vcap/packages/postgres-common/utils.sh
15-
set -u
16-
1717
case "${action}" in
1818
"start")
1919
mkdir -p "${RUN_DIR}"

jobs/postgres/templates/pgconfig.sh.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ if [ -f "${VERSION_FILE}" ]; then
2828
DATA_DIR_OLD="${PG_STORE_DIR}/${pgversion_upgrade_from}"
2929
PACKAGE_DIR_OLD=/var/vcap/packages/${pgversion_upgrade_from}
3030
fi
31-
31+
<%
32+
if !['rfc3339', 'deprecated'].include?(p('databases.logging.format.timestamp'))
33+
raise "'#{p('databases.logging.format.timestamp')}' is not a valid timestamp format for the property 'databases.logging.format.timestamp'. Valid options are: 'rfc339' and 'deprecated'."
34+
end
35+
%>
3236
RUN_DIR=/var/vcap/sys/run/postgres
3337
LOG_DIR=/var/vcap/sys/log/postgres
38+
LOG_FORMAT=<%= p("databases.logging.format.timestamp") %>
3439
HOOK_LOG_OUT=${LOG_DIR}/hooks.stdout.log
3540
HOOK_LOG_ERR=${LOG_DIR}/hooks.stderr.log
3641
# external_pid_file in postgresql.conf takes care of

jobs/postgres/templates/postgres_ctl.sh.erb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/bash -eu
22

3-
export PIDNUM=$$
3+
pidnum=$$
4+
pname=$(basename $0)
45

5-
exec > >(tee -a >(logger -p user.info -t vcap.$(basename $0).stdout) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/$(basename $0).log)
6-
exec 2> >(tee -a >(logger -p user.error -t vcap.$(basename $0).stderr) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/$(basename $0).err.log)
6+
source /var/vcap/jobs/postgres/bin/pgconfig.sh
7+
set +u
8+
source /var/vcap/packages/postgres-common/utils.sh
9+
set -u
10+
11+
tee_output_to_sys_log "${LOG_DIR}" "${pname}" "${LOG_FORMAT}" "${pname}.log" "${pname}.err.log" "${pidnum}"
712

813
function main() {
914
local action
1015
action="${1}"
1116

12-
source /var/vcap/jobs/postgres/bin/pgconfig.sh
13-
set +u
14-
source /var/vcap/packages/postgres-common/utils.sh
15-
set -u
16-
1717
case "${action}" in
1818
"start")
1919
mkdir -p "${RUN_DIR}"

jobs/postgres/templates/pre-start.sh.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/bin/bash -eu
22

3-
export PIDNUM=$$
4-
5-
exec > >(tee -a >(logger -p user.info -t vcap.$(basename $0).stdout) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/pre-start.stdout.log)
6-
exec 2> >(tee -a >(logger -p user.error -t vcap.$(basename $0).stderr) | awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] $PIDNUM"); print " " $0 }' >> /var/vcap/sys/log/postgres/pre-start.stderr.log)
3+
pidnum=$$
4+
pname=$(basename $0)
75

86
set +u
97
source /var/vcap/packages/postgres-common/utils.sh
108
set -u
119
source /var/vcap/jobs/postgres/bin/pgconfig.sh
1210
source /var/vcap/jobs/postgres/bin/utils.sh
1311

12+
tee_output_to_sys_log "${LOG_DIR}" "${pname}" "${LOG_FORMAT}" "${pname}.stdout.log" "${pname}.stderr.log" "${pidnum}"
13+
1414
function upgrade(){
1515
if [ -d $DATA_DIR -a -f $POSTGRES_UPGRADE_LOCK ]; then
1616
echo "FAIL: DB upgrade stopped in the middle, manual intervention required, quitting..."

src/postgres-common/utils.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,31 @@ killProcessTree() {
132132
listProcessTree $1 | xargs kill
133133
kill $1
134134
}
135+
136+
function tee_output_to_sys_log() {
137+
local log_dir="$1"
138+
local pname="$2"
139+
local log_format="$3"
140+
local log_out="$4"
141+
local log_err="$5"
142+
local pid_num="$6"
143+
144+
if [ "${log_format}" == "deprecated" ]; then
145+
exec > >(tee -a >(logger -p user.info -t "vcap.${pname}.stdout") | prepend_datetime ${pid_num} >> "${log_dir}/${log_out}")
146+
exec 2> >(tee -a >(logger -p user.error -t "vcap.${pname}.stderr") | prepend_datetime ${pid_num} >> "${log_dir}/${log_err}")
147+
else
148+
exec > >(tee -a >(logger -p user.info -t "vcap.${pname}.stdout") | prepend_rfc3339_datetime ${pid_num} >>"${log_dir}/${log_out}")
149+
exec 2> >(tee -a >(logger -p user.error -t "vcap.${pname}.stderr") | prepend_rfc3339_datetime ${pid_num} >>"${log_dir}/${log_err}")
150+
fi
151+
}
152+
153+
function prepend_datetime() {
154+
export pid_num="$1"
155+
awk -W interactive '{ system("echo -n [$(date +\"%Y-%m-%d %H:%M:%S%z\")] ${pid_num}"); print " " $0 }'
156+
}
157+
158+
function prepend_rfc3339_datetime() {
159+
export pid_num="$1"
160+
perl -ne 'BEGIN { use Time::HiRes "time"; use POSIX "strftime"; STDOUT->autoflush(1) }; my $t = time; $process_pid = $ENV{'pid_num'}, my $fsec = sprintf ".%09d", ($t-int($t))*1000000000; my $time = strftime("[%Y-%m-%dT%H:%M:%S".$fsec."Z]", localtime $t); print("$time $process_pid $_")'
161+
162+
}

0 commit comments

Comments
 (0)