quicklog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 4 Imported by: 6

README

Quicklog

Opinionated logger that abstracts both terminal and JSON logging. Rendering method is automatically chosen based on the environment.

go get -u github.com/a-novel-kit/quicklog

GitHub Actions Workflow Status codecov

GitHub repo file or directory count GitHub code size in bytes

Coverage graph

Documentation

Index

Constants

View Source
const TermWidthEnv = "TERM_WIDTH"

TermWidthEnv is the name of the environment variable that can be used to override the TermWidth value.

Variables

View Source
var TermWidth = lo.CoalesceOrEmpty(getTermWidth(), 80)

TermWidth is the default rendering width used for terminal output.

It may be overridden by setting the TermWidthEnv environment variable.

Functions

func RenderWithChildJSON

func RenderWithChildJSON(parent map[string]interface{}, child Message) map[string]interface{}

RenderWithChildJSON automatically renders a parent with its child in JSON format.

func RenderWithChildTerminal

func RenderWithChildTerminal(parent string, child Message) string

RenderWithChildTerminal automatically renders a parent with its child in terminal format.

Types

type AnimatedMessage

type AnimatedMessage interface {
	// RunTerminal starts rendering the message in dynamic mode. The returned channel is subscribed by the logger to
	// render the message in real-time.
	//
	// The CI flag is passed for environments where real-time outputs might result in loads of repetitive logs
	// (building docker image for example). In this case, only relevant updates should be sent to the chan.
	RunTerminal(ci bool) <-chan string

	// RunJSON is similar to RunTerminal but tailored for JSON output. The CI flag is assumed to always be true.
	RunJSON() <-chan map[string]interface{}

	// Close terminates the logger, and releases all its resources.
	Close()
}

AnimatedMessage allow a Message to produce an output that is dynamically updated.

type Level

type Level string

Level specify the importance of the log message. Some implementations may use different channels depending on the log level.

const (
	// LevelInfo is the lowest log level. It is used for general information messages.
	LevelInfo Level = "INFO"
	// LevelWarning is used for messages that are not errors but may require attention.
	LevelWarning Level = "WARNING"
	// LevelError is used for messages that indicate an error occurred.
	LevelError Level = "ERROR"
	// LevelFatal is used for messages that indicate a fatal error occurred. A logger implementation should
	// automatically exit the program, or trigger a crash, after logging a message with this level.
	LevelFatal Level = "FATAL"
)

type Logger

type Logger interface {
	// Log a message with the specified level.
	Log(level Level, message Message)

	// LogAnimated logs a message that can be updated in real-time.
	//
	// Running an animated log prevents new messages from being printed until the animated log is closed.
	// Attempting to log while an animated message is running will cause a panic.
	LogAnimated(message AnimatedMessage) (cleaner func())
}

type Message

type Message interface {
	// RenderTerminal renders a message in a format that is suitable for terminal output.
	RenderTerminal() string
	// RenderJSON renders a message in a format that is suitable for JSON output.
	RenderJSON() map[string]interface{}
}

Message is a generic representation of a data that supports rendering under different formats.

Directories

Path Synopsis
cmd
demo command

Jump to

Keyboard shortcuts

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