From a0999589c0f904c5176a18fe4681063db0417b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernanda=20Hern=C3=A1ndez?= Date: Thu, 21 Jan 2021 10:31:31 -0600 Subject: [PATCH] [ADD] gcp_json2log: add script to convert google cloud platform json log in postgresql.log Method 'get_psql_log_from_gcp_json' needs a full path with name file and it'll generate a postgresql.log file --- gcp_json2log.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 gcp_json2log.py diff --git a/gcp_json2log.py b/gcp_json2log.py new file mode 100644 index 0000000..159ed2b --- /dev/null +++ b/gcp_json2log.py @@ -0,0 +1,23 @@ +""" +Convert Google Cloud Platform log in json to postgresql log +""" +import ijson +import os + + +def get_psql_log_from_gcp_json(gcp_json_file): + """ + ijson is a module that will work with JSON as a stream. + Here doc: https://pypi.org/project/ijson/#lower-level-interfaces + Check if value is textPayload, because next item is textPayload value + gcp_json_file: full path of json log + """ + + is_textpayload = False + with open(gcp_json_file) as fjson, open("postgresql.log", "a") as log: + for prefix, the_type, value in ijson.parse(fjson, multiple_values=True): + if is_textpayload: + log.write("%s\n" % value) + is_textpayload = False + + is_textpayload = value == "textPayload"