logu

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: MIT Imports: 8 Imported by: 1

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/v2"
)

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

NewGoLogger creates an implementation of Logger that uses the Go logging package to write to os.Stdout for debug and info messages, and os.Stderr for warning and error messagees:

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 NewLogPkgLogger

func NewLogPkgLogger() Logger

NewLogPkgLogger creates an an implementation of Logger that uses the Go logging package to write to the standard logger:

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.

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_v2 is a generated GoMock package.
Package mock_v2 is a generated GoMock package.

Jump to

Keyboard shortcuts

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