logger

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 7 Imported by: 0

README

unalogger

logger

Opinionated logxi based logging with the option of passing errors on to Sentry


Getting started

Install

To install logger pull it with go get like this

go get github.com/unacast/logger

or if you use dep just do a

dep ensure -add github.com/unacast/logger

oooor if you use glide just do a

glide get github.com/unacast/logger
glide install
Usage expamles
Regular stdout logging
package main
import (
    "github.com/unacast/logger"
    "errors"
)

var log = logger.New("main")

func main() {

    // Info takes a message and a var args list of key-value pairs that are 
    log.Info(
        "Log message",
        "jobID", "1312313", "count", 1000,
    )
    
    // Error takes an error as the second argument and it's added to 
    // the list of key-value pairs with "error" as the key 
    err := errors.New("This is an error!")
    log.Info(
        "Error log message",
        err,
        "jobID", "1312313", "count", 1000,
    )
}
Logging to custom logfile, e.g. for apps running on Appengine flex

It might be a good idea to create a local logger package to do the setup with the desired filename etc.

package logger
import (
    "github.com/unacast/logger"
)

const logFileEnv = "UC_[APP NAME HERE]_LOG_FILE"

var (
	logFileName = os.Getenv(logFileEnv)
)

func New(name string) unalogger.UnaLogger {
	return unalogger.NewLogger(
        unalogger.Config{
            Name: name, 
            FileName: logFileName
        }
     )
}
Use the Stackdriver Error Reporting functionality

unalogger has an integration to optionally handle panics and report them as errors to Stackdriver Error Reporting. Initialise the errorClient in the start of your program, typically in main.go.

func main() {

	consts := constants.Consts

	// Enable Stackdriver Errorreporting
	errorClient, recoverPanics := logger.SetUpErrorReporting(context.Background(), consts.Project, "shipit", consts.GitSha)
    // Defering this function handles panics in this scope/goroutine
	defer recoverPanics()
    // Strictly not necessary, 
	defer errorClient.Close()
    ...
}

If you have several sub-goroutines in your app, you have to pass the recoverPanics into them and defer recoverPanics locally to handle panics there.

Loggers

Logxi defaults to using a json formatter in production and a typical log line looks like this

{"_t":"2017-08-25T15:04:44+0200", "_p":"20698", "severity":"INFO", "_n":"main", "message":"The service has successfully launched"}

Some of the keys has been renamed to confrom with the Google Cloud Logging format

Locally logxi uses a formatter that outputs lines like this

15:07:59.502228 INFO goa mount ctrl: K8sStatuses action: Liveness
   route: GET /liveness

Maintainers

  • @torbjornvatn

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExitOnPanic = true

ExitOnPanic makes it possible to disable exiting in ReportPanics

Functions

func CloseClient

func CloseClient()

CloseClient should be deferred right after calling InitErrorReporting to enure that the client is closed down gracefully

func InitErrorReporting

func InitErrorReporting(ctx context.Context, projectID, serviceName, serviceVersion string) error

InitErrorReporting will enable the errors of all calls to Error and Fatal to be sent to Google Error Reporting It also enables the

func ReportPanics

func ReportPanics(ctx context.Context) func()

ReportPanics should be defered in every new scope where you want to catch pancis and have them pass on to Stackdriver Error Reporting

func SetUpErrorReporting deprecated

func SetUpErrorReporting(ctx context.Context, projectID, serviceName, serviceVersion string) (client *errorreporting.Client, reportPanics func())

Deprecated: The functionality is split into InitErrorReporting, ReportPanics and CloseClient instead SetUpErrorReporting creates an ErrorReporting client and returns that client together with a reportPanics function. That function should be defered in every new scope where you want to catch pancis and have them pass on to Stackdriver Error Reporting

Types

type Config

type Config struct {
	Name     string
	FileName string
}

Config contains Name and FileName for the logger

type UnaLogger

type UnaLogger interface {
	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Error(msg string, err error, args ...interface{})
	Fatal(msg string, err error, args ...interface{})
	Underlying() log.Logger
}

UnaLogger wraps a logxi logger and delegate to some of it's logging methods

func New

func New(name string) UnaLogger

New creates a new logger with the given (string) name

func NewLogger

func NewLogger(conf Config) UnaLogger

NewLogger creates a new logger with the given (Config) name

Jump to

Keyboard shortcuts

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