logger

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2018 License: Apache-2.0 Imports: 6 Imported by: 7

Documentation

Overview

A logger package that compatible with sdk log package. It supports level logging and file auto rotating.

It also has a predefined 'standard' Logger called StdLogger accessible through helper functions Debug[f], Info[f], Warn[f], Error[f], LogLevel and SetJack.

It supports 4 level:

LevelError = iota
LevelWarning
LevelInformational
LevelDebug

You can use LogLevel to handle the log level.

File rotating based on package gopkg.in/natefinch/lumberjack.v2, you can control file settings by using SetJack.

Quick start

Use StdLogger:

import "github.com/mnhkahn/gogogo/logger"

logger.Info("hello, world.")

Defined our own logger:

l := logger.NewWriterLogger(w, flag, 3)
l.Info("hello, world")

Log flags compatible with sdk log package, https://golang.org/pkg/log/#pkg-constants.

See also

For more information, goto godoc https://godoc.org/github.com/mnhkahn/gogogo/logger

Chinese details, goto http://blog.cyeam.com//golang/2017/07/14/go-log?utm_source=github

Example
package main

import (
	"github.com/mnhkahn/gogogo/logger"
)

func main() {
	logger.Info("hello, world.")
}
Output:

Index

Examples

Constants

View Source
const (
	LevelError = iota
	LevelWarning
	LevelInformational
	LevelDebug
)

logger supports 4 levels. Default level is LevelInformational.

Variables

View Source
var (
	StdLogger = NewLogger(log.LstdFlags|log.Lshortfile, 3)
)

StdLogger is a predefined logger prints to stdout.

Functions

func Debug

func Debug(v ...interface{})

Debug print log with level Debug.

func Debugf

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

Debugf print log with level Debug.

func Error

func Error(v ...interface{})

Error print log with level Error.

func Errorf

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

Errorf print log with level Error.

func GenerateFmtStr

func GenerateFmtStr(n int) string

GenerateFmtStr is a helper function to construct formatter string.

func Info

func Info(v ...interface{})

Info print log with level Info.

func Infof

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

Infof print log with level Info.

func LogLevel

func LogLevel(logLevel string) string

LogLevel sets the log level.

func SetJack

func SetJack(lfn string, maxsize int)

SetJack sets the StdLogger's writer to file lfn.

func Warn

func Warn(v ...interface{})

Warn print log with level Warn.

func Warnf

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

Warnf print log with level Warn.

Types

type Logger

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

Logger ...

func NewFileLogger added in v1.0.3

func NewFileLogger(lfn string, maxsize int, flag int, depth int) *Logger

NewFileLogger makes a new file logger, it prints to file lfn. File will auto rotate by size maxsize. maxsize is the maximum size in megabytes of the log file

func NewLogger

func NewLogger(flag int, depth int) *Logger

NewLogger makes a new logger prints to stdout.

Example
package main

import (
	"github.com/mnhkahn/gogogo/logger"
	"log"
	"os"
)

func main() {
	w := os.Stdout
	flag := log.Llongfile
	l := logger.NewWriterLogger(w, flag, 3)
	l.Info("hello, world")
}
Output:

func NewLogger2

func NewLogger2(lfn string, maxsize int, flag int, numWorkers int, jobQueueLen int, depth int) *Logger

Deprecated NewLogger2 use NewFileLogger instead.

func NewLogger3

func NewLogger3(w io.Writer, flag int, numWorkers int, jobQueueLen int, depth int) *Logger

Deprecated NewLogger3 use NewWriterLogger instead.

func NewWriterLogger added in v1.0.3

func NewWriterLogger(w io.Writer, flag int, depth int) *Logger

NewWriterLogger makes a new writer file, it prints to writer.

func (*Logger) Debug

func (ll *Logger) Debug(format string, v ...interface{})

Debug print log with level Debug.

func (*Logger) Error

func (ll *Logger) Error(format string, v ...interface{})

Error print log with level Error.

func (*Logger) GetLevel

func (ll *Logger) GetLevel() string

GetLevel gets the current log level name.

func (*Logger) Info

func (ll *Logger) Info(format string, v ...interface{})

Info print log with level Info.

func (*Logger) SetFlag

func (ll *Logger) SetFlag(flag int)

SetFlag sets log flags. For more information, see the sdk https://golang.org/pkg/log/#pkg-constants.

func (*Logger) SetJack

func (ll *Logger) SetJack(lfn string, maxsize int)

SetJack makes logger writes to new file lfn.

func (*Logger) SetLevel

func (ll *Logger) SetLevel(l int) int

SetLevel sets the log level.

func (*Logger) SetPrefix

func (ll *Logger) SetPrefix(prefix string)

SetPrefix set the logger prefix. Default prefix is [D] for Debug, [I] for Info, [W] for Warn and [E] for Error.

func (*Logger) Warn

func (ll *Logger) Warn(format string, v ...interface{})

Warn print log with level Warn.

Jump to

Keyboard shortcuts

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