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

Unintended traceback omittion #735

Closed
shkumagai opened this issue Mar 28, 2023 · 1 comment · Fixed by #736
Closed

Unintended traceback omittion #735

shkumagai opened this issue Mar 28, 2023 · 1 comment · Fixed by #736
Assignees
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@shkumagai
Copy link
Contributor

After PR #394 was merged, tracebacks are no longer duplicated after structured log messages.

However, the effect of that change is not limited to StructuredLogHandler.

When you try to use StructuredLogHandler in conjunction with some other log handlers as follows, you will encounter an unintended problem.
Specifically, any log handlers will be called later than the StructuredLogHandler will never output traceback because they can not detect record.exc_info and record.exc_text. Both are set to None.
This behaviour is caused by the record object has changed directly in StructuredLogHander.

Code example

import logging

from google.cloud.logging.handlers import StructuredLogHandler


class SampleHandler(logging.StreamHandler): ...


logger = logging.getLogger()
logger.handlers.clear()
logger.addHandler(StructuredLogHandler())
logger.addHandler(SampleHandler())

try:
    raise Exception("Test")
except Exception:
    logger.exception("Failed")

print("----------")

Output

$ python run1.py
{"logging.googleapis.com/diagnostic": {"instrumentation_source": [{"name": "python", "version": "3.5.0"}]},"severity": "INFO", "logging.googleapis.com/labels": {"python_logger": "google.cloud.logging_v2.handlers.structured_log"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 137, "file": "/Users/shoji.kumagai/work/temp/logging_test/google-cloud-logging/.venv/lib/python3.11/site-packages/google/cloud/logging_v2/handlers/structured_log.py", "function": "emit_instrumentation_info"}, "httpRequest": {} }
{"logging.googleapis.com/diagnostic": {"instrumentation_source": [{"name": "python", "version": "3.5.0"}]},"severity": "INFO", "logging.googleapis.com/labels": {"python_logger": "google.cloud.logging_v2.handlers.structured_log"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 137, "file": "/Users/shoji.kumagai/work/temp/logging_test/google-cloud-logging/.venv/lib/python3.11/site-packages/google/cloud/logging_v2/handlers/structured_log.py", "function": "emit_instrumentation_info"}, "httpRequest": {} }
{'logging.googleapis.com/diagnostic': {'instrumentation_source': [{'name': 'python', 'version': '3.5.0'}]}}
{"message": "Failed\nTraceback (most recent call last):\n  File \"/Users/shoji.kumagai/work/temp/logging_test/google-cloud-logging/run1.py\", line 16, in <module>\n    raise Exception(\"Test\")\nException: Test","severity": "ERROR", "logging.googleapis.com/labels": {"python_logger": "root"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 18, "file": "/Users/shoji.kumagai/work/temp/logging_test/google-cloud-logging/run1.py", "function": "<module>"}, "httpRequest": {} }
Failed
----------

As you can see above, the structured log message is output without traceback intendedly.
On the other hand, the log message of SampleHandler is also output without traceback though it's not intended.

Suggestion

  1. Never use the original record object in the StructuredLogHander, but use its copied one
  2. Use the custom Formatter which overridden format() method to omit any traceback
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label Mar 28, 2023
@losalex losalex added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 28, 2023
@losalex losalex assigned daniel-sanche and unassigned losalex Mar 28, 2023
@rushilsrivastava
Copy link

I'm still strangely seeing this in 3.9.0, all errors are duplicated by structured_logger and at the INFO level

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: p2 Moderately-important priority. 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.

4 participants