glog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 4 Imported by: 1

README

Utility library for logs (Go)

This a very simple utility library to create configurable loggers that can be nested.

Documentation

Installation

To install the library in your project, run:

go get github.com/AgustinSRG/glog

Usage

In the main function, create a root logger with the CreateRootLogger(config) function, passing the configuration as argument, which allows to enable or disable each specific level of log: ERROR, WARNING, INFO, DEBUG, TRACE.

You can then call the CreateChildLogger(prefix) on the root logger to create logger with the same configuration, but adding a prefix. For example, logs for the example function may have the [ExampleFunction] prefix to better locate the logs.

Once you have the logger, you may call its methods to add logs.

Example:

package main

import (
    // Import the module
    "github.com/AgustinSRG/glog"
)

func main() {
    // Create a root logger
	logger := glog.CreateRootLogger(glog.CreateLoggerConfigurationFromLevel(glog.INFO), glog.StandardLogFunction)

	// Log messages
	logger.Info("Example log message")

	// You can also log with format
	logger.Infof("Example log message: %v, %v, %v", 4, "example string", true)

	// If you log a level that is not enabled, no logs will be made
	logger.Debug("Example debug message")

	// You can also check the config to prevent running the function if disabled
	if logger.Config.DebugEnabled {
		logger.Debug("Example debug message")
	}

	// You can create child loggers, to include prefixes
	childLogger := logger.CreateChildLogger("[PREFIX] ")

	// This will log: [PREFIX] Example log message
	childLogger.Info("Example log message")
}

Build the library

To install dependencies, run:

go get .

To build the code, run:

go build .

Run linter

To run the code linter, run:

golangci-lint run

Run tests

In order to run the tests for this library, run:

go test -v

Documentation

Index

Constants

View Source
const DEBUG int = 4

Debug level

View Source
const ERROR int = 1

Error level

View Source
const INFO int = 3

Info level

View Source
const SILENT int = 0

Silent level (no logs)

View Source
const TRACE int = 5

Trace level

View Source
const WARNING int = 2

Warning level

Variables

This section is empty.

Functions

func LevelFromString

func LevelFromString(str string) (int, error)

Parses level from string (case insensitive)

Parameters: - str: The string to parse

Returns the parsed level, or an error if the value has an invalid format

func LevelToString

func LevelToString(level int) string

Turns level from int to string

Parameters: - level: The level

Returns the level as string

func StandardLogFunction

func StandardLogFunction(level int, message string)

Standard log function to log messages using the "log" package

Parameters: - level: The log level - message: The log message

Types

type Logger

type Logger struct {
	// Logger configuration
	Config LoggerConfiguration

	// Prefix for logs
	Prefix string

	// Log function
	LogFunc func(level int, message string)
}

Logger

func CreateRootLogger

func CreateRootLogger(config LoggerConfiguration, logFunc func(level int, message string)) *Logger

Creates a root logger

Parameters: - config: The logger configuration - logFunc: The function to log the messages. By default, use glog.StandardLogFunction

Returns a Logger instance

func (*Logger) CreateChildLogger

func (logger *Logger) CreateChildLogger(prefix string) *Logger

Creates child logger, adding a prefix to the logs of the parent logger

Parameters: - prefix: The prefix for the logs

Returns a Logger instance

func (*Logger) Debug

func (logger *Logger) Debug(message string)

Logs an DEBUG message

Parameters: - message: The message

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, a ...any)

Logs an DEBUG message (with format)

Parameters: - format: The message format - a: The parameters to be used in the format

Example:

logger.Debugf("The value of the intermediate value is: %v", value)

func (*Logger) Error

func (logger *Logger) Error(message string)

Logs an ERROR message

Parameters: - message: The message

func (*Logger) Errorf

func (logger *Logger) Errorf(format string, a ...any)

Logs an ERROR message (with format)

Parameters: - format: The message format - a: The parameters to be used in the format

Example:

logger.Errorf("An error happened in the request to %v | %v", url, error.Error())

func (*Logger) Info

func (logger *Logger) Info(message string)

Logs an INFO message

Parameters: - message: The message

func (*Logger) Infof

func (logger *Logger) Infof(format string, a ...any)

Logs an INFO message (with format)

Parameters: - format: The message format - a: The parameters to be used in the format

Example:

logger.Infof("Received request from: %v", ip)

func (*Logger) Log

func (logger *Logger) Log(level int, message string)

Logs a message

Parameters: - level: Log level - message: The message

func (*Logger) Trace

func (logger *Logger) Trace(message string)

Logs an TRACE message

Parameters: - message: The message

func (*Logger) Tracef

func (logger *Logger) Tracef(format string, a ...any)

Logs an TRACE message (with format)

Parameters: - format: The message format - a: The parameters to be used in the format

Example:

logger.Tracef("DB Query: %v", query)

func (*Logger) Warning

func (logger *Logger) Warning(message string)

Logs an WARNING message

Parameters: - message: The message

func (*Logger) Warningf

func (logger *Logger) Warningf(format string, a ...any)

Logs an WARNING message (with format)

Parameters: - format: The message format - a: The parameters to be used in the format

Example:

logger.Warningf("Unknown value: %v", value)

type LoggerConfiguration

type LoggerConfiguration struct {
	// True to enable ERROR messages
	ErrorEnabled bool

	// True to enable WARNING messages
	WarningEnabled bool

	// True to enable INFO messages
	InfoEnabled bool

	// True to enable DEBUG messages
	DebugEnabled bool

	// True to enable TRACE messages
	TraceEnabled bool
}

Logger configuration

func CreateLoggerConfigurationFromLevel

func CreateLoggerConfigurationFromLevel(level int) LoggerConfiguration

Creates a configuration from a level

Parameters: - level: The lowest level to log

Returns a configuration with all the levels lower then the specified disabled

Jump to

Keyboard shortcuts

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