logu

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2020 License: MIT Imports: 8 Imported by: 0

README

logu GoDoc Build Status Go Report Card

Injectable wrappers around Google AppEngine logging for Go.

logu is an interface around the Google AppEngine log package that integrates well with dependency injection. An implementation of the logging interface is also provided for testing, a null logger implementation which throws away all log messages, as well as an implementation that writes to the go log package. Mocks generated by mockgen are also provided as part of this package.

A full example of using logu along with erru and di is available here.

Documentation

Overview

Package logu contains injectable wrappers around AppEngine logging.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAppEngineDiDefs

func NewAppEngineDiDefs() []*di.Def

NewAppEngineDiDefs returns di definitions suitable for a project run in Google's AppEngine.

Types

type Logger

type Logger interface {
	// Debugf prints a formatted message intended for debugging.
	Debugf(format string, args ...interface{})

	// Errorf prints a formatted error message to the log.
	Errorf(format string, args ...interface{})

	// Infof prints a formatted info message to the log.
	Infof(format string, args ...interface{})

	// Warningf prints a formatted warning message to the log.
	Warningf(format string, args ...interface{})
}

Logger represents a general interface around the logging implementation

Example
package main

import (
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/clavoie/di/v2"
	"github.com/clavoie/erru"
	"github.com/clavoie/logu"
)

func onResolveErr(err *di.ErrResolve, w http.ResponseWriter, r *http.Request) {
	logger := logu.NewAppEngineLogger(r)
	logger.Errorf("err encountered while resolving dependencies: %v", err.String())

	httpErr, isHttpErr := err.Err.(erru.HttpErr)
	if isHttpErr {
		w.WriteHeader(httpErr.StatusCode())
	} else {
		w.WriteHeader(http.StatusInternalServerError)
	}
}

type Dependency interface {
	DoWork() bool
}

type dependency struct{}

func (d *dependency) DoWork() bool { return false }
func NewDependency() Dependency    { return new(dependency) }

var defs = []*di.Def{{NewDependency, di.PerHttpRequest}}

func MyHandler(logger logu.Logger, dep Dependency) {
	result := dep.DoWork()

	if result == false {
		logger.Errorf("Value for result: %v", result)
	}
}

func main() {
	resolver, err := di.NewResolver(onResolveErr, defs, logu.NewAppEngineDiDefs())

	if err != nil {
		log.Fatal(err)
	}

	httpHandler, err := resolver.HttpHandler(MyHandler)

	if err != nil {
		log.Fatal(err)
	}

	req := httptest.NewRequest("GET", "http://example.com/foo", nil)
	w := httptest.NewRecorder()
	httpHandler(w, req)
}
Output:

func NewAppEngineLogger

func NewAppEngineLogger(r *http.Request) Logger

NewAppEngineLogger creates a new logging implementation from the current http request that logs to the Google AppEngine.

func NewGoLogger

func NewGoLogger() Logger

goLogger is an implementation of Logger that wraps the Go logging package:

https://golang.org/pkg/log/

Only calls to Print, Printf, and Println are made from this implementation. No calls to any of the Fatal or Panic functions are ever made.

Info and Debug logs are written to Stdout, Warning and Error logs are written to Stderr. Log lines are written with a prefix of '[TYPE] ', such that Info logs have a prefix of '[INFO] ', error logs have a prefix of '[ERROR] ', etc. log.LstdFlags are used for all logs.

func NewNullLogger

func NewNullLogger() Logger

NewNullLogger returns a new Logger implementation that does nothing. All logging messages are destroyed.

func NewTestLogger

func NewTestLogger(t *testing.T) Logger

NewTestLogger returns a new Logger implementation that logs for tests.

Directories

Path Synopsis
Package mock_logu is a generated GoMock package.
Package mock_logu is a generated GoMock package.

Jump to

Keyboard shortcuts

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