glg

package module
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2019 License: MIT Imports: 13 Imported by: 377

README

glg 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.11

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).
		AddLevelWriter(glg.INFO, infolog).                         // add info log file destination
		AddLevelWriter(glg.ERR, errlog).                           // add error log file destination
		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.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.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")
	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")
}

Sample Logs

Benchmarks

Bench

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

Index

Constants

View Source
const (
	// LOG is log level
	LOG LEVEL = iota
	// PRINT is print log level
	PRINT
	// INFO is info log level
	INFO
	// DEBG is debug log level
	DEBG
	// 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

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

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 return colorless string

func CustomLog

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

CustomLog outputs custom level log

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 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 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 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 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 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 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 Successf

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

Successf outputs formatted Success level log

func Warn

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

Warn outputs Warn level log

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 SetPrefix added in v1.2.2

func SetPrefix(pref string) *Glg

SetPrefix set 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) CustomLog

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

CustomLog outputs custom level log

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) 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) DisableLevelColor

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

DisableLevelColor disables color output

func (*Glg) EnableColor

func (g *Glg) EnableColor() *Glg

EnableColor enables color output

func (*Glg) EnableLevelColor

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

EnableLevelColor enables color output

func (*Glg) Error

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

Error outputs Error log

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) 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) 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) 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) 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) SetLevelColor

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

SetLevelColor sets the color for each level

func (*Glg) SetLevelMode

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

SetLevelMode set 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) SetMode

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

SetMode sets glg logging mode

func (*Glg) SetPrefix

func (g *Glg) SetPrefix(pref string) *Glg

SetPrefix set 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) 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) Warn

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

Warn outputs Warn level log

func (*Glg) Warnf

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

Warnf outputs formatted Warn level log

type LEVEL

type LEVEL uint8

LEVEL is log 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