Instantiate a logging client manually

Viewed 166

I am trying to do the following to connect with stackdriver to my google account:

>>> import google.cloud.logging
>>> client = google.cloud.logging.Client()

I also have my key file located at:

os.path.join(SITE_ROOT, 'utils', 'gcs_testing.json')

Is there a way to manually pass this in python instead of having to export the variable, GOOGLE_APPLICATION_CREDENTIALS. If so, how would this be done?

1 Answers

This should be pretty simple, you can try the following:

>>> import google.cloud.logging
>>> myFile = os.path.join(SITE_ROOT, 'utils', 'gcs_testing.json')
>>> client=google.cloud.logging.Client.from_service_account_json(myFile)

Now you should be able to write to your logs:

>>> logger=client.logger('log_name')
>>> logger.log_text('hello!')
Related