gcploghook

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

README

gcploghook

I used the blog article from Huy Ngo 4 tips for logging on GCP using golang and logrus as a base for this small module. It is basically all that is needed to log from a go app to Google Cloud Logging. An example how to use this in a go app:

package main

import (
    "os"

    stackdriver "github.com/andyfusniak/stackdriver-gae-logrus-plugin"
    "github.com/sirupsen/logrus"
    "github.com/jum/logrus-gcploghook"
)

main() |{
    googleProject := os.Getenv("GOOGLE_CLOUD_PROJECT")
    myLog := logrus.New()
    formatter := stackdriver.GAEStandardFormatter(
        stackdriver.WithProjectID(googleProject),
    )
    myLog.SetFormatter(formatter)
    myLog.SetOutput(os.Stdout)
    log_name := os.Getenv("LOG_NAME")
    if log_name != "" {
        hook, err := gcploghook.NewStackDriverHook(googleProject, log_name, os.Getenv("LOG_INSTANCEID"), os.Getenv("LOG_INSTANCENAME"), os.Getenv("LOG_INSTANCEZONE"))
        if err != nil {
            myLog.WithError(err).Fatal("StackDriver")
        }
        myLog.AddHook(hook)
        myLog.SetOutput(io.Discard)
    }
    myLog.Info("Hello world!")
}

The environment variable GOOGLE_APPLICATION_CREDENTIALS needs to point to your credential .json file downloaded from Google Cloud Console for the project named in GOOGLE_CLOUD_PROJECT. The environment variable LOG_NAME determines if the log is send via the Google Cloud logging API, if this is unset the code is assumed to run inside the Google Cloud Environment already, e.g. dockerized as a Google Cloud Run container and stdout receives the logs.

Documentation

Overview

Package gcloghook implements a hook for the github.com/sirupsen/logrus package to send structured log records to the Google Cloud Logging service.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StackDriverHook

type StackDriverHook struct {
	io.Closer
	// contains filtered or unexported fields
}

func NewStackDriverHook

func NewStackDriverHook(googleProject string, logName string, logInstanceID string, logInstanceName string, logInstanceZone string) (*StackDriverHook, error)

NewStackDriverHook allocates a new hook ready to be used for logrus.AddHook. The hook will send log records to the Google Cloud Logging API, and if the logrus error record uses a level of ErrorLevel or higher, the record will also be forwarded to the Error reporting API.

Example
googleProject := os.Getenv("GOOGLE_CLOUD_PROJECT")
myLog := logrus.New()
formatter := stackdriver.GAEStandardFormatter(
	stackdriver.WithProjectID(googleProject),
)
myLog.SetFormatter(formatter)
myLog.SetOutput(os.Stdout)
log_name := os.Getenv("LOG_NAME")
if log_name != "" {
	hook, err := NewStackDriverHook(googleProject, log_name, os.Getenv("LOG_INSTANCEID"), os.Getenv("LOG_INSTANCENAME"), os.Getenv("LOG_INSTANCEZONE"))
	if err != nil {
		myLog.WithError(err).Fatal("StackDriver")
	}
	myLog.AddHook(hook)
	myLog.SetOutput(io.Discard)
}
myLog.Info("Hello world!")
Output:

func (*StackDriverHook) Close

func (sh *StackDriverHook) Close() error

func (*StackDriverHook) Fire

func (sh *StackDriverHook) Fire(entry *logrus.Entry) error

func (*StackDriverHook) Levels

func (sh *StackDriverHook) Levels() []logrus.Level

func (*StackDriverHook) Wait

func (sh *StackDriverHook) Wait()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL