logger

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

README

resurfaceio-logger-go

Easily log API requests and responses to your own system of record.

pkg.go.dev documentation for this Go module.

Contents

Installation

In the same directory as your project's go.mod and go.sum files.

go get github.com/resurfaceio/logger-go

Setup the Resurface app

You can also find these instructions here.

If you don't already have Docker installed, you can do so by following these instructions.

From the terminal, run this Docker command to start up the Resurface app using docker.

docker run -d --name resurface -p 4000:4000 -p 4001:4001 -p 4002:4002 resurfaceio/resurface:2.3.1

Point your browser at http://localhost:4002.

Logging from gorilla/mux

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/resurfaceio/logger-go" //<----- 1
)


func main() {
	router := mux.NewRouter()
  
	options := logger.Options{ //<----- 2
		Rules:   "include_debug\n",
		Url:     "http://localhost:4001/message",
		Enabled: true,
		Queue:   nil,
	}

	httpLoggerForMux, err := logger.NewHttpLoggerForMuxOptions(options) //<----- 3

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

	router.Use(httpLoggerForMux.LogData) //<----- 4

	log.Fatal(http.ListenAndServe(":5000", router))
}

Protecting User Privacy

Loggers always have an active set of rules that control what data is logged and how sensitive data is masked. All of the examples above apply a predefined set of rules, include_debug, but logging rules are easily customized to meet the needs of any application.

Logging rules documentation

Documentation

Overview

Resurface Go Logger provides tools to log API requests and responses from different Golang web frameworks to a complete API system of record. (https://resurface.io)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendHttpMessage

func SendHttpMessage(logger *HttpLogger, resp *http.Response, req *http.Request, start time.Time)

SendHttpMessage(l *HttpLogger, resp *http.Response, req *http.Request, t time.Time) Uses logger l to send a log of the given resp, req, and t to the loggers url t defines the start time of the logging process used to calculate the logging interval

Types

type HttpLogger added in v1.1.0

type HttpLogger struct {
	// contains filtered or unexported fields
}

HttpLogger is the struct contains a pointer to a baseLogger instance and a set of rules used to define the behaviour of the logger.

func NewHttpLogger

func NewHttpLogger(options Options) (*HttpLogger, error)

NewHttpLogger returns a pointer to a new HttpLogger object, with the given options applied, and an error

func (HttpLogger) Disable added in v1.1.0

func (logger HttpLogger) Disable()

func (HttpLogger) Enable added in v1.1.0

func (logger HttpLogger) Enable()

func (HttpLogger) Enabled added in v1.1.0

func (logger HttpLogger) Enabled() bool

func (HttpLogger) Queue added in v1.2.0

func (logger HttpLogger) Queue() []string

type HttpLoggerForMux

type HttpLoggerForMux struct {
	HttpLogger *HttpLogger
	// contains filtered or unexported fields
}

HttpLoggerForMux defines a struct used to log specifically gorilla/mux apps

func NewHttpLoggerForMux

func NewHttpLoggerForMux() (*HttpLoggerForMux, error)

NewHttpLoggerForMux returns a pointer to an instance of an HttpLoggerForMux struct with the default options applied and an error. If there is no error, the error value returned will be nil.

func NewHttpLoggerForMuxOptions

func NewHttpLoggerForMuxOptions(options Options) (*HttpLoggerForMux, error)

NewHttpLoggerForMuxOptions(o Options) returns a pointer to a HttpLoggerForMux struct with the given options o applied and an error. If there is no error, the error value returned will be nil.

func (HttpLoggerForMux) LogData

func (muxLogger HttpLoggerForMux) LogData(next http.Handler) http.Handler

LogData() takes 1 argument of type http.Handler and returns an object of the same type, http.Handler. This function is intended to be used in a Middleware function in a gorilla/mux server. For details on how to setup Middleware for a mux server see: https://github.com/resurfaceio/logger-go#logging_from_mux

type HttpRule

type HttpRule struct {
	// contains filtered or unexported fields
}

func NewHttpRule

func NewHttpRule(_verb string, _scope *regexp.Regexp,
	_param1 interface{}, _param2 interface{}) *HttpRule

type HttpRules

type HttpRules struct {
	// contains filtered or unexported fields
}

struct for rules that are applied to http logging messages

func GetHttpRules

func GetHttpRules() *HttpRules

get package global httpRules containing default rules sets

func (*HttpRules) AllowHttpUrl

func (rules *HttpRules) AllowHttpUrl() bool

func (*HttpRules) CopySessionField

func (rules *HttpRules) CopySessionField() []*HttpRule

func (*HttpRules) DebugRules

func (rules *HttpRules) DebugRules() string

func (*HttpRules) DefaultRules

func (rules *HttpRules) DefaultRules() string

func (*HttpRules) Remove

func (rules *HttpRules) Remove() []*HttpRule

func (*HttpRules) RemoveIf

func (rules *HttpRules) RemoveIf() []*HttpRule

func (*HttpRules) RemoveIfFound

func (rules *HttpRules) RemoveIfFound() []*HttpRule

func (*HttpRules) RemoveUnless

func (rules *HttpRules) RemoveUnless() []*HttpRule

func (*HttpRules) RemoveUnlessFound

func (rules *HttpRules) RemoveUnlessFound() []*HttpRule

func (*HttpRules) Replace

func (rules *HttpRules) Replace() []*HttpRule

func (*HttpRules) Sample

func (rules *HttpRules) Sample() []*HttpRule

func (*HttpRules) SetDefaultRules

func (rules *HttpRules) SetDefaultRules(r string)

*HttpRules.SetDefaultRules(r string) sets the default rules of the logger to rule(s) r

func (*HttpRules) Size

func (rules *HttpRules) Size() int

func (*HttpRules) SkipCompression

func (rules *HttpRules) SkipCompression() bool

func (*HttpRules) SkipSubmissio

func (rules *HttpRules) SkipSubmissio() bool

func (*HttpRules) StandardRules

func (rules *HttpRules) StandardRules() string

func (*HttpRules) Stop

func (rules *HttpRules) Stop() []*HttpRule

func (*HttpRules) StopIf

func (rules *HttpRules) StopIf() []*HttpRule

func (*HttpRules) StopIfFound

func (rules *HttpRules) StopIfFound() []*HttpRule

func (*HttpRules) StopUnless

func (rules *HttpRules) StopUnless() []*HttpRule

func (*HttpRules) StopUnlessFound

func (rules *HttpRules) StopUnlessFound() []*HttpRule

func (*HttpRules) StrictRules

func (rules *HttpRules) StrictRules() string

func (*HttpRules) Text

func (rules *HttpRules) Text() string

type NetHttpClientLogger

type NetHttpClientLogger struct {
	http.Client
	HttpLogger *HttpLogger
}

NetHttpClientLogger defines a struct used to log specifically from the client side of API interactions using the net/http package.

func NewNetHttpClientLogger

func NewNetHttpClientLogger() (*NetHttpClientLogger, error)

NewNetHttpClientLogger() takes no arguments and returns 2 objects; a pointer to an instance of an NetHttpClientLogger struct and an error. The NetHttpClientLogger returned by this function has the default options applied. If there is no error, the error value returned will be nil.

func NewNetHttpClientLoggerOptions

func NewNetHttpClientLoggerOptions(options Options) (*NetHttpClientLogger, error)

NewNetHttpClientLoggerOptions() takes 1 argument of type logger.Options and returns 2 objects; a pointer to an instance of an NetHttpClientLogger struct and an error. The NetHttpClientLogger returned by this function has the given options applied. If there is no error, the error value returned will be nil.

func (*NetHttpClientLogger) CloseIdleConnections

func (clientLogger *NetHttpClientLogger) CloseIdleConnections()

net.http.Client.CloseIdleConnections() wrapper

func (*NetHttpClientLogger) Do

func (clientLogger *NetHttpClientLogger) Do(req *http.Request) (resp *http.Response, err error)

net.http.Client.Do wrapper with logging

func (*NetHttpClientLogger) Get

func (clientLogger *NetHttpClientLogger) Get(url string) (resp *http.Response, err error)

net.http.Client.Get wrapper with logging

func (*NetHttpClientLogger) Head

func (clientLogger *NetHttpClientLogger) Head(url string) (resp *http.Response, err error)

net.http.Client.Head wrapper with logging

func (*NetHttpClientLogger) Logger

func (logger *NetHttpClientLogger) Logger() *HttpLogger

func (*NetHttpClientLogger) Post

func (clientLogger *NetHttpClientLogger) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)

net.http.Client.Post wrapper with logging

func (*NetHttpClientLogger) PostForm

func (clientLogger *NetHttpClientLogger) PostForm(url string, data url.Values) (resp *http.Response, err error)

net.http.Client.PostForm wrapper with logging

type Options

type Options struct {
	//Rules defines the rules that will be applied to the logger.
	Rules string

	//Url defines the Url the logger will send the logs to.
	Url string

	//Enabled defines the state of the logger; enabled or disabled.
	Enabled interface{}

	//Queue is a slice of strings used to store logs; exclusively for testing purposes.
	//Queue must be nil for the logger to properly function.
	Queue []string
}

Options struct is passed to a "NewLogger" function to specifiy the desired configuration of the logger to be created.

type UsageLoggers

type UsageLoggers struct {
	// contains filtered or unexported fields
}

func GetUsageLoggers

func GetUsageLoggers() (*UsageLoggers, error)

func (*UsageLoggers) Disable

func (uLogger *UsageLoggers) Disable()

* * Disable all usage loggers.

func (*UsageLoggers) Enable

func (uLogger *UsageLoggers) Enable()

* * Enable all usage loggers, except those explicitly disabled.

func (*UsageLoggers) IsEnabled

func (uLogger *UsageLoggers) IsEnabled() bool

* * Returns true if usage loggers are generally enabled.

func (*UsageLoggers) UrlByDefault

func (uLogger *UsageLoggers) UrlByDefault() string

* * Returns url to use by default.

Jump to

Keyboard shortcuts

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