logstore

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: GPL-3.0 Imports: 19 Imported by: 1

README

Log Store Open in Gitpod

Tests Status Go Report Card PkgGoDev

Logs messages to a database table.

License

This project is licensed under the GNU General Public License version 3 (GPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/gpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Installation

go get -u github.com/dracory/logstore

Setup

logStore, err = logstore.NewStore(logstore.NewStoreOptions{
    DB: databaseInstance,
    LogTableName: "log",
    AutomigrateEnabled: true,
})

if err != nil {
    panic(error.Error())
}

Usage

logStore.Info("Hello")

// with additional context
logStore.InfoWithContext("Hello", map[string]string{
    "name": "John Doe"
})

Slog

As slog is the now official logger in golang, LogStore provides a SlogHandler.

Logger = *slog.New(logstore.NewSlogHandler(&LogStore))

logger.Info("Hello", "name", "John Doe")

Log Levels

  1. LevelTrace - Something very low level
  2. LevelDebug - Useful debugging information
  3. LevelInfo - Something noteworthy happened!
  4. LevelWarn - You should probably take a look at this
  5. LevelError - Something failed but I'm not quitting
  6. LevelFatal - Bye. Calls os.Exit(1) after logging
  7. LevelPanic - I'm bailing. Calls panic() after logging

Change Log

2024.09.23 - Added a SlogHandler

2023.07.19 - Updated instance creation to use options struct

2022.06.26 - Updated dependencies

2021.12.21 - Added LICENSE

2021.12.21 - Added test badge

2021.12.21 - Added support for DB dialects

2021.12.21 - Removed GORM dependency and moved to the standard library

Documentation

Index

Constants

View Source
const (
	// LevelTrace trace level
	LevelTrace = "trace"
	// LevelDebug debug level
	LevelDebug = "debug"
	// LevelError error level
	LevelError = "error"
	// LevelFatal fatal level
	LevelFatal = "fatal"
	// LevelInfo info level
	LevelInfo = "info"
	// LevelPanic panic level
	LevelPanic = "panic"
	// LevelWarning warning level
	LevelWarning = "warning"
)
View Source
const COLUMN_CONTEXT = "context"
View Source
const COLUMN_ID = "id"
View Source
const COLUMN_LEVEL = "level"
View Source
const COLUMN_MESSAGE = "message"
View Source
const COLUMN_TIME = "time"

Variables

View Source
var (
	ErrLogTableNameRequired = errors.New("log store: logTableName is required")
	ErrDBRequired           = errors.New("log store: DB is required")
)

Errors

Functions

func NewStore

func NewStore(opts NewStoreOptions) (*storeImplementation, error)

NewStore creates a new session store

Types

type Log

type Log struct {
	ID      string
	Level   string
	Message string
	Context string
	Time    *time.Time
}

Log type

type NewStoreOptions

type NewStoreOptions struct {
	LogTableName       string
	DB                 *sql.DB
	DbDriverName       string
	AutomigrateEnabled bool
	DebugEnabled       bool
}

NewStoreOptions define the options for creating a new session store

type SlogHandler

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

func NewSlogHandler

func NewSlogHandler(logStore StoreInterface) *SlogHandler

func (*SlogHandler) Enabled

func (handler *SlogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SlogHandler) Handle

func (handler *SlogHandler) Handle(ctx context.Context, record slog.Record) error

func (*SlogHandler) WithAttrs

func (handler *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SlogHandler) WithGroup

func (handler *SlogHandler) WithGroup(name string) slog.Handler

type StoreInterface

type StoreInterface interface {
	// AutoMigrate creates the necessary database tables
	AutoMigrate() error

	// EnableDebug enables or disables debug mode
	EnableDebug(debug bool)

	// Log adds a log entry
	Log(logEntry *Log) error

	// Debug adds a debug log
	Debug(message string) error

	// DebugWithContext adds a debug log with context data
	DebugWithContext(message string, context interface{}) error

	// Error adds an error log
	Error(message string) error

	// ErrorWithContext adds an error log with context data
	ErrorWithContext(message string, context interface{}) error

	// Fatal adds a fatal log
	Fatal(message string) error

	// FatalWithContext adds a fatal log with context data
	FatalWithContext(message string, context interface{}) error

	// Info adds an info log
	Info(message string) error

	// InfoWithContext adds an info log with context data
	InfoWithContext(message string, context interface{}) error

	// Panic adds a panic log and calls panic(message) after logging
	Panic(message string)

	// PanicWithContext adds a panic log with context data and calls panic(message) after logging
	PanicWithContext(message string, context interface{})

	// Trace adds a trace log
	Trace(message string) error

	// TraceWithContext adds a trace log with context data
	TraceWithContext(message string, context interface{}) error

	// Warn adds a warn log
	Warn(message string) error

	// WarnWithContext adds a warn log with context data
	WarnWithContext(message string, context interface{}) error
}

StoreInterface defines the interface for a log store

Jump to

Keyboard shortcuts

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