Categorygithub.com/kenshaw/sdhook
modulepackage
0.3.0
Repository: https://github.com/kenshaw/sdhook.git
Documentation: pkg.go.dev

# README

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.

# Functions

EntriesService is an option that sets the Google API entry service to use with Stackdriver.
ErrorReportingLogName is an option that sets the log name to send with each error message for error reporting.
ErrorReportingService is an option that defines the name of the service being tracked for Stackdriver error reporting.
ErrorService is an option that sets the Google API error reporting service to use.
GoogleComputeCredentials is an option that loads the Google Service Account credentials from the GCE metadata associated with the GCE compute instance.
No description provided by the author
GoogleServiceAccountCredentialsFile is an option that loads Google Service Account credentials for use with the StackdriverHook from the specified file.
GoogleServiceAccountCredentialsJSON is an option that creates the Stackdriver logging service using the supplied Google service account credentials.
HTTPClient is an option that sets the http.Client to be used when creating the Stackdriver service.
Labels is an option that sets the labels to send with each log entry.
Levels is an option that sets the logrus levels that the StackdriverHook will create log entries for.
LoggingService is an option that sets the Google API logging service to use.
LogName is an option that sets the log name to send with each log entry.
MonitoredResource is an option that sets the monitored resource to send with each log entry.
New creates a StackdriverHook using the provided options that is suitible for using with logrus for logging to Google Stackdriver.
PartialSuccess is an option that toggles whether or not to write partial log entries.
ProjectID is an option that sets the project ID which is needed for the log name.
Resource is an option that sets the resource information to send with each log entry.
UseLoggingServiceForErrors is an option that reports errors to Stackdriver error reporting via the logging API.

# Constants

ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.
ResType values.

# Structs

Hook provides a Google Stackdriver logging hook for use with logrus.

# Type aliases

Option represents an option that modifies the Stackdriver hook settings.
ResType is a monitored resource descriptor type.