logger

package module
v0.0.0-...-dcb9944 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: MIT Imports: 12 Imported by: 12

README

logger

Build Status

Efficent and simpe logger for golang with more features supported.

  • custom log levels

  • custom log tags

  • custom log colors (only worked for *NIX)

  • custom log output and output format

Install

go get -u github.com/dolab/logger

Usage

import "github.com/dolab/logger"

func main() {
    log, _ := logger.New("stdout")
    log.SetLevel(logger.Ldebug)

    // normal
    log.Debug("Hello, logger!")
    log.Infof("Hello, %s!", "logger")

    // create new logger with tags based on log
    taggedLog := log.New("X-REQUEST-ID")
    taggedLog.Debug("Receive HTTP request")
    taggedLog.Warnf("Send response with %d.", 200)
}

Output

  • stdout = os.Stdout
  • stderr = os.Stderr
  • null | nil = os.DevNull
  • path/to/file = os.OpenFile("path/to/file", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)

Level

  • Ldebug = DEBUG
  • Linfo = INFO
  • Lwarn = WARN
  • Lerror = ERROR
  • Lfatal = FATAL
  • Lpanic = PANIC
  • Ltrace = Stack

License

MIT

Author

Spring MC

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLevel = errors.New("Invalid level")
)

Functions

This section is empty.

Types

type Level

type Level int
const (
	Ldebug Level
	Linfo
	Lwarn
	Lerror
	Lfatal
	Lpanic
	Ltrace
	Llog
)

func ResolveLevelByName

func ResolveLevelByName(name string) Level

Resolves level by name, returns lmin without definition by default

func (Level) IsValid

func (l Level) IsValid() bool

func (Level) String

func (l Level) String() string

type Logger

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

func New

func New(output string) (*Logger, error)

New creates a logger with the requested output. (default to stderr) NOTE: available outputs are [stdout|stderr|null|nil|path/to/file]

func (*Logger) AddTags

func (l *Logger) AddTags(tags ...string)

AddTags adds new tags to all logs, duplicated tags will be ignored.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal calls l.Output to print to the logger and exit process with sign 1. Arguments are handled in the manner of fmt.Print.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf calls l.Output to print to the logger and exit process with sign 1. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Flag

func (l *Logger) Flag() int

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Level

func (l *Logger) Level() Level

func (*Logger) New

func (l *Logger) New(tags ...string) *Logger

New allocates a new Logger for given tags with shared output

func (*Logger) Output

func (l *Logger) Output(level Level, s string) error

Output writes the output for a logging event. The string s contains the text to print after the tags specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic calls l.Output to print to the logger and panic process. Arguments are handled in the manner of fmt.Print.

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf calls l.Output to print to the logger and panic process. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) SetColor

func (l *Logger) SetColor(colorful bool)

SetColor sets whether output logs with colorful

func (*Logger) SetFlag

func (l *Logger) SetFlag(flag int)

SetFlag changes flag of source file path format

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level) error

SetLevel sets min level of output

func (*Logger) SetLevelByName

func (l *Logger) SetLevelByName(name string) error

SetLevelByName sets min level of output by name, available values are [debug|info|warn|error|fatal|panic|stack]. It returns ErrLevel for invalid name.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets output of Logger

func (*Logger) SetSkip

func (l *Logger) SetSkip(depth int)

SetSkip changes the PC

func (*Logger) SetTags

func (l *Logger) SetTags(tags ...string)

SetTags sets tags of all logs, it'll replace previous definition.

func (*Logger) Skip

func (l *Logger) Skip() int

func (*Logger) Tags

func (l *Logger) Tags() []string

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace calls l.Output to print to the logger and output process stacks, and exit process with sign 1 at last. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Write

func (l *Logger) Write(b []byte) (int, error)

Write implements io.Writer interface

Jump to

Keyboard shortcuts

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