Skip to content

Commit

Permalink
[ADD] gcp_json2log: add script to convert google cloud platform json …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fernandahf committed Jan 21, 2021
1 parent 7ec5871 commit bb3106f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gcp_json2log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
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):
if is_textpayload:
log.write("%s\n" % value)
is_textpayload = False

if value == "textPayload":
is_textpayload = True

0 comments on commit bb3106f

Please sign in to comment.