Sdhook
Package sdhook provides a Google Stackdriver logging hook for logrus
Install / Use
/learn @kenshaw/SdhookREADME
About sdhook
Package sdhook provides a logrus compatible hook for Google Stackdriver logging.
Installation
Install in the usual Go way:
go get -u github.com/kenshaw/sdhook
Usage
Simply create the hook, and add it to a logrus logger:
// create hook using service account credentials
h, err := sdhook.New(
sdhook.GoogleServiceAccountCredentialsFile("./credentials.json"),
)
// create logger with extra fields
//
// logrus fields will be converted to Stackdriver labels
logger := logrus.New().WithFields(logrus.Fields{
"field1": 15,
"field2": 20,
})
// add hook
logger.Hooks.Add(h)
// log something
logger.Printf("something %d", 15)
The example above sends log entries directly to the logging API. If you have the logging agent running, you can send log entries to it instead, with the added benefit of having extra instance metadata added to your log entries by the agent. In the example above, the initialization would simply be:
// create hook using the logging agent
h, err := sdhook.New(
sdhook.GoogleLoggingAgent(),
)
Please also see _example/example.go for a more complete example.
Error Reporting
If you'd like to enable sending errors to Google's Error Reporting (https://cloud.google.com/error-reporting/), you have to set the name of the service, app or system you're running. Following the example above, the initialization would then be:
// create hook using the logging agent
h, err := sdhook.New(
sdhook.GoogleLoggingAgent(),
sdhook.ErrorReportingService("your-great-app"),
)
The value of the ErrorReportingService function parameter above corresponds
to the string value you'd like to see in the service field of the Error
Reporting payload, as defined by https://cloud.google.com/error-reporting/docs/formatting-error-messages
Also note that, if you enable error reporting, errors and messages of more
severe levels go into the error log and will not be displayed in the regular
log. The error log name is either defined by the ErrorReportingLogName
function or defaults to <regular-log-name>_errors. This fulfills Google's
Error Reporting requirement that the log name should have the string err in
its name. See more in: https://cloud.google.com/error-reporting/docs/setup/ec2
To fulfill Google's Error Reporting requirement of a payload containing error
stack frame information (file name, function name and line number), it assumes
that this information has been added as a logrus.Field of name caller and
type stack.Frame from Facebook's stack package.
One way to easily achieve this transparently is to use another logrus Hook like
Gurpartap's logrus-stack.
See GoDoc for a full API listing.
Related Skills
node-connect
349.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.7kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
