https://github.com/rai200890/python_google_cloud_logger
Python log formatter according to Google Cloud v2 Specification
https://github.com/rai200890/python_google_cloud_logger
google-cloud-logging logging python3
Last synced: 3 months ago
JSON representation
Python log formatter according to Google Cloud v2 Specification
- Host: GitHub
- URL: https://github.com/rai200890/python_google_cloud_logger
- Owner: rai200890
- License: mit
- Created: 2018-10-28T21:21:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-01T23:13:51.000Z (about 4 years ago)
- Last Synced: 2025-04-22T10:42:58.251Z (3 months ago)
- Topics: google-cloud-logging, logging, python3
- Language: Python
- Homepage:
- Size: 72.3 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python_google_cloud_logger
[](https://circleci.com/gh/rai200890/python_google_cloud_logger)
[](https://badge.fury.io/py/google-cloud-logger)
[](https://codeclimate.com/github/rai200890/python_google_cloud_logger/maintainability)
[](https://codeclimate.com/github/rai200890/python_google_cloud_logger/test_coverage)Python log formatter for Google Cloud according to [v2 specification](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry) using [python-json-logger](https://github.com/madzak/python-json-logger) formatter
Inspired by Elixir's [logger_json](https://github.com/Nebo15/logger_json)
## Instalation
### Pipenv
```
pipenv install google_cloud_logger
```### Pip
```
pip install google_cloud_logger
```## Usage
```python
LOG_CONFIG = {
"version": 1,
"formatters": {
"json": {
"()": "google_cloud_logger.GoogleCloudFormatter",
"application_info": {
"type": "python-application",
"name": "Example Application"
},
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s"
}
},
"handlers": {
"json": {
"class": "logging.StreamHandler",
"formatter": "json"
}
},
"loggers": {
"root": {
"level": "INFO",
"handlers": ["json"]
}
}
}
import loggingfrom logging import config
config.dictConfig(LOG_CONFIG) # load log config from dict
logger = logging.getLogger("root") # get root logger instance
logger.info("farofa", extra={"extra": "extra"}) # log message with extra arguments
```Example output:
```json
{"timestamp": "2018-11-03T22:05:03.818000Z", "severity": "INFO", "message": "farofa", "labels": {"type": "python-application", "name": "Example Application"}, "metadata": {"userLabels": {"extra": "extra"}}, "sourceLocation": {"file": "", "line": 1, "function": ""}}
```## Credits
Thanks [@thulio](https://github.com/thulio), [@robsonpeixoto](https://github.com/robsonpeixoto), [@ramondelemos](https://github.com/ramondelemos)