From dc686681e0799b12c40f44f85fc5bfd7fed4e57f Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 17 Jan 2022 10:16:53 +0900 Subject: Introduce log_destination=jsonlog "jsonlog" is a new value that can be added to log_destination to provide logs in the JSON format, with its output written to a file, making it the third type of destination of this kind, after "stderr" and "csvlog". The format is convenient to feed logs to other applications. There is also a plugin external to core that provided this feature using the hook in elog.c, but this had to overwrite the output of "stderr" to work, so being able to do both at the same time was not possible. The files generated by this log format are suffixed with ".json", and use the same rotation policies as the other two formats depending on the backend configuration. This takes advantage of the refactoring work done previously in ac7c807, bed6ed3, 8b76f89 and 2d77d83 for the backend parts, and 72b76f7 for the TAP tests, making the addition of any new file-based format rather straight-forward. The documentation is updated to list all the keys and the values that can exist in this new format. pg_current_logfile() also required a refresh for the new option. Author: Sehrope Sarkuni, Michael Paquier Reviewed-by: Nathan Bossart, Justin Pryzby Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com --- doc/src/sgml/config.sgml | 223 ++++++++++++++++++++++++++++++++++++++++++++--- doc/src/sgml/func.sgml | 8 +- 2 files changed, 216 insertions(+), 15 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index c0fbf03dd3c..4cd9818acf8 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -5931,7 +5931,8 @@ SELECT * FROM parent WHERE key = 2400; PostgreSQL supports several methods for logging server messages, including - stderr, csvlog and + stderr, csvlog, + jsonlog, and syslog. On Windows, eventlog is also supported. Set this parameter to a list of desired log destinations separated by @@ -5950,25 +5951,35 @@ SELECT * FROM parent WHERE key = 2400; CSV-format log output. - When either stderr or - csvlog are included, the file - current_logfiles is created to record the location - of the log file(s) currently in use by the logging collector and the - associated logging destination. This provides a convenient way to - find the logs currently in use by the instance. Here is an example of - this file's content: + If jsonlog is included in + log_destination, log entries are output in + JSON format, which is convenient for loading logs + into programs. + See for details. + must be enabled to generate + JSON-format log output. + + + When either stderr, + csvlog or jsonlog are + included, the file current_logfiles is created to + record the location of the log file(s) currently in use by the logging + collector and the associated logging destination. This provides a + convenient way to find the logs currently in use by the instance. Here + is an example of this file's content: stderr log/postgresql.log csvlog log/postgresql.csv +jsonlog log/postgresql.json current_logfiles is recreated when a new log file is created as an effect of rotation, and when log_destination is reloaded. It is removed when - neither stderr - nor csvlog are included - in log_destination, and when the logging collector is - disabled. + none of stderr, + csvlog or jsonlog are + included in log_destination, and when the logging + collector is disabled. @@ -6106,6 +6117,13 @@ local0.* /var/log/postgresql (If log_filename ends in .log, the suffix is replaced instead.) + + If JSON-format output is enabled in log_destination, + .json will be appended to the timestamped + log file name to create the file name for JSON-format output. + (If log_filename ends in .log, the suffix is + replaced instead.) + This parameter can only be set in the postgresql.conf file or on the server command line. @@ -7467,6 +7485,187 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv; + + Using JSON-Format Log Output + + + Including jsonlog in the + log_destination list provides a convenient way to + import log files into many different programs. This option emits log + lines in (JSON) format. + + + + String fields with null values are excluded from output. + Additional fields may be added in the future. User applications that + process jsonlog output should ignore unknown fields. + + + + Each log line is serialized as a JSON object as of the following + set of keys with their values. + + + + Keys and values of JSON log entries + + + + Key name + Type + Description + + + + + timestamp + string + Time stamp with milliseconds + + + user + string + User name + + + dbname + string + Database name + + + pid + number + Process ID + + + remote_host + string + Client host + + + remote_port + number + Client port + + + session_id + string + Session ID + + + line_num + number + Per-session line number + + + ps + string + Current ps display + + + session_start + string + Session start time + + + vxid + string + Virtual transaction ID + + + txid + string + Regular transaction ID + + + error_severity + string + Error severity + + + state_code + string + SQLSTATE code + + + message + string + Error message + + + detail + string + Error message detail + + + hint + string + Error message hint + + + internal_query + string + Internal query that led to the error + + + internal_position + number + Cursor index into internal query + + + context + string + Error context + + + statement + string + Client-supplied query string + + + cursor_position + string + Cursor index into query string + + + func_name + string + Error location function name + + + file_name + string + File name of error location + + + file_line_num + number + File line number of the error location + + + application_name + string + Client application name + + + backend_type + string + Type of backend + + + leader_pid + number + Process ID of leader for active parallel workers + + + query_id + number + Query ID + + + +
+
Process Title diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 391d01bcf3d..a270f89dfe9 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -22446,10 +22446,12 @@ SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n); format, pg_current_logfile without an argument returns the path of the file having the first format found in the ordered list: stderr, - csvlog. NULL is returned - if no log file has any of these formats. + csvlog, jsonlog. + NULL is returned if no log file has any of these + formats. To request information about a specific log file format, supply - either csvlog or stderr as the + either csvlog, jsonlog or + stderr as the value of the optional parameter. The result is NULL if the log format requested is not configured in . -- cgit v1.2.3