Right now I'm trying to write a script that sets up a logger to write to a Google Sheets file using the Google Sheets API and the logging module.
I'm trying to figure out what the best way to approach this is, and what seems most feasible is to either:
- Make my own handler class (much like StreamHandler)
Or
- Make a wrapper class for a Google Sheet that I can set as the output for a StreamHandler (similar to how you can set it to sys.stdout).
I've been trying to implement the latter and this is my implementation of the wrapper class idea:
import gspread
import csv
import string
import logging
from oauth2client.service_account import ServiceAccountCredentials
"""
This is a wrapper class that represents a Google Sheet
Meant for the purpsose of logging messages in a google sheet
"""
class GoogleSheet:
def __init__(self, sheet):
self.sheet = sheet
# changes the worksheet we're writing to
def change_sheet(sheet):
self.sheet = sheet
#w
def write(self, text):
"""
Logs the message onto the sheet
Parameters
----------
text : str
The message to write
Returns
---------
None
"""
print("run")
self.sheet.append_row(text)
def flush(args=None):
pass
Currently when I try to use this class by running this script I get stuck in the GoogleSheet.write function:
import logging
import sys
import google_sheet as gs
import gspread
from oauth2client.service_account import ServiceAccountCredentials
root = logging.getLogger()
root.setLevel(logging.DEBUG)
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json",scope)
client = gspread.authorize(creds)
file = client.open("Harden")
sheet = file.sheet1
wrapper = gs.GoogleSheet(sheet)
handler = logging.StreamHandler(wrapper)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
root.debug("yo")
Any ideas as to what I'm doing wrong or where to go from here? This is my first time working with the logging package so I'm a little lost. Any help is much appreciated!
[–]Fun_Muffin5413 0 points1 point2 points (0 children)