Package: gcp-mixed-logging
GCP Mixed LogGCP Mixed Log 提供了统一的日志/数据接口, 在开发中代替 Python logging 及 Fluent-logging, 并附加额外信息规范化数据.Getting startedInstalling from PyPIpipinstallgcp-mixed-loggingCloud Logging使用到了Python 版 Cloud Logging 库, 权限配置参见官方文档Tips:
在本地和其他位置运行时, 需要使用Service Account或下方Auth说明中的第2种方法.Fluentd支持持久化日志转发至 Fluentd 或 Fluent Bit.Fluentd / Fluent Bit安装方法参见:FluentdFluent BitAPISummaryclass gcp_mixed_logging.MixedLoggingfrom gcp_mixed_logging import MixedLoggingmembers:cloudlogging_name: Cloud Loging pathis_alive: is alivemethodabountclose()停止接收日志, 缓存中日志写入Cloud Logging / Fluentddebug(msg)诊断日志, msg: str or dictinfo(msg)msg: str or dictwarning(msg)msg: str or dicterror(msg)msg: str or dictmetric(..)todopersist(tag, msg)持久化日志, 转发至FluentMixedLogging(module: str, stage: str...)Param:module: str: Module or sub-module namestage: str: stage, dev/test/prod/...fluent_host: str = 'localhost', fluentd host ip or dnsfluent_port: int = 24224, fluentd forward portproject: str = None, project name, if none, read from default authscopes: Optional[Collection[str]] = _DEFAULT_SCOPESS, scopes for default authresource: Resource = _GLOBAL_RESOURCE, resource of cloud loggingdebug/info/warning/error(msg: Any)Send message to Cloud Logging in background thread, append information:labels: module/stage/hostjsonPaylod: filename/function/lineno of caller frameParam:
msg: Any, text or dict messagepersist(tag: str, msg: dict, track: bool = False, track_severity: str = "DEFAULT", **kw)Foward message to Fluent in background thread, append information:host/timestampsinsert_id: increment intger per tag per hostParam:tag: str, tag name for fluentmsg: dict, payload data in dict typetrack: bool = False, both track to cloud loggingtrack_severity: str = "DEFAULT", severity of cloud loggingExamplefromgcp_mixed_loggingimportMixedLogging# using on GCE with local fluentlog=MixedLogging('module',stage='prod')# using with credential and remote fluent hostlog=MixedLogging('module',stage='prod',fluent_host='ip or dns',fluent_port=24224,project='project-id')# cloud logging: plain textlog.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 messagelog.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 logginglog.persist("user-info",{"user":"Mark","age":25},track=True)MoreGoogle AuthRefer togoogle.auth:If the environment variableGOOGLE_APPLICATION_CREDENTIALSis 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 theGoogle 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 loginIf the Cloud SDK has an active project, the project ID is returned. The active project can be set using:gcloud config set projectIf the application is running in theApp Engine standard environmentthen the credentials and project ID from theApp Identity Serviceare used.If the application is running inCompute Engineor theApp Engine flexible environmentthen the credentials and project ID are obtained from theMetadata Service.If no credentials are found,
:class:~google.auth.exceptions.DefaultCredentialsErrorwill be raised.