11

My Stack:
Google App Engine Standard
Python (2.7)

Goal:
To create named logs in Google Stackdriver Logging, https://console.cloud.google.com/logs/viewer

Docs - Stackdriver Logging: https://google-cloud-python.readthedocs.io/en/latest/logging/usage.html

Code:

from google.cloud import logging as stack_logging
from google.cloud.logging.resource import Resource
import threading

class StackdriverLogging:
    def __init__(self, resource=Resource(type='project', labels={'project_id': 'project_id'}), project_id='project_id'):

    self.resource = resource
    self.client = stack_logging.Client(project=project_id)

    def delete_logger(self, logger_name):
        logger = self.client.logger(logger_name)
        logger.delete()

    def async_log(self, logger_name, sev, msg):
        t = threading.Thread(target=self.log, args=(logger_name, sev, msg,))
        t.start()

    def log(self, logger_name, sev, msg):
        logger = self.client.logger(logger_name)

    if isinstance(msg, str):
        logger.log_text(msg, severity=sev, resource=self.resource)
    elif isinstance(msg, dict):
        logger.log_struct(msg, severity=sev, resource=self.resource)

class hLog(webapp2.RequestHandler):
   def get(self):
      stackdriver_logger = StackdriverLogging()
      stackdriver_logger.async_log("my_new_log", "WARNING", msg="Hello")
      stackdriver_logger.async_log("my_new_log", "INFO", msg="world")

ERROR: Found 1 RPC request(s) without matching response

If this is not possible in Google App Engine Standard (Python) any way to get this code to work:

  from google.cloud import logging
  client = logging.Client()
  # client = logging.Client.from_service_account_json('credentials.json')
  logger = client.logger("my_new_log")
  logger.log_text("hello world") 

If credentials are required, I like to use the project service account.

Any help would be appreciated. Thank you.

1
  • 1
    was just looking over some Python logging issues a couple of days ago, at which time I noticed a log in Stackdriver with the name "app". not sure where the name came from, but it was logging as I wanted, respecting the log-level and everything. while looking for docs on this I ran across the following: googlecloudplatform.github.io/google-cloud-python/latest/…. I did not use this but perhaps it will help you out? Commented Nov 10, 2017 at 16:13

1 Answer 1

2
+50

I usually tie the Python logging module directly into Google Stackdriver Logging. To do this, I create a log_helper module:

from google.cloud import logging as gc_logging
import logging

logging_client = gc_logging.Client()
logging_client.setup_logging(logging.INFO)

from logging import *

Which I then import into other files like this:

import log_helper as logging

After which you can use the module like you would the default python logging module.

To create named logs with the default python logging module, use different loggers for different namespaces:

import log_helper as logging

test_logger = logging.getLogger('test')
test_logger.setLevel(logging.INFO)
test_logger.info('is the name of this logger')

Output:

INFO:test:is the name of this logger

Sign up to request clarification or add additional context in comments.

4 Comments

This is NOT THE CORRECT ANSWER!, This does not address the question of creating named logs
@BrianB Named logging is pretty straightforward with the default logging module. You can use different loggers for different namespaces.
How would you create a named log with Google App Engine Standard (Python)?
I've added an explanation on how to create named logs to the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.