kataposis

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

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

Go to latest
Published: Nov 30, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

A Go logging library with database integration

Kataposis

Workflow Go Report Last Commit Contributors


Table of Contents

Introduction

Kataposis (or ktpss) is a logging library for Go that provides a simple but effective logging mechanism, seemlessly saving entries to a database. It provides a fluent API for constructing and storing log entries in the database, and it also supports fetching log entries based on various criteria.

At the moment, supported databases include Postgres. Kataposis currently supports the basic log parameters such as message, log level, timestamp, and resource ID; with a design that allows for easy support for more parameters as needed in the future.

Installation

First step towards using Kataposis in your Go application is to add the module to your Go program:

go get -u cheezaram.tech/kataposis

Usage

Initialization

To initialize Kataposis and set up the database connection, use the Init function along with optional configuration options:

package main

import (
	"time"

	"cheezaram.tech/kataposis"
)

func main() {
	var err error
	// Initialize the logger.
	log, err := kataposis.Init(
		kataposis.WithPGAddr("localhost:5432"),
		kataposis.WithPGDB("kataposis"),
		kataposis.WithPGUser("kataposis"),
		kataposis.WithPGPassword("kataposis"),
	)

	if err != nil {
		// Handle error
	}
}
Logging Messages

Use the fluent API provided by Kataposis to create log entries and save them to the database:

func main() {
	// Previous initialization code.

	// Log a message.
	err = log.Msg("Log message").RID("1234").Level("info").Timestamp(time.Now())
	if err != nil {
		// Handle error
	}
}
Querying Log Entries

You can fetch log entries from the database based on specific criteria using the Fetch method:

func main() {
	// Previous initialization code.

	afterTime := time.Now()
	entries, err := log.Fetch(
		"message",  // Message
		"1234",     // Resource ID
		"",         // Log level. Will not be included in the query condition.
		nil,        // Before timestamp. Will not be included in the query condition.
		&afterTime, // After timestamp
	)

	if err != nil {
		// Handle error
	}

	// Process fetched log entries
	for _, entry := range entries {
		// Do something with each log entry
		fmt.Printf("Message: %s, Timestamp: %s\n", entry.GetMsg(), entry.GetTimestamp())
	}
}

You can retrieve the value of each field using the methods of the kataposis.LogEntry struct, usually prefixed with Get.

NB: If any argument is set to its default value during fetching, the argument will not be included in the query condition.

Database Schema

Kataposis creates a table named logs in the specified database with the following schema:

CREATE TABLE IF NOT EXISTS logs (
    id SERIAL PRIMARY KEY,
    rid TEXT,
    message TEXT,
    level TEXT,
    timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

Author

This library was written by Chee-zaram Okeke with love. Feel free to connect.

Contributing

If you find any bugs, feel free to open an issue. Pull request for new features or bug fixes are welcome.

After commiting your changes, run the utils/contributors.sh script to add yourself to the contributors list.

See CONTRIBUTORS for details of all contributors.

Licensing

See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithPGAddr

func WithPGAddr(addr string) configValues

WithPGAddr sets the PostgreSQL connection address. Address should be in the format `host:port`. E.g. `localhost:5432`.

func WithPGDB

func WithPGDB(d string) configValues

func WithPGPassword

func WithPGPassword(p string) configValues

func WithPGUser

func WithPGUser(u string) configValues

Types

type LogEntry

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

LogEntry provides a fluent API for logging. It logs the `message` with the `resourceID`, `level` and `timestamp`.

Only when the `Timestamp` method is called will the log will be entered into the database.

func Init

func Init(opts ...configValues) (*LogEntry, error)

Init uses functional options pattern to initialize the database connection and create the logs table if it does not already exist. It returns a `LogEntry` object that can be used to log messages which are then saved to the database.

func (*LogEntry) Fetch

func (l *LogEntry) Fetch(
	msg logMessage, rid logResourceID, level logLevel, beforeTime *time.Time,
	afterTime *time.Time,
) ([]LogEntry, error)

Fetch is used to fetch log entries from the database. It performs the query to the database based on the arguments provided. If an argument is the default value for the type, that argument is not included in the query string.

`msg` is used to perform a LIKE query on the `message` column.

Fetch returns a slice of LogEntry objects and an error if the query fails.

func (LogEntry) GetLevel

func (l LogEntry) GetLevel() logLevel

GetLevel returns the level of the log entry.

`NB`: It should only be called on an entry returned by the `Fetch` method. Calling `GetLevel` on any other object will result in an undefined behavior.

func (LogEntry) GetMsg

func (l LogEntry) GetMsg() logMessage

GetMsg returns the message of the log entry.

`NB`: It should only be called on an entry returned by the `Fetch` method. Calling `GetMsg` on any other object will result in an undefined behavior.

func (LogEntry) GetRID

func (l LogEntry) GetRID() logResourceID

GetRID returns the resource ID of the log entry.

`NB`: It should only be called on an entry returned by the `Fetch` method. Calling `GetRID` on any other object will result in an undefined behavior.

func (LogEntry) GetTimestamp

func (l LogEntry) GetTimestamp() time.Time

GetTimestamp returns the time stamp of the log entry.

`NB`: It should only be called on an entry returned by the `Fetch` method. Calling `GetTimestamp` on any other object will result in an undefined behavior.

func (*LogEntry) Level

func (l *LogEntry) Level(level logLevel) *LogEntry

Level is used to set the log level of the entry. It takes in a logLevel and returns a LogEntry object.

The log entry is never saved to the database until the `Timestamp` method is called.

func (*LogEntry) Msg

func (l *LogEntry) Msg(msg logMessage) *LogEntry

Msg is used to set the message of the log entry. It takes in a `logMessage` and returns a `LogEntry` object.

The log entry is never saved to the database until the `Timestamp` method is called.

func (*LogEntry) RID

func (l *LogEntry) RID(rid logResourceID) *LogEntry

RID sets the `resourceID` for the log entry. It takes in a `logResourceID` and returns a LogEntry object.

The log entry is never saved to the database until the `Timestamp` method is called.

func (*LogEntry) Timestamp

func (l *LogEntry) Timestamp(ts time.Time) error

Timestamp is used to set the timestamp of the entry. It takes in a logTimestamp and saves the entry to the database specified in the configuration.

It returns an error if the database connection cannot be established.

Directories

Path Synopsis
internal
db

Jump to

Keyboard shortcuts

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