3

I am running a very simple Flask python App in Standard Environment Google App Engine. I am trying to view log information via the command: gcloud app logs tail -s default.

I tried printing using 3 different methods:

  1. logging.info("Printing via Google App Engine Log")
  2. print("Printing via python print")
  3. app.logger.info("Printing via Flask Log")

Sadly, none of them worked.

So how do I print out lines in the live log stream?

Thanks


Edit: Added a section of my code

from flask import Flask
from flask import request

import requests
import json
import logging

app = Flask(__name__)

@app.route("/", methods=["GET"])
def webhook():

    app.logger.info("Printing via Flask Log")
    print("Printing via python print")
    logging.info("Printing via Google App Engine Log")

    if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
        if not request.args.get("hub.verify_token") == VERIFICATION_TOKEN:
            return "Verification token mismatch", 403
        return request.args["hub.challenge"], 200

    return "Hello world", 200

If you think that showing other files such as .yaml file helps, please let me know. Thanks!

Bosen
  • 941
  • 2
  • 12
  • 26
  • The 1st method works just fine for me. Is the cmd showing some entries but not the ones you're expecting or it is not producing anything? Do you see the respective entries in the [developer console logs viewer](https://console.cloud.google.com/logs/viewer)? – Dan Cornilescu Aug 02 '17 at 17:52
  • @DanCornilescu I am unable to see any of the logs that I have printed via the 3 commands listed. I can only see the GET and POST request logs coming in. Are you using Flask as well? Can I have a look at your main.py? – Bosen Aug 03 '17 at 01:34
  • I do see something odd - only the request logs are displayed in the `gcloud app logs [tail|read]` cmds, but not the application logs (i.e. those from your `logging.*` calls) attached to them, which may be what you're seeing as well. Check the end of this post about seeing them in the developer console: https://stackoverflow.com/questions/43557680/reading-application-logs-on-google-app-engine-from-developer-console/43565578#43565578. I'd consider this a gcloud bug. – Dan Cornilescu Aug 03 '17 at 02:12

1 Answers1

5

Per this Google Cloud Platform doc, the following is an example of how to use the different logging levels:

import logging

import webapp2


class MainPage(webapp2.RequestHandler):
    def get(self):
        logging.debug('This is a debug message')
        logging.info('This is an info message')
        logging.warning('This is a warning message')
        logging.error('This is an error message')
        logging.critical('This is a critical message')

        try:
            raise ValueError('This is a sample value error.')
        except ValueError:
            logging.exception('A example exception log.')

        self.response.out.write('Logging example.')


app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)

1- On your deployment, use --verbosity flag:

gcloud app deploy --verbosity debug

2- Run gcloud app logs tail -s default

3- Trigger any endpoint. In the above case, any GET call would do.

Kenworth
  • 593
  • 2
  • 9
  • Hi, I am using Flask as my though, is there a difference? Added a section of my code running on google app engine standard environment. Thanks – Bosen Aug 03 '17 at 01:59
  • ["logging"](https://www.loggly.com/ultimate-guide/python-logging-basics/) is a Python module for emitting log messages. [Flask](http://pymbook.readthedocs.io/en/latest/flask.html) is a web framework. These two have different jobs and are normally used together. It won't make a difference if you are using Flask or not. – Kenworth Aug 03 '17 at 22:18