Logo

0x5a.live

for different kinds of informations and explorations.

GitHub - no-src/log: A simple logging framework out of the box

A simple logging framework out of the box. Contribute to no-src/log development by creating an account on GitHub.

Visit SiteGitHub - no-src/log: A simple logging framework out of the box

GitHub - no-src/log: A simple logging framework out of the box

A simple logging framework out of the box. Contribute to no-src/log development by creating an account on GitHub.

Powered by 0x5a.live ๐Ÿ’—

log

Build License Go Reference Go Report Card codecov Release Mentioned in Awesome Go

Installation

go get -u github.com/no-src/log

Quick Start

Current support following loggers

For example, init a console logger, to write logs.

package main

import (
	"errors"

	"github.com/no-src/log"
	"github.com/no-src/log/level"
)

func main() {
	// init console logger as default logger
	// replace the line of code with any logger you need
	log.InitDefaultLogger(log.NewConsoleLogger(level.DebugLevel))

	defer log.Close()

	text := "hello world"
	// use default logger to write logs
	log.Debug("%s, test debug log", text)
	log.Info("%s, test info log", text)
	log.Warn("%s, test warn log", text)
	log.Error(errors.New("log err"), "%s, test error log", text)
	log.ErrorIf(errors.New("log err"), "%s, test error log", text)
	log.Log("%s, test log log", text)
}

Logger

Empty Logger

Init empty logger as default logger.

log.InitDefaultLogger(log.NewEmptyLogger())

Console Logger

Init console logger as default logger.

log.InitDefaultLogger(log.NewConsoleLogger(level.DebugLevel))

File Logger

Init file logger as default logger.

if logger, err := log.NewFileLogger(level.DebugLevel, "./logs", "default_"); err == nil {
    log.InitDefaultLogger(logger)
} else {
    log.Error(err, "init file logger error")
}

Multi Logger

Init multi logger as default logger.

if logger, err := log.NewFileLogger(level.DebugLevel, "./logs", "multi_"); err == nil {
    log.InitDefaultLogger(log.NewMultiLogger(log.NewConsoleLogger(level.DebugLevel), logger))
} else {
    log.Error(err, "init file logger error")
}

Sample Logger

Init console logger as default logger and set the sample rate, default is 1.

log.InitDefaultLoggerWithSample(log.NewConsoleLogger(level.DebugLevel), 0.6)

Use default logger to write logs by random sampling.

text := "hello world"
log.DebugSample("[sample] %s, test debug log", text)
log.InfoSample("[sample] %s, test info log", text)
log.WarnSample("[sample] %s, test warn log", text)
log.ErrorSample(errors.New("log err"), "[sample] %s, test error log", text)
log.ErrorIfSample(errors.New("log err from ErrorIfSample"), "[sample] %s, test error log", text)

GoLang Resources

are all listed below.

Resources

listed to get explored on!!

Made with โค๏ธ

to provide different kinds of informations and resources.