Skip to content

Write each log record with one write() per destination - #76

Open
teknogeek0 wants to merge 2 commits into
mainfrom
feat/logging-core
Open

teknogeek0 wants to merge 2 commits into
mainfrom
feat/logging-core

Conversation

@teknogeek0

Copy link
Copy Markdown
Collaborator

Each log record is now built in one buffer and written with one write() to each destination: stderr, the PGCOPYDB_LOG_FILENAME file, and a new JSON Lines file at <dir>/log/pgcopydb.jsonl. The stderr and log file output is byte-identical to the current output. The new tests/logging suite stores the current output as golden files and compares against them.

Defects in the current logger that the new suite shows:

  • The PGCOPYDB_LOG_FILENAME file is a buffered FILE* that is not flushed per record or before fork(). In a stress run with 16 writers, up to 93% of the text records were torn.
  • With PGCOPYDB_LOG_JSON or PGCOPYDB_LOG_JSON_FILE, a message longer than 2048 bytes overflows a stack buffer and the process crashes.
  • semaphore_log_lock_function unlocks after a failed lock, which sets the semaphore value to 2.
  • catch_quit_and_exit logs and calls exit() from the signal handler.

Behavior changes:

  • <dir>/log/pgcopydb.jsonl is written when --dir is set. It gets INFO records, or more with -v, -d, or -z, and it is appended to across runs. PGCOPYDB_LOG_WORKDIR=off|<level> controls it. Its records add run_id and role keys.
  • JSON records from forked processes add a role key after message.
  • Invalid UTF-8 in a JSON message becomes �. Before, the message key was dropped.
  • On SIGQUIT, pgcopydb writes SIGQUIT received, exiting now to stderr, removes the System V resources it owns, and calls _exit().

The PGCOPYDB_LOG_FILENAME file used a buffered FILE* that was not flushed
per record or before fork(), so lines from different processes were torn,
copied into children, or lost on a crash. JSON messages longer than 2048
bytes overflowed a stack buffer. Each record is now built in one buffer and
written with one write() to stderr, to the log file, and to a new JSON Lines
file at <dir>/log/pgcopydb.jsonl (PGCOPYDB_LOG_WORKDIR). The SIGQUIT handler
no longer logs from signal context. The new tests/logging suite checks that
the text and JSON output does not change and that concurrent writers do not
tear or duplicate records.
Remove the list indentation and the trailing space in a code span.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant