loggo

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

README

loggo

loggo is a log level sensitive logging library containg apis to log strings, or apis to log using the fmt.Printf signature.

Usage

Install loggo, then select on of the two following ways to use loggo.

Instance
var logger := loggo.New(loggo.Config{})

logger.Debug()
logger.Error()
logger.Fatal()
logger.Log()
logger.Success()
logger.Trace()
logger.Warn()

Using an instance of loggo is the recommended approach when creating a library to be consumed. This ensures that your users loggo configuration doesn't conflict with your own if they intend to use the singleton.

Singleton
loggo.Configure(loggo.Config{}) // Optional
loggo.Debug()
loggo.Error()
loggo.Fatal()
loggo.Log()
loggo.Success()
loggo.Trace()
loggo.Warn()

loggo also exposes a conveniant singleton instance which can be accessed directly from the root of the package. If loggo.Configure is not called, a default configuration is used. loggo.Configure will only be applied once on the first call, and therefore is also thread safe.

Defaults
// Default config
loggo.Config{
    colours:          <see default colours>,
	logLevel:         Level.Info,
	ignoreEnvVar:     false, // Configure loggo to ignore the environment variable LOG_LEVEL override.
	enableTimestamps: false, // Configure loggo to print timestamps.
	disableMsgPrefix: false, // Configure loggo to omit the log level prefix of messages.
}

// Default colours
loggo.Colouring{
    Debug:   Colours.Magenta,
	Error:   Colours.Red,
	Fatal:   Colours.Red,
	Default: Colours.Blue,
	Success: Colours.Green,
	Trace:   Colours.Grey,
	Warn:    Colours.Yellow,
}
Additional Utils

loggo also contains a few useful utils for formatting, colouring, and creating custom loggers.

loggo.Bold(string)
loggo.Boldf(string, ...any) // Both wrap your input in bold ansi codes, and return the resulting string.

loggo.Colour(colour, string)
loggo.Colourf(colour, string, ...any) // Both wrap your input in colour ansi codes, and return the resulting string.

loggo.CustomColour(r byte, b byte, g byte) // Creates a colour from RGB values 0-255.

loggo.CustomLogger(*loggo, colour, string, LogLevel)
loggo.CustomLoggerf(*loggo, colour, string, LogLevel) // Both create a custom logging function using an instance of loggo, colour, name, and a level to log at. The first creates a standard string logger, the second creates a logger using the fmt.Printf signature.

loggo.Format(loggo.Formatting{}, string)
loggo.Formatf(loggo.Formatting{}, string, ...any) // Both wrap your input in multiple formatting options including colour, bold, and underline.

loggo.Underline(string)
loggo.Underlinef(string, ...any) // Both wrap your input in underline ansi codes, and return the resulting string.
Constants
loggo.Colours() // Returns a struct of colour ansi code constants, for use with the utils above.

loggo.Levels() // Returns a struct of log level constants, for use with the utils above.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bold

func Bold(s string) string

Bold wraps a string in bold ansi codes. Returns a string

func Boldf added in v0.0.2

func Boldf(s string, a ...any) string

Boldf wraps a string in bold ansi codes. Uses the same interface as fmt.Printf. Returns a string

func Clear

func Clear()

Clear will clear the terminal and move the cursor to (0,0).

func ClearLine

func ClearLine()

ClearLine will clear the current line and move the cursor to the start of the line.

func Colour added in v0.0.2

func Colour(colour colour, s string) string

Colour wraps a string in the colour provided. Use the provided colour constants in loggo, or generate one using CustomColour. Returns a string

func Colourf added in v0.0.2

func Colourf(colour colour, s string, a ...any) string

Colour wraps a string in the colour provided. Use the provided colour constants in loggo, or generate one using CustomColour. Uses the same interface as fmt.Printf after the initial colour string. Returns a string

func Colours

func Colours() colours

func Configure

func Configure(loggoConfig Config)

(Singleton) It is a thread safe function which allows you to configure Loggo once, and then use the singleton api to reuse that same configuration throughout your code. The configuration passed to the first call of Configure, is stored and will be used in subsequent calls. Any configuration passed to subsequent calls is ignored. If Configure is never called, the singleton api will use the default configuration.

func CustomColour

func CustomColour(red byte, green byte, blue byte) colour

CustomColour turns 3 integers for red, green, and blue into an ansi code. Returns a string which can be used to configure Colours

func Debug

func Debug(s string)

(Singleton)

func Debugf added in v0.0.2

func Debugf(s string, a ...any)

(Singleton) Uses the same interface as fmt.Printf.

func Error

func Error(s string)

(Singleton)

func Errorf added in v0.0.2

func Errorf(s string, a ...any)

(Singleton) Uses the same interface as fmt.Printf.

func Fatal

func Fatal(s string)

(Singleton) Calling Fatal will log the message, if logLevel is not Level.Silent, then will always call os.Exit(1).

func Fatalf added in v0.0.2

func Fatalf(s string, a ...any)

(Singleton) Calling Fatal will log the message, if logLevel is not Level.Silent, then will always call os.Exit(1). Uses the same interface as fmt.Printf.

func Format added in v0.0.2

func Format(f Formatting, s string) string

Format is a helper function which can apply multiple formatting options to a string at once. Using the Formatting struct, you can apply bold, underling and a colour in a any combination. Returns a string.

func Formatf added in v0.0.2

func Formatf(f Formatting, s string, a ...any) string

Formatf is a helper function which can apply multiple formatting options to a string at once. Using the Formatting struct, you can apply bold, underling and a colour in a any combination. Uses the same interface as fmt.Printf after the inital Formatting struct. Returns a string.

func Get

func Get() loggo

(Singleton) Get returns a copy of the singleton instance. Returns an instance of Loggo

func Info

func Info(s string)

(Singleton)

func Infof added in v0.0.2

func Infof(s string, a ...any)

(Singleton) Uses the same interface as fmt.Printf.

func Levels added in v0.0.2

func Levels() levels

func New

func New(config Config) loggo

Create a new instance of loggo.

func Success

func Success(s string)

(Singleton) Logs at the Level.Info log level.

func Successf added in v0.0.2

func Successf(s string, a ...any)

(Singleton) Logs at the Level.Info log level. Uses the same interface as fmt.Printf.

func Timestamps added in v0.0.2

func Timestamps() timestamps

func Trace

func Trace(s string)

(Singleton)

func Tracef added in v0.0.2

func Tracef(s string, a ...any)

(Singleton) Uses the same interface as fmt.Printf.

func Underline

func Underline(s string) string

Underline wraps a string in underline ansi codes. Returns a string

func Underlinef added in v0.0.2

func Underlinef(s string, a ...any) string

Underlinef wraps a string in underline ansi codes. Uses the same interface as fmt.Printf. Returns a string

func Warn added in v0.0.2

func Warn(s string)

(Singleton)

func Warnf added in v0.0.2

func Warnf(s string, a ...any)

(Singleton) Uses the same interface as fmt.Printf.

Types

type Colouring added in v0.0.2

type Colouring struct {
	Debug   colour
	Error   colour
	Fatal   colour
	Info    colour
	Success colour
	Trace   colour
	Warn    colour
}

Configure the colour of each LogLevel.

type Config

type Config struct {
	// Configure the colour of each LogLevel.
	Colours Colouring
	// The default LogLevel for the instance can be overrwridden with environment variables if IgnoreEnvVar is false. Default: Levels().Default
	LogLevel LogLevel
	// Configure whether the instance should ignore the env var LOG_LEVEL. Default: false.
	IgnoreEnvVar bool
	// Enable ISO timestamps on logs. Default: false
	Timestamp timestamp
	// Disable the log level prefix before the built in logging messages. Custom loggers are unaffected. Default: false.
	DisablePrefix bool
	// Disable the indent and word wrapping functionality for faster logging. Default: false.
	DisableWordWrap bool
}

Config is a struct for configuring loggo instances.

type Formatting added in v0.0.2

type Formatting struct {
	Bold      bool
	Colour    colour
	Underline bool
}

type LogLevel

type LogLevel int

func (LogLevel) FromString added in v0.0.2

func (l LogLevel) FromString(s string) LogLevel

func (LogLevel) String

func (l LogLevel) String() string

type Logger added in v0.0.2

type Logger func(s string)

func CustomLogger

func CustomLogger(config TemplateConfig, logLevel LogLevel) Logger

CustomLogger creates a CustomLogger using the singleton loggo instance. Returns a function which logs a string.

type Loggerf added in v0.0.2

type Loggerf func(s string, a ...any)

func CustomLoggerf added in v0.0.2

func CustomLoggerf(config TemplateConfig, logLevel LogLevel) Loggerf

CustomLoggerf creates a CustomLogger using the singleton loggo instance. Returns a function which uses the same interface as fmt.Printf.

type Loggerm added in v0.0.2

type Loggerm func(s string, a any)

type Loggo

type Loggo interface {
	Debug(string)
	Debugf(string, ...any)
	Error(string)
	Errorf(string, ...any)
	Fatal(string)
	Fatalf(string, ...any)
	Info(string)
	Infof(string, ...any)
	Success(string)
	Successf(string, ...any)
	Trace(string)
	Tracef(string, ...any)
	Warn(string)
	Warnf(string, ...any)
	CustomLogger(TemplateConfig, LogLevel) Logger
	CustomLoggerf(TemplateConfig, LogLevel) Loggerf
}

type TemplateConfig added in v0.0.2

type TemplateConfig struct {
	// Enable printing the timestamp in the formats 15:04:05.999, or 2006-01-02 15:04:05.999, before the message. Default: None.
	Timestamp timestamp
	// Disable word wrap, and indentation, for a faster template. Default: false.
	DisableWordWrap bool
	// Indent requires word wrap to be enabled. Default: 0,
	Indent int
	// Colour Name of your logger. Use a builtin loggo colour, loggo.Colours().<name>, or make a custom colour using loggo.CustomColour(...)
	Colour colour
	// The name your logger will be printed before the message as part of the prefix. Ignored if left blank.
	Name string
}

Use TemplateConfig to configure CustomLoggers

Directories

Path Synopsis
benchmarks
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳