logs

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: BSD-2-Clause Imports: 9 Imported by: 0

README

Golang logging library

Features:

  • Create logs (with timestamp, level, message, location in source code and optional data)
  • Serialize structured logs (as JSON, single-line text)
  • Write logs to io.Writer easily (including to multiple writers, ex: terminal + file)
  • Use common log levels (info, warning, error, etc.)

Todo:

  • Add unit tests
  • Add example to readme

Documentation

Overview

Package logs provides utilities for handling application logs.

Index

Constants

View Source
const (
	DataKeyTimestamp   = dataKeyPrefix + "created_at"
	DataKeyLevel       = dataKeyPrefix + "level"
	DataKeySrcFunction = dataKeyPrefix + "src_function"
	DataKeySrcFileLine = dataKeyPrefix + "src_file_line"
	DataKeyFSys        = dataKeyPrefix + "fsys"
)

Variables

This section is empty.

Functions

func AsJSON added in v0.0.1

func AsJSON(l *Log) []byte

Returns a single-line-JSON representation of a log. This function will panic if the JSON marshalling of the log returns an error.

func AsPlainText added in v0.0.1

func AsPlainText(l *Log) []byte

Returns a single-line textual representation of a log.

func AsPrettyJSON added in v0.0.1

func AsPrettyJSON(l *Log) []byte

Returns the JSON representation of a log with line breaks and indentations. This function will panic if the JSON marshalling of the log returns an error.

Types

type DefaultLogger added in v0.0.1

type DefaultLogger struct {
	Writers     []io.Writer // For ex: stdout and/or file
	Serializer  Serializer  // For ex: As JSON
	BaseOptions []LogOption // For ex: creation timestamp, source code location
	LogPrefix   string      // For ex: "HTTP" or "Server Name"
	LogSuffix   string      // For ex: ",\n" to seperate JSON logs by commas and line breaks
}

func (*DefaultLogger) LoggerFunc added in v0.0.1

func (dl *DefaultLogger) LoggerFunc() (LoggerFunc, error)

type Log

type Log struct {
	Message string         `json:"message"`
	Data    map[string]any `json:"data,omitempty"`
}

Log holds logging data, it has a timestamp, a level of severity and a message. It can also include additional data fields.

func NewLog added in v0.0.1

func NewLog(msg string, opts ...LogOption) *Log

Creates a new log with the timestamp set to the current time.

type LogLevel added in v0.0.1

type LogLevel int

LogLevel represents the severity of a log. Severity of logs could range anywhere between simple debug info to critical errors.

const (
	LevelUnknown LogLevel = iota // Only for temporary use, like context.TODO()
	LevelDebug                   // Debug (usually not meant to be kept in production)
	LevelInfo                    // Informative data
	LevelWarn                    // Warnings
	LevelError                   // Internal errors
	LevelPanic                   // Panics / fatal errors
)

Represents the level of severity of a log.

func (LogLevel) String added in v0.0.1

func (lvl LogLevel) String() string

String returns the textual representation of a level.

type LogOption

type LogOption func(*Log)

LogOption modifies a log. LogOptions are typically used to add more data to a log.

func WithData

func WithData(key string, value any) LogOption

WithData adds more data to a log. The value should serializable in order to be writable to the logger output.

func WithFSys added in v0.0.1

func WithFSys(fsys fs.FS) LogOption

WithFS adds info about a file system (name and size of files) to the log.

func WithLevel added in v0.0.1

func WithLevel(lvl string) LogOption

WithLevel adds a severity level to the log.

func WithSrc added in v0.0.1

func WithSrc() LogOption

WithSrc stores the location where the log was created in the source code.

func WithTimestamp added in v0.0.1

func WithTimestamp() LogOption

WithTimestamp adds a creation datetime to the log.

type LoggerFunc added in v0.0.1

type LoggerFunc func(*Log) error

type Serializer added in v0.0.1

type Serializer func(*Log) []byte

Serializer can convert a Log to bytes so that it can be written.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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