glg

package module
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2021 License: MIT Imports: 16 Imported by: 338

README

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

Documentation

Overview

Package glg can quickly output that are colored and leveled logs with simple syntax

Package glg can quickly output that are colored and leveled logs with simple syntax

Package glg can quickly output that are colored and leveled logs with simple syntax

Index

Constants

View Source
const (
	// DEBG is debug log level
	DEBG LEVEL = iota + 1
	// TRACE is trace log level
	TRACE
	// PRINT is print log level
	PRINT
	// LOG is log level
	LOG
	// INFO is info log level
	INFO
	// OK is success notify log level
	OK
	// WARN is warning log level
	WARN
	// ERR is error log level
	ERR
	// FAIL is failed log level
	FAIL
	// FATAL is fatal log level
	FATAL

	// UNKNOWN is unknown log level
	UNKNOWN LEVEL = LEVEL(math.MaxUint8)

	// NONE is disable Logging
	NONE MODE = iota + 1
	// STD is std log mode
	STD
	// BOTH is both log mode
	BOTH
	// WRITER is io.Writer log mode
	WRITER

	TraceLineNone traceMode = 1 << iota
	TraceLineShort
	TraceLineLong

	DefaultCallerDepth = 2
)

Variables

This section is empty.

Functions

func Black

func Black(str string) string

Black returns Black colored string

func Brown

func Brown(str string) string

Brown returns Brown colored string

func Colorless

func Colorless(str string) string

Colorless returns colorless string

func CustomLog

func CustomLog(level string, val ...interface{}) error

CustomLog outputs custom level log

func CustomLogFunc added in v1.3.0

func CustomLogFunc(level string, f func() string) error

CustomLogFunc outputs custom level log returned from the function

func CustomLogf

func CustomLogf(level string, format string, val ...interface{}) error

CustomLogf outputs formatted custom level log

func Cyan

func Cyan(str string) string

Cyan returns cyan colored string

func Debug

func Debug(val ...interface{}) error

Debug outputs Debug level log

func DebugFunc added in v1.3.0

func DebugFunc(f func() string) error

DebugFunc outputs Debug level log returned from the function

func Debugf

func Debugf(format string, val ...interface{}) error

Debugf outputs formatted Debug level log

func Error

func Error(val ...interface{}) error

Error outputs Error log

func ErrorFunc added in v1.3.0

func ErrorFunc(f func() string) error

ErrorFunc outputs Error level log returned from the function

func Errorf

func Errorf(format string, val ...interface{}) error

Errorf outputs formatted Error log

func Fail

func Fail(val ...interface{}) error

Fail outputs Failed log

func FailFunc added in v1.3.0

func FailFunc(f func() string) error

FailFunc outputs Fail level log returned from the function

func Failf

func Failf(format string, val ...interface{}) error

Failf outputs formatted Failed log

func Fatal

func Fatal(val ...interface{})

Fatal outputs Failed log and exit program

func Fatalf

func Fatalf(format string, val ...interface{})

Fatalf outputs formatted Failed log and exit program

func Fatalln

func Fatalln(val ...interface{})

Fatalln outputs line fixed Failed log and exit program

func FileWriter

func FileWriter(path string, perm os.FileMode) *os.File

FileWriter generates *osFile -> io.Writer

func Gray

func Gray(str string) string

Gray returns Gray colored string

func Green

func Green(str string) string

Green returns green colored string

func HTTPLogger

func HTTPLogger(name string, handler http.Handler) http.Handler

HTTPLogger is simple http access logger

func HTTPLoggerFunc

func HTTPLoggerFunc(name string, hf http.HandlerFunc) http.Handler

HTTPLoggerFunc is simple http access logger

func Info

func Info(val ...interface{}) error

Info outputs Info level log

func InfoFunc added in v1.3.0

func InfoFunc(f func() string) error

InfoFunc outputs Info level log returned from the function

func Infof

func Infof(format string, val ...interface{}) error

Infof outputs formatted Info level log

func Log

func Log(val ...interface{}) error

Log writes std log event

func LogFunc added in v1.3.0

func LogFunc(f func() string) error

LogFunc outputs Log level log returned from the function

func Logf

func Logf(format string, val ...interface{}) error

Logf writes std log event with format

func Orange

func Orange(str string) string

Orange returns orange colored string

func Print

func Print(val ...interface{}) error

Print outputs Print log

func PrintFunc added in v1.3.0

func PrintFunc(f func() string) error

PrintFunc outputs Print log returned from the function

func Printf

func Printf(format string, val ...interface{}) error

Printf outputs formatted Print log

func Println

func Println(val ...interface{}) error

Println outputs fixed line Print log

func Purple

func Purple(str string) string

Purple returns purple colored string

func RawString added in v1.2.10

func RawString(data []byte) string

RawString returns raw log string exclude time & tags

func Red

func Red(str string) string

Red returns red colored string

func ReplaceExitFunc added in v1.2.7

func ReplaceExitFunc(fn func(i int))

ReplaceExitFunc replaces exit function. If you do not want to start os.Exit at glg.Fatal error, use this function to register arbitrary function

func Success

func Success(val ...interface{}) error

Success outputs Success level log

func SuccessFunc added in v1.3.0

func SuccessFunc(f func() string) error

SuccessFunc outputs Success level log returned from the function

func Successf

func Successf(format string, val ...interface{}) error

Successf outputs formatted Success level log

func Trace added in v1.6.0

func Trace(val ...interface{}) error

Trace outputs Trace level log

func TraceFunc added in v1.6.0

func TraceFunc(f func() string) error

TraceFunc outputs Trace log returned from the function

func Tracef added in v1.6.0

func Tracef(format string, val ...interface{}) error

Tracef outputs formatted Trace level log

func Warn

func Warn(val ...interface{}) error

Warn outputs Warn level log

func WarnFunc added in v1.3.0

func WarnFunc(f func() string) error

WarnFunc outputs Warn level log returned from the function

func Warnf

func Warnf(format string, val ...interface{}) error

Warnf outputs formatted Warn level log

func White

func White(str string) string

White returns white colored string

func Yellow

func Yellow(str string) string

Yellow returns yellow colored string

Types

type Glg

type Glg struct {
	// contains filtered or unexported fields
}

Glg is glg base struct

func Get

func Get() *Glg

Get returns singleton glg instance

func New

func New() *Glg

New returns plain glg instance

func Reset added in v1.5.1

func Reset() *Glg

Reset provides parameter reset function for glg struct instance

func SetPrefix added in v1.2.2

func SetPrefix(lev LEVEL, pref string) *Glg

SetPrefix sets Print logger prefix

func (*Glg) AddErrLevel

func (g *Glg) AddErrLevel(tag string, mode MODE, isColor bool) *Glg

AddErrLevel adds error log level and returns LEVEL

func (*Glg) AddLevelWriter

func (g *Glg) AddLevelWriter(level LEVEL, writer io.Writer) *Glg

AddLevelWriter adds writer to glg std writer per logging level

func (*Glg) AddStdLevel

func (g *Glg) AddStdLevel(tag string, mode MODE, isColor bool) *Glg

AddStdLevel adds std log level and returns LEVEL

func (*Glg) AddWriter

func (g *Glg) AddWriter(writer io.Writer) *Glg

AddWriter adds writer to glg std writers

func (*Glg) Atol added in v1.6.0

func (g *Glg) Atol(tag string) LEVEL

Atol converts level string to Glg.LEVEL

func (*Glg) CustomLog

func (g *Glg) CustomLog(level string, val ...interface{}) error

CustomLog outputs custom level log

func (*Glg) CustomLogFunc added in v1.3.0

func (g *Glg) CustomLogFunc(level string, f func() string) error

CustomLogFunc outputs custom level log returned from the function

func (*Glg) CustomLogf

func (g *Glg) CustomLogf(level string, format string, val ...interface{}) error

CustomLogf outputs formatted custom level log

func (*Glg) Debug

func (g *Glg) Debug(val ...interface{}) error

Debug outputs Debug level log

func (*Glg) DebugFunc added in v1.3.0

func (g *Glg) DebugFunc(f func() string) error

DebugFunc outputs Debug level log returned from the function

func (*Glg) Debugf

func (g *Glg) Debugf(format string, val ...interface{}) error

Debugf outputs formatted Debug level log

func (*Glg) DisableColor

func (g *Glg) DisableColor() *Glg

DisableColor disables color output

func (*Glg) DisableJSON added in v1.5.0

func (g *Glg) DisableJSON() *Glg

func (*Glg) DisableLevelColor

func (g *Glg) DisableLevelColor(lv LEVEL) *Glg

DisableLevelColor disables color output

func (*Glg) DisableLevelTimestamp added in v1.5.6

func (g *Glg) DisableLevelTimestamp(lv LEVEL) *Glg

DisableLevelTimestamp disables timestamp output

func (*Glg) DisableTimestamp added in v1.5.6

func (g *Glg) DisableTimestamp() *Glg

DisableTimestamp disables timestamp output

func (*Glg) EnableColor

func (g *Glg) EnableColor() *Glg

EnableColor enables color output

func (*Glg) EnableJSON added in v1.5.0

func (g *Glg) EnableJSON() *Glg

func (*Glg) EnableLevelColor

func (g *Glg) EnableLevelColor(lv LEVEL) *Glg

EnableLevelColor enables color output

func (*Glg) EnableLevelTimestamp added in v1.5.6

func (g *Glg) EnableLevelTimestamp(lv LEVEL) *Glg

EnableLevelTimestamp enables timestamp output

func (*Glg) EnablePoolBuffer added in v1.5.0

func (g *Glg) EnablePoolBuffer(size int) *Glg

func (*Glg) EnableTimestamp added in v1.5.6

func (g *Glg) EnableTimestamp() *Glg

EnableTimestamp enables timestamp output

func (*Glg) Error

func (g *Glg) Error(val ...interface{}) error

Error outputs Error log

func (*Glg) ErrorFunc added in v1.3.0

func (g *Glg) ErrorFunc(f func() string) error

ErrorFunc outputs Error level log returned from the function

func (*Glg) Errorf

func (g *Glg) Errorf(format string, val ...interface{}) error

Errorf outputs formatted Error log

func (*Glg) Fail

func (g *Glg) Fail(val ...interface{}) error

Fail outputs Failed log

func (*Glg) FailFunc added in v1.3.0

func (g *Glg) FailFunc(f func() string) error

FailFunc outputs Fail level log returned from the function

func (*Glg) Failf

func (g *Glg) Failf(format string, val ...interface{}) error

Failf outputs formatted Failed log

func (*Glg) Fatal

func (g *Glg) Fatal(val ...interface{})

Fatal outputs Failed log and exit program

func (*Glg) Fatalf

func (g *Glg) Fatalf(format string, val ...interface{})

Fatalf outputs formatted Failed log and exit program

func (*Glg) Fatalln

func (g *Glg) Fatalln(val ...interface{})

Fatalln outputs line fixed Failed log and exit program

func (*Glg) GetCurrentMode

func (g *Glg) GetCurrentMode(level LEVEL) MODE

GetCurrentMode returns current logging mode

func (*Glg) HTTPLogger

func (g *Glg) HTTPLogger(name string, handler http.Handler) http.Handler

HTTPLogger is simple http access logger

func (*Glg) HTTPLoggerFunc

func (g *Glg) HTTPLoggerFunc(name string, hf http.HandlerFunc) http.Handler

HTTPLoggerFunc is simple http access logger

func (*Glg) Info

func (g *Glg) Info(val ...interface{}) error

Info outputs Info level log

func (*Glg) InfoFunc added in v1.3.0

func (g *Glg) InfoFunc(f func() string) error

InfoFunc outputs Info level log returned from the function

func (*Glg) Infof

func (g *Glg) Infof(format string, val ...interface{}) error

Infof outputs formatted Info level log

func (*Glg) InitWriter

func (g *Glg) InitWriter() *Glg

InitWriter is initialize glg writer

func (*Glg) Log

func (g *Glg) Log(val ...interface{}) error

Log writes std log event

func (*Glg) LogFunc added in v1.3.0

func (g *Glg) LogFunc(f func() string) error

LogFunc outputs Log level log returned from the function

func (*Glg) Logf

func (g *Glg) Logf(format string, val ...interface{}) error

Logf writes std log event with format

func (*Glg) Print

func (g *Glg) Print(val ...interface{}) error

Print outputs Print log

func (*Glg) PrintFunc added in v1.3.0

func (g *Glg) PrintFunc(f func() string) error

PrintFunc outputs Print log returned from the function

func (*Glg) Printf

func (g *Glg) Printf(format string, val ...interface{}) error

Printf outputs formatted Print log

func (*Glg) Println

func (g *Glg) Println(val ...interface{}) error

Println outputs fixed line Print log

func (*Glg) RawString added in v1.2.10

func (g *Glg) RawString(data []byte) string

RawString returns raw log string exclude time & tags

func (*Glg) Reset added in v1.2.7

func (g *Glg) Reset() *Glg

Reset provides parameter reset function for glg struct instance

func (*Glg) SetCallerDepth added in v1.6.3

func (g *Glg) SetCallerDepth(depth int) *Glg

SetCallerDepth configures output line trace caller depth

func (*Glg) SetLevel added in v1.5.9

func (g *Glg) SetLevel(lv LEVEL) *Glg

SetLevel sets glg global log level

func (*Glg) SetLevelColor

func (g *Glg) SetLevelColor(level LEVEL, color func(string) string) *Glg

SetLevelColor sets the color for each level

func (*Glg) SetLevelLineTraceMode added in v1.6.2

func (g *Glg) SetLevelLineTraceMode(lv LEVEL, mode traceMode) *Glg

SetLevelLineTraceMode configures output line traceFlag

func (*Glg) SetLevelMode

func (g *Glg) SetLevelMode(level LEVEL, mode MODE) *Glg

SetLevelMode sets glg logging mode* per level

func (*Glg) SetLevelWriter

func (g *Glg) SetLevelWriter(level LEVEL, writer io.Writer) *Glg

SetLevelWriter sets writer to glg std writer per logging level

func (*Glg) SetLineTraceMode added in v1.6.2

func (g *Glg) SetLineTraceMode(mode traceMode) *Glg

SetLineTraceMode configures output line traceFlag

func (*Glg) SetMode

func (g *Glg) SetMode(mode MODE) *Glg

SetMode sets glg logging mode

func (*Glg) SetPrefix

func (g *Glg) SetPrefix(lev LEVEL, pref string) *Glg

SetPrefix sets Print logger prefix

func (*Glg) SetWriter

func (g *Glg) SetWriter(writer io.Writer) *Glg

SetWriter sets writer to glg std writers

func (*Glg) Success

func (g *Glg) Success(val ...interface{}) error

Success outputs Success level log

func (*Glg) SuccessFunc added in v1.3.0

func (g *Glg) SuccessFunc(f func() string) error

SuccessFunc outputs Success level log returned from the function

func (*Glg) Successf

func (g *Glg) Successf(format string, val ...interface{}) error

Successf outputs formatted Success level log

func (*Glg) TagStringToLevel

func (g *Glg) TagStringToLevel(tag string) LEVEL

TagStringToLevel converts level string to Glg.LEVEL

func (*Glg) Trace added in v1.6.0

func (g *Glg) Trace(val ...interface{}) error

Trace outputs Trace level log

func (*Glg) TraceFunc added in v1.6.0

func (g *Glg) TraceFunc(f func() string) error

TraceFunc outputs Trace level log returned from the function

func (*Glg) Tracef added in v1.6.0

func (g *Glg) Tracef(format string, val ...interface{}) error

Tracef outputs formatted Trace level log

func (*Glg) Warn

func (g *Glg) Warn(val ...interface{}) error

Warn outputs Warn level log

func (*Glg) WarnFunc added in v1.3.0

func (g *Glg) WarnFunc(f func() string) error

WarnFunc outputs Warn level log returned from the function

func (*Glg) Warnf

func (g *Glg) Warnf(format string, val ...interface{}) error

Warnf outputs formatted Warn level log

type JSONFormat added in v1.5.2

type JSONFormat struct {
	Date   string      `json:"date,omitempty"`
	Level  string      `json:"level,omitempty"`
	File   string      `json:"file,omitempty"`
	Detail interface{} `json:"detail,omitempty"`
}

JSONFormat is json object structure for logging

type LEVEL

type LEVEL uint8

LEVEL is log level

func Atol added in v1.6.0

func Atol(tag string) LEVEL

Atol converts level string to Glg.LEVEL

func TagStringToLevel

func TagStringToLevel(tag string) LEVEL

TagStringToLevel converts level string to glg.LEVEL

func (LEVEL) String

func (l LEVEL) String() string

type MODE

type MODE uint8

MODE is logging mode (std only, writer only, std & writer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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