项目作者: rai200890

项目描述 :
Python log formatter according to Google Cloud v2 Specification
高级语言: Python
项目地址: git://github.com/rai200890/python_google_cloud_logger.git
创建时间: 2018-10-28T21:21:45Z
项目社区:https://github.com/rai200890/python_google_cloud_logger

开源协议:MIT License

下载


python_google_cloud_logger

CircleCI
PyPI version
Maintainability
Test Coverage

Python log formatter for Google Cloud according to v2 specification using python-json-logger formatter

Inspired by Elixir’s logger_json

Instalation

Pipenv

  1. pipenv install google_cloud_logger

Pip

  1. pip install google_cloud_logger

Usage

  1. LOG_CONFIG = {
  2. "version": 1,
  3. "formatters": {
  4. "json": {
  5. "()": "google_cloud_logger.GoogleCloudFormatter",
  6. "application_info": {
  7. "type": "python-application",
  8. "name": "Example Application"
  9. },
  10. "format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s"
  11. }
  12. },
  13. "handlers": {
  14. "json": {
  15. "class": "logging.StreamHandler",
  16. "formatter": "json"
  17. }
  18. },
  19. "loggers": {
  20. "root": {
  21. "level": "INFO",
  22. "handlers": ["json"]
  23. }
  24. }
  25. }
  26. import logging
  27. from logging import config
  28. config.dictConfig(LOG_CONFIG) # load log config from dict
  29. logger = logging.getLogger("root") # get root logger instance
  30. logger.info("farofa", extra={"extra": "extra"}) # log message with extra arguments

Example output:

  1. {"timestamp": "2018-11-03T22:05:03.818000Z", "severity": "INFO", "message": "farofa", "labels": {"type": "python-application", "name": "Example Application"}, "metadata": {"userLabels": {"extra": "extra"}}, "sourceLocation": {"file": "<ipython-input-9-8e9384d78e2a>", "line": 1, "function": "<module>"}}

Credits

Thanks @thulio, @robsonpeixoto, @ramondelemos