logex

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 11 Imported by: 55

README

Logex

Build Status GoDoc

An golang log lib, supports tracing and level, wrap by standard log lib

How To Get

shell

go get github.com/chzyer/logex

source code

import "github.com/chzyer/logex" // package name is logex

func main() {
  logex.Info("Hello!")
}

Level

import "github.com/chzyer/logex"

func main() {
  logex.Println("")
  logex.Debug("debug staff.") // Only show if has an "DEBUG" named env variable(whatever value).
  logex.Info("info")
  logex.Warn("")
  logex.Fatal("") // also trigger exec "os.Exit(1)"
  logex.Error(err) // print error
  logex.Struct(obj) // print objs follow such layout "%T(%+v)"
  logex.Pretty(obj) // print objs as JSON-style, more readable and hide non-publish properties, just JSON
}

Extendability

source code

type MyStruct struct {
  BiteMe bool
}

may change to

type MyStruct struct {
  BiteMe bool
  logex.Logger // just this
}

func main() {
  ms := new(MyStruct)
  ms.Info("woo!")
}

Runtime Tracing

All log will attach theirs stack info. Stack Info will shown by an layout, {packageName}.{FuncName}:{FileName}:{FileLine}

package main

import "github.com/chzyer/logex"

func test() {
	logex.Pretty("hello")
}

func main() {
	test()
}

response

2014/10/10 15:17:14 [main.test:testlog.go:6][PRETTY] "hello"

Error Tracing

You can trace an error if you want.

package main

import (
	"github.com/chzyer/logex"
	"os"
)

func openfile() (*os.File, error) {
	f, err := os.Open("xxx")
	if err != nil {
		err = logex.Trace(err)
	}
	return f, err
}

func test() error {
	f, err := openfile()
	if err != nil {
		return logex.Trace(err)
	}
	f.Close()
	return nil
}

func main() {
	err := test()
	if err != nil {
		logex.Error(err)
		return
	}
	logex.Info("test success")
}

response

2014/10/10 15:22:29 [main.main:testlog.go:28][ERROR] [main.openfile:11;main.test:19] open xxx: no such file or directory

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Println    = std.Println
	Infof      = std.Infof
	Info       = std.Info
	Debug      = std.Debug
	Debugf     = std.Debugf
	Error      = std.Error
	Errorf     = std.Errorf
	Warn       = std.Warn
	PrintStack = std.PrintStack
	Stack      = std.Stack
	Panic      = std.Panic
	Fatal      = std.Fatal
	Struct     = std.Struct
	Pretty     = std.Pretty
	Todo       = std.Todo
)
View Source
var (
	INFO   = "[INFO] "
	ERROR  = "[ERROR] "
	PANIC  = "[PANIC] "
	DEBUG  = "[DEBUG] "
	WARN   = "[WARN] "
	FATAL  = "[FATAL] "
	STRUCT = "[STRUCT] "
	PRETTY = "[PRETTY] "
	TODO   = "[TODO] "
)
View Source
var DebugLevel = 1
View Source
var ShowCode = true

Functions

func DecodeError

func DecodeError(e error) string

func Define

func Define(info string) *traceError

func Equal

func Equal(e1, e2 error) bool

func EqualAny

func EqualAny(e error, es []error) bool

func NewError

func NewError(info ...interface{}) *traceError

func NewErrorf

func NewErrorf(format string, info ...interface{}) *traceError

func NewGoLog

func NewGoLog(w io.Writer) *goLog.Logger

func SetStd

func SetStd(l *Logger)

func Sprint

func Sprint(o ...interface{}) string

func Sprintf

func Sprintf(f string, o ...interface{}) string

func Trace

func Trace(err error, info ...interface{}) error

func TraceError

func TraceError(err error, info ...interface{}) *traceError

set runtime info to error

func TraceEx

func TraceEx(depth int, err error, info ...interface{}) *traceError

func Tracef

func Tracef(err error, obj ...interface{}) *traceError

func Tracefmt

func Tracefmt(layout string, objs ...interface{}) error

Types

type Logger

type Logger struct {
	Logger *goLog.Logger
	// contains filtered or unexported fields
}

func DownLevel

func DownLevel(i int) Logger

func NewLogger

func NewLogger(l int) *Logger

func NewLoggerEx

func NewLoggerEx(w io.Writer) *Logger

func (Logger) Debug

func (l Logger) Debug(o ...interface{})

func (Logger) Debugf

func (l Logger) Debugf(f string, o ...interface{})

func (Logger) DownLevel

func (l Logger) DownLevel(i int) Logger

decide to show which level's stack

func (Logger) Error

func (l Logger) Error(o ...interface{})

func (Logger) Errorf

func (l Logger) Errorf(f string, o ...interface{})

func (Logger) Fatal

func (l Logger) Fatal(o ...interface{})

func (Logger) Fatalf

func (l Logger) Fatalf(f string, o ...interface{})

func (Logger) Info

func (l Logger) Info(o ...interface{})

func (Logger) Infof

func (l Logger) Infof(f string, o ...interface{})

func (Logger) Output

func (l Logger) Output(calldepth int, s string) error

func (Logger) Panic

func (l Logger) Panic(o ...interface{})

func (Logger) Panicf

func (l Logger) Panicf(f string, o ...interface{})

func (Logger) Pretty

func (l Logger) Pretty(os ...interface{})

output objects to json format

func (Logger) Print

func (l Logger) Print(o ...interface{})

just print

func (Logger) PrintStack

func (l Logger) PrintStack()

func (Logger) Printf

func (l Logger) Printf(layout string, o ...interface{})

just print by format

func (Logger) Println

func (l Logger) Println(o ...interface{})

just println

func (Logger) Stack

func (l Logger) Stack() []byte

func (Logger) Struct

func (l Logger) Struct(o ...interface{})

func (Logger) Todo

func (l Logger) Todo(o ...interface{})

func (Logger) Warn

func (l Logger) Warn(o ...interface{})

func (Logger) Warnf

func (l Logger) Warnf(f string, o ...interface{})

Jump to

Keyboard shortcuts

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