Categorygithub.com/kpango/glg
modulepackage
1.6.15
Repository: https://github.com/kpango/glg.git
Documentation: pkg.go.dev

# README

License: MIT release CircleCI codecov Codacy Badge Go Report Card GolangCI Go Walker GoDoc DepShield Badge FOSSA Status

glg is simple golang logging library

Requirement

Go 1.16

Installation

go get github.com/kpango/glg

Example

package main

import (
	"net/http"
	"time"

	"github.com/kpango/glg"
)

// NetWorkLogger sample network logger
type NetWorkLogger struct{}

func (n NetWorkLogger) Write(b []byte) (int, error) {
	// http.Post("localhost:8080/log", "", bytes.NewReader(b))
	http.Get("http://127.0.0.1:8080/log")
	glg.Success("Requested")
	glg.Infof("RawString is %s", glg.RawString(b))
	return 1, nil
}

func main() {

	// var errWriter io.Writer
	// var customWriter io.Writer
	infolog := glg.FileWriter("/tmp/info.log", 0666)

	customTag := "FINE"
	customErrTag := "CRIT"

	errlog := glg.FileWriter("/tmp/error.log", 0666)
	defer infolog.Close()
	defer errlog.Close()

	glg.Get().
		SetMode(glg.BOTH). // default is STD
		// DisableColor().
		// SetMode(glg.NONE).
		// SetMode(glg.WRITER).
		// SetMode(glg.BOTH).
		// InitWriter().
		// AddWriter(customWriter).
		// SetWriter(customWriter).
		// AddLevelWriter(glg.LOG, customWriter).
		// AddLevelWriter(glg.INFO, customWriter).
		// AddLevelWriter(glg.WARN, customWriter).
		// AddLevelWriter(glg.ERR, customWriter).
		// SetLevelWriter(glg.LOG, customWriter).
		// SetLevelWriter(glg.INFO, customWriter).
		// SetLevelWriter(glg.WARN, customWriter).
		// SetLevelWriter(glg.ERR, customWriter).
		// EnableJSON().
		SetLineTraceMode(glg.TraceLineNone).
		AddLevelWriter(glg.INFO, infolog). // add info log file destination
		AddLevelWriter(glg.ERR, errlog).   // add error log file destination
		AddLevelWriter(glg.WARN, rotate)   // add error log file destination

	glg.Info("info")
	glg.Infof("%s : %s", "info", "formatted")
	glg.Log("log")
	glg.Logf("%s : %s", "info", "formatted")
	glg.Debug("debug")
	glg.Debugf("%s : %s", "info", "formatted")
	glg.Trace("Trace")
	glg.Tracef("%s : %s", "tracef", "formatted")
	glg.Warn("warn")
	glg.Warnf("%s : %s", "info", "formatted")
	glg.Error("error")
	glg.Errorf("%s : %s", "info", "formatted")
	glg.Success("ok")
	glg.Successf("%s : %s", "info", "formatted")
	glg.Fail("fail")
	glg.Failf("%s : %s", "info", "formatted")
	glg.Print("Print")
	glg.Println("Println")
	glg.Printf("%s : %s", "printf", "formatted")

	// set global log level to ERR level
	glg.Info("before setting level to ERR this message will show")
	glg.Get().SetLevel(glg.ERR)
	glg.Info("after setting level to ERR this message will not show")
	glg.Error("this log is ERR level this will show")
	glg.Get().SetLevel(glg.DEBG)
	glg.Info("log level is now DEBG, this INFO level log will show")

	glg.Get().
		AddStdLevel(customTag, glg.STD, false).                    // user custom log level
		AddErrLevel(customErrTag, glg.STD, true).                  // user custom error log level
		SetLevelColor(glg.TagStringToLevel(customTag), glg.Cyan).  // set color output to user custom level
		SetLevelColor(glg.TagStringToLevel(customErrTag), glg.Red) // set color output to user custom level
	glg.CustomLog(customTag, "custom logging")
	glg.CustomLog(customErrTag, "custom error logging")

	// glg.Info("kpango's glg supports disable timestamp for logging")
	glg.Get().DisableTimestamp()
	glg.Info("timestamp disabled")
	glg.Warn("timestamp disabled")
	glg.Log("timestamp disabled")
	glg.Get().EnableTimestamp()
	glg.Info("timestamp enabled")
	glg.Warn("timestamp enabled")
	glg.Log("timestamp enabled")

	glg.Info("kpango's glg support line trace logging")
	glg.Error("error log shows short line trace by default")
	glg.Info("error log shows none trace by default")
	glg.Get().SetLineTraceMode(glg.TraceLineShort)
	glg.Error("after configure TraceLineShort, error log shows short line trace")
	glg.Info("after configure TraceLineShort, info log shows short line trace")
	glg.Get().DisableTimestamp()
	glg.Error("after configure TraceLineShort and DisableTimestamp, error log shows short line trace without timestamp")
	glg.Info("after configure TraceLineShort and DisableTimestamp, info log shows short line trace without timestamp")
	glg.Get().EnableTimestamp()
	glg.Get().SetLineTraceMode(glg.TraceLineLong)
	glg.Error("after configure TraceLineLong, error log shows long line trace")
	glg.Info("after configure TraceLineLong, info log shows long line trace")
	glg.Get().DisableTimestamp()
	glg.Error("after configure TraceLineLong and DisableTimestamp, error log shows long line trace without timestamp")
	glg.Info("after configure TraceLineLong and DisableTimestamp, info log shows long line trace without timestamp")
	glg.Get().EnableTimestamp()
	glg.Get().SetLineTraceMode(glg.TraceLineNone)
	glg.Error("after configure TraceLineNone, error log without line trace")
	glg.Info("after configure TraceLineNone, info log without line trace")
	glg.Get().SetLevelLineTraceMode(glg.INFO, glg.TraceLineLong)
	glg.Info("after configure Level trace INFO=TraceLineLong, only info log shows long line trace")
	glg.Error("after configure Level trace INFO=TraceLineLong, error log without long line trace")
	glg.Get().SetLevelLineTraceMode(glg.ERR, glg.TraceLineShort)
	glg.Info("after configure Level trace ERR=TraceLineShort, info log still shows long line trace")
	glg.Error("after configure Level trace ERR=TraceLineShort, error log now shows short line trace")
	glg.Get().SetLineTraceMode(glg.TraceLineNone)

	glg.Info("kpango's glg support json logging")
	glg.Get().EnableJSON()
	err := glg.Warn("kpango's glg", "support", "json", "logging")
	if err != nil {
		glg.Get().DisableJSON()
		glg.Error(err)
		glg.Get().EnableJSON()
	}
	err = glg.Info("hello", struct {
		Name   string
		Age    int
		Gender string
	}{
		Name:   "kpango",
		Age:    28,
		Gender: "male",
	}, 2020)
	if err != nil {
		glg.Get().DisableJSON()
		glg.Error(err)
		glg.Get().EnableJSON()
	}	glg.CustomLog(customTag, "custom logging")

	glg.CustomLog(customErrTag, "custom error logging")

	glg.Get().AddLevelWriter(glg.DEBG, NetWorkLogger{}) // add info log file destination

	http.Handle("/glg", glg.HTTPLoggerFunc("glg sample", func(w http.ResponseWriter, r *http.Request) {
		glg.New().
		AddLevelWriter(glg.Info, NetWorkLogger{}).
		AddLevelWriter(glg.Info, w).
		Info("glg HTTP server logger sample")
	}))

	http.ListenAndServe("port", nil)

	// fatal logging
	glg.Fatalln("fatal")
}

Benchmarks

Contribution

  1. Fork it ( https://github.com/kpango/glg/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

kpango

LICENSE

glg released under MIT license, refer LICENSE file.
FOSSA Status

# Packages

No description provided by the author

# Functions

Atol converts level string to Glg.LEVEL.
Black returns Black colored string.
Brown returns Brown colored string.
Colorless returns colorless string.
CustomLog outputs custom level log.
CustomLogf outputs formatted custom level log.
CustomLogFunc outputs custom level log returned from the function.
Cyan returns cyan colored string.
Debug outputs Debug level log.
Debugf outputs formatted Debug level log.
DebugFunc outputs Debug level log returned from the function.
Error outputs Error log.
Errorf outputs formatted Error log.
ErrorFunc outputs Error level log returned from the function.
Fail outputs Failed log.
Failf outputs formatted Failed log.
FailFunc outputs Fail level log returned from the function.
Fatal outputs Failed log and exit program.
Fatalf outputs formatted Failed log and exit program.
Fatalln outputs line fixed Failed log and exit program.
FileWriter generates *osFile -> io.Writer.
Get returns singleton glg instance.
Gray returns Gray colored string.
Green returns green colored string.
HTTPLogger is simple http access logger.
HTTPLoggerFunc is simple http access logger.
Info outputs Info level log.
Infof outputs formatted Info level log.
InfoFunc outputs Info level log returned from the function.
Log writes std log event.
Logf writes std log event with format.
LogFunc outputs Log level log returned from the function.
New returns plain glg instance.
Orange returns orange colored string.
Print outputs Print log.
Printf outputs formatted Print log.
PrintFunc outputs Print log returned from the function.
Println outputs fixed line Print log.
Purple returns purple colored string.
RawString returns raw log string exclude time & tags.
Red returns red colored string.
ReplaceExitFunc replaces exit function.
Reset provides parameter reset function for glg struct instance.
SetPrefix sets Print logger prefix.
Success outputs Success level log.
Successf outputs formatted Success level log.
SuccessFunc outputs Success level log returned from the function.
TagStringToLevel converts level string to glg.LEVEL.
Trace outputs Trace level log.
Tracef outputs formatted Trace level log.
TraceFunc outputs Trace log returned from the function.
Warn outputs Warn level log.
Warnf outputs formatted Warn level log.
WarnFunc outputs Warn level log returned from the function.
White returns white colored string.
Yellow returns yellow colored string.

# Constants

BOTH is both log mode.
DEBG is debug log level.
No description provided by the author
ERR is error log level.
FAIL is failed log level.
FATAL is fatal log level.
INFO is info log level.
LOG is log level.
NONE is disable Logging.
OK is success notify log level.
PRINT is print log level.
STD is std log mode.
TRACE is trace log level.
No description provided by the author
No description provided by the author
No description provided by the author
UNKNOWN is unknown log level.
WARN is warning log level.
WRITER is io.Writer log mode.

# Structs

Glg is glg base struct.
JSONFormat is json object structure for logging.

# Type aliases

LEVEL is log level.
MODE is logging mode (std only, writer only, std & writer).