write log to cloud logging or fluentd, for app in gcp
Project description
GCP Mixed Log
GCP Mixed Log 提供了统一的日志/数据接口, 在开发中代替 Python logging 及 Fluent-logging, 并附加额外信息规范化数据.
Getting started
Installing from PyPI
pip install gcp-mixed-logging
Cloud Logging
使用到了Python 版 Cloud Logging 库, 权限配置参见官方文档
Tips: 在本地和其他位置运行时, 需要使用Service Account或下方Auth说明中的第2种方法.
Fluentd
支持持久化日志转发至 Fluentd 或 Fluent Bit.
Fluentd / Fluent Bit安装方法参见:
API
Summary
class gcp_mixed_logging.MixedLogging
from gcp_mixed_logging import MixedLogging
members:
- cloudlogging_name: Cloud Loging path
- is_alive: is alive
method | abount |
---|---|
close() | 停止接收日志, 缓存中日志写入Cloud Logging / Fluentd |
debug(msg) | 诊断日志, msg: str or dict |
info(msg) | msg: str or dict |
warning(msg) | msg: str or dict |
error(msg) | msg: str or dict |
metric(..) | todo |
persist(tag, msg) | 持久化日志, 转发至Fluent |
MixedLogging(module: str, stage: str...)
Param:
- module: str: Module or sub-module name
- stage: str: stage, dev/test/prod/...
- fluent_host: str = 'localhost', fluentd host ip or dns
- fluent_port: int = 24224, fluentd forward port
- project: str = None, project name, if none, read from default auth
- scopes: Optional[Collection[str]] = _DEFAULT_SCOPESS, scopes for default auth
- resource: Resource = _GLOBAL_RESOURCE, resource of cloud logging
debug/info/warning/error(msg: Any)
Send message to Cloud Logging in background thread, append information:
- labels: module/stage/host
- jsonPaylod: filename/function/lineno of caller frame Param: msg: Any, text or dict message
persist(tag: str, msg: dict, track: bool = False, track_severity: str = "DEFAULT", **kw)
Foward message to Fluent in background thread, append information:
- host/timestamps
- insert_id: increment intger per tag per host
Param:
- tag: str, tag name for fluent
- msg: dict, payload data in dict type
- track: bool = False, both track to cloud logging
- track_severity: str = "DEFAULT", severity of cloud logging
Example
from gcp_mixed_logging import MixedLogging
# using on GCE with local fluent
log = MixedLogging('module', stage='prod')
# using with credential and remote fluent host
log = MixedLogging(
'module', stage='prod',
fluent_host='ip or dns', fluent_port=24224,
project='project-id'
)
# cloud logging: plain text
log.debug("this is a debug message")
log.info("this is a info message")
log.warn("this is a warn message")
log.error("this is a error message")
# cloud logging: struct message
log.info({
"user": "Mark",
"age": 25
})
# fluent:
# 1. time append to log
# 2. send to fluent with tag: 'module-prod.user-info'
log.persist("user-info", {
"user": "Mark",
"age": 25
})
# foward log to fluent and cloud logging
log.persist("user-info", {
"user": "Mark",
"age": 25
}, track=True)
More
Google Auth
Refer to google.auth
:
-
If the environment variable
GOOGLE_APPLICATION_CREDENTIALS
is set to the path of a valid service account JSON private key file, then it is loaded and returned. The project ID returned is the project ID defined in the service account file if available (some older files do not contain project ID information). -
If the
Google Cloud SDK
_ is installed and has application default credentials set they are loaded and returned.To enable application default credentials with the Cloud SDK run:
gcloud auth application-default login
If the Cloud SDK has an active project, the project ID is returned. The active project can be set using:
gcloud config set project
-
If the application is running in the
App Engine standard environment
then the credentials and project ID from theApp Identity Service
are used. -
If the application is running in
Compute Engine
or theApp Engine flexible environment
then the credentials and project ID are obtained from theMetadata Service
. -
If no credentials are found, :class:
~google.auth.exceptions.DefaultCredentialsError
will be raised.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gcp-mixed-logging-0.0.9.tar.gz
.
File metadata
- Download URL: gcp-mixed-logging-0.0.9.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5017516cf26ece83015444f32b7f9f2b5567dab0d2994580134473356db23b45 |
|
MD5 | 1c9c0f0b33ce3fad255b4af7fba93133 |
|
BLAKE2b-256 | 5acf5fd03096c7ba8859fe5d5f03ea5720fccb024bd144ed56cbccecd674800d |
File details
Details for the file gcp_mixed_logging-0.0.9-py3-none-any.whl
.
File metadata
- Download URL: gcp_mixed_logging-0.0.9-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01362a100bed81cc60ec2a9b9a5dcd0792baea84c27bc580ca73227f2a80f9bb |
|
MD5 | d9029897dceddd5bd48d9bc22754c867 |
|
BLAKE2b-256 | cd48988a05b400c44cc19d843f063abd83d5cd913263df5d6995160a0ea631ef |