elog

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

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

Go to latest
Published: Jun 19, 2022 License: MIT Imports: 2 Imported by: 0

README

Event Logger

This package provides a simple interface for event logging to generic backends as well as a solid set of backend implementations.

Available Storage Backends
  • csv: logging events in CSV format, easy for development and log collection
  • bq: logging to Google BigQuery, great for production
  • gsheets: logging to a Google Sheet, great for low-volume production
Backend Decorators
  • async: a decorator to make logging asynchronous
  • row: a decorator to make logging to row based backends easier

Installation

go install github.com/trichner/elog

Usage

package main

import (
	"github.com/trichner/elog"
	"github.com/trichner/elog/csv"
	"os"
	"time"
)

func main() {

	// create a new logger with the CSV backend
	logger, err := csv.NewCsvEventLogger(os.Stdout)
	if err != nil {
		panic(err)
	}

	// create a new event
	event := elog.NewEventBuilder().
		WithKind("user_login").
		WithTimeStamp(time.Now()).
		WithPayload(map[string]string{
			"user":   "Alice",
			"remote": "42.0.4.23",
		}).
		Build()

	// ... and log it!
	logger.Log(event)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	ID        string
	Kind      string
	SessionID string
	Timestamp time.Time
	Payload   any
}

type EventBuilder

type EventBuilder interface {
	WithID(id string) EventBuilder
	WithKind(kind string) EventBuilder
	WithSessionID(session string) EventBuilder
	WithTimeStamp(ts time.Time) EventBuilder
	WithPayload(payload any) EventBuilder

	Build() *Event
}

EventBuilder helps to assemble events. The builder will create a universally unique ID (UUID) for the event ID if none is provided. Furthermore, the Timestamp will be set to 'now' in case it was not provided.

func NewEventBuilder

func NewEventBuilder() EventBuilder

type EventLogger

type EventLogger interface {
	Log(e *Event) error
}
Example
package main

import (
	"github.com/trichner/elog"
	"github.com/trichner/elog/csv"
	"os"
	"time"
)

func main() {

	// create a new logger with the CSV backend
	logger, err := csv.NewCsvEventLogger(os.Stdout)
	if err != nil {
		panic(err)
	}

	event := elog.NewEventBuilder().
		WithKind("user_login").
		WithTimeStamp(time.Now()).
		WithPayload(map[string]string{
			"user":   "Alice",
			"remote": "42.0.4.23",
		}).
		Build()

	logger.Log(event)
}
Output:

func NewNoOpEventLogger

func NewNoOpEventLogger() EventLogger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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