logart

package module
v0.0.0-...-da968a9 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: GPL-3.0 Imports: 6 Imported by: 0

README

Logart

Logart is a structured logging tool that aims to simplify logging to a database

It is not yet in stable state, but is used in production and actively developped.

All contributions are welcome, please feel free to raise an issue or make a PR.

Example

API is a struct containing a database connection that has a method for logging all the fields you need.

Code
// CreateItem is a request for creating an item
func (api *API) CreateItem(w http.ResponseWriter, r *http.Request) {
	// Get information we need from the context
	s := session.FromContext(r)

	// unmarshal the payload
	i := &db.Item{}
	json.NewDecoder(r.Body).Decode(i)
	io.Copy(io.Discard, r.Body)

	// create an audit of the access
	logart.Read().
		With("user", s.UserID).
		With("reference", i.Reference).
		Commit(api.LogRequest(r.Context()))
}

// LogRequest logs the request
func (api *API) LogRequest(ctx context.Context) func(l *logart.Log) error {
	return func(l *logart.Log) error {
		return api.DB.Log(ctx, db.LogParams{
			Timestamp: l.Timestamp,
			Event:     l.Event,
			Content:   l.FieldString("reference"),
			User:      l.FieldInt64("user"),
		})
	}
}
SQL
-- query
-- name: Log :exec
INSERT INTO "transactions" ("timestamp", "reference", "user", "event")
VALUES ($1, $2, $3, $4);

-- schema
CREATE TABLE "public"."transactions" (
    "id" serial PRIMARY KEY,
    "timestamp" timestamp NOT NULL,
    "reference" varchar,
    "event" varchar NOT NULL,
    "user" integer NOT NULL
);

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(ev Eventer)

Set the global logger

Types

type Eventer

type Eventer interface {
	Event(event.Event) *Log
	ErrorHandler(error)
}

Eventer implements the CRUD logging events

type Log

type Log struct {
	Logger    Eventer // `struct{}` by default
	Event     event.Event
	Timestamp time.Time
	// contains filtered or unexported fields
}

Log is a single log message

func Create

func Create() *Log

Create wraps the global logger

func Delete

func Delete() *Log

Delete wraps the global logger

func Read

func Read() *Log

Read wraps the global logger

func Update

func Update() *Log

Update wraps the global logger

func (*Log) Commit

func (l *Log) Commit(commFunc func(*Log) error)

Commit sends the message to the database. Pass in your custom function according to your database configuration

func (*Log) Field

func (l *Log) Field(key string) driver.Value

Field return a field specified by a key

func (*Log) FieldBool

func (l *Log) FieldBool(key string) bool

FieldBool return the field as a bool

func (*Log) FieldBytes

func (l *Log) FieldBytes(key string) []byte

FieldBytes return the field as a byte slice

func (*Log) FieldFloat64

func (l *Log) FieldFloat64(key string) float64

FieldFloat64 return the field as a float64 panics if the field is not a float

func (*Log) FieldInt64

func (l *Log) FieldInt64(key string) int64

FieldInt64 return the field as an int64 panics if the field is not an int

func (*Log) FieldString

func (l *Log) FieldString(key string) string

FieldString return the field as a string

func (*Log) FieldTime

func (l *Log) FieldTime(key string) time.Time

FieldTime return the field as a time.Time

func (*Log) With

func (l *Log) With(key string, value driver.Value) *Log

With adds any field the user want.

type Logger

type Logger struct{}

Logger is a default empty logger

func (Logger) ErrorHandler

func (l Logger) ErrorHandler(err error)

ErrorHandler handles the errors the logger can encounter

func (Logger) Event

func (l Logger) Event(e event.Event) *Log

Event creates a new event log

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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