Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: 'client.setup_logging()' doesn't identify the underlying platform #7

Closed
guillaumeblaquiere opened this issue Oct 25, 2019 · 9 comments · Fixed by #200
Closed
Assignees
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Milestone

Comments

@guillaumeblaquiere
Copy link

My objective is to format correctly the logs into my Python app for having a good ingestion and formatting on Stackdriver logging console with level well interpreted.

The standard logging doesn't work for App Engine python 3.7 (work in Python 2) and on Cloud Run, because the logs aren't formatted in FluentD.
However, with Cloud Function, it's the case.

I found how to perform a workaround, (see example), but I think there is a bug in your library. Indeed, the function client.setup_logging() should set up my logger with the correct format according with the environment.

It's not the case, and that's why I have to implement the workaround. Sadly, this workaround don't take into account the environment and when I run locally the app, I have this log trace:

 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
{"message": " * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)", "timestamp": {"seconds": 1571789029, "nanos": 987048387}, "thread": 140201867048192, "severity": "INFO"}
{"message": "call info1", "timestamp": {"seconds": 1571789035, "nanos": 285192966}, "thread": 140201732208384, "severity": "INFO"}
{"message": "call info2", "timestamp": {"seconds": 1571789035, "nanos": 285630702}, "thread": 140201732208384, "severity": "INFO"}
{"message": "this is an error2", "timestamp": {"seconds": 1571789035, "nanos": 285893201}, "thread": 140201732208384, "severity": "ERROR"}
{"message": "127.0.0.1 - - [23/Oct/2019 02:03:55] \"GET / HTTP/1.1\" 200 -", "timestamp": {"seconds": 1571789035, "nanos": 286895513}, "thread": 140201732208384, "severity": "INFO"}

Which is in FluentD format, but barely lisible.

The automatic configuration promised at this page doesn't happen!

Environment details

  1. Specify the API at the beginning of the title (for example, "BigQuery: ...")
    General, Core, and Other are also allowed as types

Logging API for App Engine and Cloud Run

  1. OS type and version
    Python 3.7, linux( appengine, container, local environment)

  2. Python version and virtual environment information: python --version
    3.7. Version not detailed because occur on App Engine

  3. google-cloud- version: pip show google-<service> or pip freeze
    google-cloud-logging==1.14.0

Steps to reproduce

Deploy an app on Cloud Run or App Engine standard Python 3.7. Bellow an example with the workaround. Try to change the log handler with client.setup_logging() no special formater are set, and the log on Stackdriver aren't well ingested.

Code example

This is a working example. I don't use client.setup_logging()

import os
from flask import Flask, request
import logging
from google.cloud.logging.handlers.container_engine import ContainerEngineHandler


# This change nothing to the standard logger on App Engine and Cloud Run env
# client = google.cloud.logging.Client()
# client.setup_logging()

# This is a working workaround on GCP but with not lisible log in local env
handler = ContainerEngineHandler()
cloud_logger = logging.getLogger()
cloud_logger.setLevel(logging.DEBUG)
cloud_logger.addHandler(handler)

app = Flask(__name__)
@app.route('/', methods=['GET'])

def call_oracle_connection():
    logger = logging.getLogger("cloudLogger")
    logger.info('call info1')
    logger = logging.getLogger()
    logger.info('call info2')
    logging.error('this is an error2')
    return oracle_connection(request)

if __name__ == "__main__":
    app.run(host='0.0.0.0',port=int(os.environ.get('PORT',8080)))

Thanks!

@tseaver tseaver changed the title client.setup_logging() doesn't identify the underlying platform Logging: 'client.setup_logging()' doesn't identify the underlying platform Oct 25, 2019
@adamalton
Copy link

I also have the same issue. I've followed the instructions here: https://cloud.google.com/logging/docs/setup/python

But when viewing the logs in the Cloud web Console they're all badly formatted (line breaks are missing, so tracebacks are unreadable) and the 'level' info is missing, so you can't filter by info/warning/error/etc.

This has got to be a bug in the library. It can't possibly be deliberate to have the logs look this terrible, or to require such levels of weird voodoo configuration to get it the formatting to work.

@plamut plamut transferred this issue from googleapis/google-cloud-python Feb 5, 2020
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label Feb 5, 2020
@plamut plamut added the type: question Request for information or clarification. Not an issue. label Feb 5, 2020
@jakebiesinger-onduo
Copy link

jakebiesinger-onduo commented Mar 4, 2020

I filed https://issuetracker.google.com/issues/150734081 and then stumbled on this issue, which is basically a dupe of both the problem and workaround.

+1 to no voodoo to getting logging working properly. According to [the docs]:(https://cloud.google.com/run/docs/logging#writing_structured_logs)

You can use the Google Cloud's operations suite Logging Client Libraries to get this functionality automatically.

Both the level and the trace should be set in stackdriver logs by virtue of explicitly setting up a google.cloud.logging handler.

@oliviernguyenquoc
Copy link

Is there any update on this or workarround ? It makes application monitoring very difficult...
We can't track bugs without this feature.

@daniel-sanche
Copy link
Contributor

Currently, this library doesn't integrate well with newer GCP environments like Cloud Run and Cloud Functions. This is a known high-priority issue we're working on fixing this quarter.

@oliviernguyenquoc As reported above, you should be able to set the handler explicitly as a workaround. Is that not working for you?

@daniel-sanche daniel-sanche self-assigned this Jan 22, 2021
@daniel-sanche daniel-sanche added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Jan 22, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jan 22, 2021
@daniel-sanche daniel-sanche added priority: p2 Moderately-important priority. Fix may not be included in next release. and removed 🚨 This issue needs some love. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Jan 25, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jan 25, 2021
@daniel-sanche daniel-sanche removed 🚨 This issue needs some love. type: question Request for information or clarification. Not an issue. labels Jan 26, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jan 26, 2021
@daniel-sanche daniel-sanche added priority: p3 Desirable enhancement or fix. May not be included in next release. and removed priority: p2 Moderately-important priority. Fix may not be included in next release. labels Jan 27, 2021
@yoshi-automation yoshi-automation removed the 🚨 This issue needs some love. label Jan 27, 2021
@oliviernguyenquoc
Copy link

@daniel-sanche : This work arround is not working on Cloud Run. We tried multiple times, by multiple developpers.
Is priority P3 change your estimate of patching in Q1 ?

This is an important feature to track our bug in production.

Thanks !

@daniel-sanche
Copy link
Contributor

@oliviernguyenquoc

Can you explain what you're seeing vs what you expect to see? Cloud Run isn't yet officially supported by the library, so it won't be auto-detected. But you should still be able to see logs, and populate missing fields manually. Are you seeing any logs on Cloud Run at all?

I'm hoping to add full Cloud Run support this quarter. But we need to add better tests and make a couple other structural fixes first

@oliviernguyenquoc
Copy link

Logs are appearing well in cloud logging, but not with the right logging level.

But we would need to get noticed as soon as an error logging is triggered in production.
Meanwhile, we run in kind of blind mode.

Thanks for your work and we widely waiting for this feature :)

@daniel-sanche
Copy link
Contributor

Hmm I did a quick test with the code described in the docs and the severity fields seemed to work for me on Cloud Run.

If you can send me a container image that reproduces your issue, I can try to take a look and find a workaround for you

@daniel-sanche
Copy link
Contributor

This should be fixed after #200. You will see it in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants