{"id":21397580,"url":"https://github.com/multani/structlog-gcp","last_synced_at":"2025-04-06T07:09:51.271Z","repository":{"id":149920767,"uuid":"620534840","full_name":"multani/structlog-gcp","owner":"multani","description":"Google Cloud Logging formatter for structlog","archived":false,"fork":false,"pushed_at":"2025-03-24T08:11:01.000Z","size":399,"stargazers_count":31,"open_issues_count":6,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T09:26:33.828Z","etag":null,"topics":["gcp","google-cloud-logging","google-cloud-platform","logging","python","structlog"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/structlog-gcp/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multani.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-28T21:54:49.000Z","updated_at":"2025-03-24T08:11:04.000Z","dependencies_parsed_at":"2024-11-22T14:43:47.101Z","dependency_job_id":"4a742895-fc60-4c95-8ee5-154a9e7c0759","html_url":"https://github.com/multani/structlog-gcp","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fstructlog-gcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fstructlog-gcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fstructlog-gcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fstructlog-gcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multani","download_url":"https://codeload.github.com/multani/structlog-gcp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445667,"owners_count":20939958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gcp","google-cloud-logging","google-cloud-platform","logging","python","structlog"],"created_at":"2024-11-22T14:43:37.633Z","updated_at":"2025-04-06T07:09:51.249Z","avatar_url":"https://github.com/multani.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Google Cloud Logging formatter for `structlog`\n\nThis is an opiniated package that configures [structlog](https://structlog.org/)\nto output log compatible with the [Google Cloud Logging log\nformat](https://cloud.google.com/logging/docs/structured-logging).\n\nThe intention of this package is to be used for applications that run in [Google\nKubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/) or [Google\nCloud Function](https://cloud.google.com/functions/), or any other systems that\nknow how to send logs to Google Cloud.\n\nAs such, the package is only concerned about **formatting logs**, where logs are\nexpected to be written on the standard output. Sending the logs to the actual\nGoogle Logging API is supposed to be done by an external agent.\n\n\nIn particular, this package provides the following configuration by default:\n\n* Logs are formatted as JSON using the [Google Cloud Logging log format](https://cloud.google.com/logging/docs/structured-logging)\n* The [Python standard library's `logging`](https://docs.python.org/3/library/logging.html)\n  log levels are available and translated to their GCP equivalents.\n* Exceptions and `CRITICAL` log messages will be reported into [Google Error Reporting dashboard](https://cloud.google.com/error-reporting/)\n* Additional logger bound arguments will be reported into the `jsonPayload` event.\n\n\n## How to use?\n\nInstall the package with `pip` or your favorite Python package manager:\n\n```sh\npip install structlog-gcp\n```\n\nThen, configure `structlog` as usual, using the Structlog processors the package\nprovides:\n\n```python\nimport structlog\nimport structlog_gcp\n\nprocessors = structlog_gcp.build_processors()\nstructlog.configure(processors=processors)\n```\n\nThen, you can use `structlog` as usual:\n\n```python\nlogger = structlog.get_logger().bind(arg1=\"something\")\n\nlogger.info(\"Hello world\")\n\nconverted = False\ntry:\n    int(\"foobar\")\n    converted = True\nexcept:\n    logger.exception(\"Something bad happens\")\n\nif not converted:\n    logger.critical(\"This is not supposed to happen\", converted=converted)\n```\n\nThe `structlog_gcp.build_processors()` function constructs structlog processors to:\n\n* Output logs as Google Cloud Logging format using the default Python JSON serializer.\n* Carry context variables across loggers (see [structlog: Context Variables](https://www.structlog.org/en/stable/contextvars.html))\n\nFor more advanced usage, see [Advanced Configuration](#advanced-configuration)\n\n\n### Errors\n\nErrors are automatically reported to the [Google Error Reporting service](https://cloud.google.com/error-reporting/).\n\nYou can configure the service name and the version used during the report with 2 different ways:\n\n* By default, the library assumes to run with Cloud Function environment\n  variables configured, in particular [the `K_SERVICE` and `K_REVISION` variables](https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically).\n* You can also pass the service name and revision at configuration time with:\n\n  ```python\n  import structlog\n  import structlog_gcp\n\n  processors = structlog_gcp.build_processors(\n      service=\"my-service\",\n      version=\"v1.2.3\",\n  )\n  structlog.configure(processors=processors)\n  ```\n\n### Advanced Configuration\n\nIf you need to have more control over the processors configured by the library, you can use the `structlog_gcp.build_gcp_processors()` builder function.\n\nThis function only configures the Google Cloud Logging-specific processors and omits all the rest.\n\nIn particular, you can use this function:\n\n* If you want to have more control over the processors to be configured in structlog. You can prepend or append other processors around the Google-specific ones.\n* If you want to serialize using another JSON serializer or with specific options.\n\nFor instance:\n\n\n```python\nimport orjson\nimport structlog\nfrom structlog.processors import JSONRenderer\n\nimport structlog_gcp\n\n\ndef add_open_telemetry_spans(...):\n    # Cf. https://www.structlog.org/en/stable/frameworks.html#opentelemetry\n    ...\n\ngcp_processors = structlog_gcp.build_gcp_processors()\n\n# Fine-tune processors\nprocessors = [add_open_telemetry_spans]\nprocessors.extend(gcp_processors)\nprocessors.append(JSONRenderer(serializer=orjson.dumps))\n\nstructlog.configure(processors=processors)\n```\n\n\u003e [!IMPORTANT]\n\u003e\n\u003e `structlog_gcp.build_gcp_processors()` **doesn't** configure a renderer and\n\u003e you must supply a JSON renderer of your choice for the library to work\n\u003e correctly.\n\n\n## Examples\n\nCheck out the [`examples` folder](https://github.com/multani/structlog-gcp/tree/main/examples) to see how it can be used.\n\n* How it should appear in the Google Cloud Logging log explorer:\n  ![](https://raw.githubusercontent.com/multani/structlog-gcp/main/docs/logs.png)\n\n* How it should appear in the Google Cloud Error Reporting dashboard:\n  ![](https://raw.githubusercontent.com/multani/structlog-gcp/main/docs/errors.png)\n\n\n## Reference\n\n* https://cloud.google.com/logging/docs/structured-logging\n* https://cloud.google.com/error-reporting/docs/formatting-error-messages\n* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultani%2Fstructlog-gcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultani%2Fstructlog-gcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultani%2Fstructlog-gcp/lists"}