log

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package log provides wrappers around logging for gosecure. By default gosecure uses the log package from the standard library, bit it can support any log handler.

Index

Examples

Constants

View Source
const InvalidLevel = "invalid level"

InvalidLevel is the key used to record an invalid log level.

Variables

This section is empty.

Functions

func Handle

func Handle(level, msg string, detail Detail)

Handle a log event using the Listener set on DefaultListener. If this is nil do nothing.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gosecure/log"
)

func main() {
	// switch to log recorder temporarily
	listener := log.DefaultListener
	recorder := &log.Recorder{}
	log.DefaultListener = recorder

	defer func() { log.DefaultListener = listener }()

	// Call the handler
	log.Handle("Error", "Something went wrong", log.Detail{"k": "v"})

	// Output the result
	fmt.Println(recorder.Output)

}
Output:

Error Something went wrong map[k:v]

func Handler

func Handler(next http.Handler) http.Handler

Handler is middleware that logs each HTTP request

Types

type Detail

type Detail map[string]interface{}

Detail is used to hold details of log message passed to a Listener.

type Listener

type Listener interface {

	// Handle an log event at the given level.
	Handle(level, msg string, detail Detail)
}

A Listener is used to listen for log events generated by gosecure and handle them accordingly.

var DefaultListener Listener

DefaultListener is the Listener used by the Handle function, and by extension all gosecure functions. By default it is nil and all log events are discarded.

type LogrusListener

type LogrusListener struct{}

LogrusListener reports using the default logrus logger.

func (LogrusListener) Handle

func (LogrusListener) Handle(level, msg string, detail Detail)

Handle errors using logrus. Invalid levels are logged as Errors and the invalid level included in the output.

type Recorder

type Recorder struct {
	Output string
}

Recorder records the last error as a string in Output

func (*Recorder) Handle

func (r *Recorder) Handle(level, msg string, detail Detail)

Handle an error by setting Recorder.Output to the error.

Jump to

Keyboard shortcuts

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