logger

package module
v1.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: GPL-3.0 Imports: 4 Imported by: 11

README

Logger

Logger is a package designed for logging in modular programs specifically, It contains many cool features like colorised logging, different logging levels, etc.

You can use this package to implement logging in your Go program, for any futher help you can check out the documentations.

Go Reference GPLv3 license

Note: This library is in the alpha stage yet and doesn't cover every aspect of logging.

Installation

You can download the library with the help of standard go get command.

go get github.com/anonyindian/logger

Usage

package main
import (
	logger "github.com/anonyindian/logger"
    "os"
)
func main() {
    log := logger.New(os.Stderr, nil)
    log.Println("Hello World")
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

GPLv3
Licensed Under GNU General Public License v3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Print

func Print(v ...any)

Print function is used to print the provided arguments with default settings.

Use New() function to create your own logger.

func Printf

func Printf(format string, a ...any)

Printf function is used to format the provided string with the arguments and print the result with default settings.

Use New() function to create your own logger.

func Println

func Println(v ...any)

Println function is used to print the provided arguments in a newline with default settings.

Use New() function to create your own logger.

func Printlnf

func Printlnf(format string, a ...any)

Printlnf function is used to format the provided string with the arguments and print the result in a newline with default settings.

Use New() function to create your own logger.

Types

type Level

type Level string

Level type is to define the logging levels, which would be used for saturating logs.

Note: In case you create new levels, they must be added to the LevelToNum and LevelToColor maps.

var (
	// LevelDebug is the DEBUG level which should be used for debugging.
	LevelDebug Level = "DEBUG"
	// LevelMain is the MAIN level which should be used for logging main arguments.
	LevelMain Level = "MAIN"
	// LevelInfo is the INFO level which should be used for logging all basic arguments.
	LevelInfo Level = "INFO"
	// LevelError is the ERROR level which should be used for logging all errors.
	LevelError Level = "ERROR"
	// LevelCritical is the CRITICAL level which should be used for logging all critical things.
	LevelCritical Level = "CRITICAL"

	// LevelToNum map converts the provided level to its integral value.
	//
	// One should add custom created levels to this map for proper functioning.
	LevelToNum = map[Level]int{
		LevelDebug:    -1,
		LevelInfo:     0,
		LevelMain:     1,
		LevelError:    2,
		LevelCritical: 3,
	}

	// LevelToColor map converts the provided level to its defined color.
	//
	// One should add custom created levels to this map for proper functioning.
	LevelToColor = map[Level]color{
		LevelDebug:    white,
		LevelInfo:     blue,
		LevelMain:     green,
		LevelError:    purple,
		LevelCritical: red,
	}
)

type Logger

type Logger struct {
	Name    string
	Level   Level
	NoColor bool
	// contains filtered or unexported fields
}

Logger is the main struct of the library which implements methods for logging.

Name (type string): Name of the new module you want to inherit into. Level (type Level): Current running level of the logger. NoColor (type bool): Enable it if you want to disable colors.

func New

func New(out *os.File, opts *LoggerOpts) *Logger

New function creates a new logger.

func (*Logger) ChangeLevel

func (l *Logger) ChangeLevel(level Level) *Logger

ChangeLevel method should be used to change the level of current logger.

func (*Logger) ChangeMinimumLevel

func (l *Logger) ChangeMinimumLevel(minimumLevel Level) *Logger

ChangeMinimumLevel method should be used to change the minimum printable level of current logger.

func (*Logger) Create

func (l *Logger) Create(name string) *Logger

Create method is used when you want to initalise logger for a new module, parent logger will be inherited with a few mutations.

func (*Logger) Print

func (l *Logger) Print(v ...any)

Print method is used to print the provided arguments with custom settings.

func (*Logger) Printf

func (l *Logger) Printf(format string, a ...any)

Printf method is used to format the provided string with the arguments and print the result with custom settings.

func (*Logger) Println

func (l *Logger) Println(v ...any)

Println method is used to print the provided arguments in a newline with custom settings.

func (*Logger) Printlnf

func (l *Logger) Printlnf(format string, a ...any)

Printlnf method is used to format the provided string with the arguments and print the result in a newline with custom settings.

type LoggerOpts

type LoggerOpts struct {
	ProjectName  string
	NoColor      bool
	MinimumLevel Level
}

LoggerOpts contain all the optional fields of New Logger.

Jump to

Keyboard shortcuts

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