logging

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

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

Go to latest
Published: Nov 30, 2017 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-logging

go-logging is the wrapper of log15, but it also supports Debugf, Infof, Warnf, Errorf and Critf, etc.

The package initialized a default global Logger and you maybe and should use it. For example,

// Initialize the default global logger.
// Notice: this is optional. The log is output to `os.Stderr` by default.
handler := NewHandler(...)
logging.SetHandler(handler)

// Write the log
logging.Debugf("debug: %d", 1)
logging.Infof("info: %s", "test")

Use the Rotating Log Handler based on the file size.

package main

import (
    logging "github.com/xgfone/go-logging"
    "github.com/xgfone/go-tools/log2/handler"
)

func main() {
    // Initialize
    h := handler.NewSizedRotatingFile("/path/to/filename.log", 1024*1024*512, 30)
    logging.SetHandler(h, logging.LogfmtFormat())

    // Output the log
    logging.Info("%s %d", "test", 123)
}

Documentation

Overview

Package logging is the wrapper of log15(https://github.com/inconshreveable/log15), but it also supports Debugf, Infof, Warnf, Errorf and Critf.

The package initialized a default global `Logger` and you maybe and should use it. For example,

// Initialize the logger.
// Notice: this is optional. The log is output to os.Stderr by default.
handler := NewHandler(...)
logging.SetHandler(handler)

// Write the log
logging.Debugf("debug: %d", 1)
logging.Infof("info: %s", "test")

Index

Constants

This section is empty.

Variables

View Source
var (
	// TerminalFormat is a terminal formatter.
	// It's the alias of log15.TerminalFormat.
	TerminalFormat = log15.TerminalFormat

	// LogfmtFormat is a logfmt formatter.
	// It's the alias of log15.LogfmtFormat.
	LogfmtFormat = log15.LogfmtFormat

	// JsonFormat is a JSON formatter to format the record into JSON.
	// It's the alias of log15.JsonFormat.
	JsonFormat = log15.JsonFormat

	// JsonFormatEx is a JSON formatter, which can output the pretty log.
	// It's the alias of log15.JsonFormatEx.
	JsonFormatEx = log15.JsonFormatEx
)

some predefined formatters.

View Source
var (
	// BufferedHandler is a buffer handler.
	//
	// It's the alias of log15.BufferedHandler.
	BufferedHandler = log15.BufferedHandler

	// CallerFileHandler will add the line number of the file name of the
	// calling function with the key "caller" to the context.
	//
	// It's the alias of log15.CallerFileHandler
	CallerFileHandler = log15.CallerFileHandler

	// CallerFuncHandler will add the calling function name with the key "key"
	// to the context.
	//
	// It's the alias of log15.CallerFuncHandler.
	CallerFuncHandler = log15.CallerFuncHandler

	// CallerStackHandler will add the stack trace with the key "stack" to the
	// context.
	//
	// It's the alias of log15.CallerStackHandler.
	CallerStackHandler = log15.CallerStackHandler

	// ChannelHandler will write all log records to the given channel,
	// which blocks if the channel is full.
	//
	// It's the alias of log15.ChannelHandler.
	ChannelHandler = log15.ChannelHandler

	// DiscardHandler will discard all log records then report success.
	//
	// It's the alias of log15.DiscardHandler.
	DiscardHandler = log15.DiscardHandler

	// FailoverHandler will write all log records to the first handler, but will
	// failover and write to the second handler if the first handler has failed.
	// And so on for all handlers.
	//
	// It's the alias of log15.FailoverHandler.
	FailoverHandler = log15.FailoverHandler

	// FileHandler is a file handler.
	//
	// It's the alias of log15.FileHandler.
	FileHandler = log15.FileHandler

	// FilterHandler is a handler with the filter.
	//
	// It's the alias of log15.FilterHandler.
	FilterHandler = log15.FilterHandler

	// FuncHandler will write all log records to the given function.
	//
	// It's the alias of log15.FuncHandler.
	FuncHandler = log15.FuncHandler

	// LazyHandler is a lazy handler.
	//
	// It's the alias of log15.LazyHandler.
	LazyHandler = log15.LazyHandler

	// LvlFilterHandler is a handler with a level filter.
	//
	// It's the alias of log15.LvlFilterHandler.
	LvlFilterHandler = log15.LvlFilterHandler

	// MatchFilterHandler only writes the log records matching the key-value.
	//
	// It's the alias of log15.MatchFilterHandler.
	MatchFilterHandler = log15.MatchFilterHandler

	// MultiHandler will dispatch the log record to each of the given handlers.
	//
	// It's the alias of log15.MultiHandler.
	MultiHandler = log15.MultiHandler

	// NetHandler will send out the log records by the network.
	//
	// It's the alias of log.NetHandler.
	NetHandler = log15.NetHandler

	// StreamHandler will write all log records to the io.Writer.
	//
	// It's the alias of log15.StreamHandler.
	StreamHandler = log15.StreamHandler

	// SyncHandler will guarantee that only a single log operation can proceed
	// at a time.
	//
	// It's the alias of log15.SyncHandler.
	SyncHandler = log15.SyncHandler
)

Some handlers

View Source
var (
	// LvlCrit is the critical level.
	LvlCrit = log15.LvlCrit

	// LvlError is the error level.
	LvlError = log15.LvlError

	// LvlWarn is the warning level.
	LvlWarn = log15.LvlWarn

	// LvlInfo is the info level.
	LvlInfo = log15.LvlInfo

	// LvlDebug is the debug level.
	LvlDebug = log15.LvlDebug
)

The predefined levels.

View Source
var FormatFunc = log15.FormatFunc

FormatFunc converts a function to Format.

View Source
var LvlFromString = log15.LvlFromString

LvlFromString convert the level name to Lvl.

Functions

func Crit

func Crit(msg string, ctx ...interface{})

Crit is equal to GetDefaultLogger().Crit(msg, ctx...).

func Critf

func Critf(format string, args ...interface{})

Critf is equal to GetDefaultLogger().Critf(format, args...).

func Debug

func Debug(msg string, ctx ...interface{})

Debug is equal to GetDefaultLogger().Debug(msg, ctx...).

func Debugf

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

Debugf is equal to GetDefaultLogger().Debugf(format, args...).

func Error

func Error(msg string, ctx ...interface{})

Error is equal to GetDefaultLogger().Error(msg, ctx...).

func Errorf

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

Errorf is equal to GetDefaultLogger().Errorf(format, args...).

func Info

func Info(msg string, ctx ...interface{})

Info is equal to GetDefaultLogger().Info(msg, ctx...).

func Infof

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

Infof is equal to GetDefaultLogger().Infof(format, args...).

func SetDefaultLogger

func SetDefaultLogger(l Logger)

SetDefaultLogger resets the default global logger.

func SetHandler

func SetHandler(h Handler)

SetHandler updates the default global logger to write records to the specified handler.

func Warn

func Warn(msg string, ctx ...interface{})

Warn is equal to GetDefaultLogger().Warn(msg, ctx...).

func Warnf

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

Warnf is equal to GetDefaultLogger().Warnf(format, args...).

Types

type Format

type Format = log15.Format

Format is the formatter interface.

type Handler

type Handler = log15.Handler

Handler is the alias of log15.Handler.

func GetHandler

func GetHandler() Handler

GetHandler gets the handler associated with the default global logger.

type Lazy

type Lazy = log15.Lazy

Lazy is the lazy type, which allows you to defer the calculation of a logged value.

type Logger

type Logger struct {
	log15.Logger
}

Logger is a logger based on log15.

func GetDefaultLogger

func GetDefaultLogger() Logger

GetDefaultLogger returns the default global logger.

func NewLogger

func NewLogger(logger ...Logger) Logger

NewLogger returns a new Logger.

func (Logger) Critf

func (l Logger) Critf(format string, args ...interface{})

Critf is equal to l.Crit(fmt.Sprintf(format, args...)).

func (Logger) Debugf

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

Debugf is equal to l.Debug(fmt.Sprintf(format, args...)).

func (Logger) Errorf

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

Errorf is equal to l.Error(fmt.Sprintf(format, args...)).

func (Logger) Infof

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

Infof is equal to l.Info(fmt.Sprintf(format, args...)).

func (Logger) New

func (l Logger) New(cxt ...interface{}) Logger

New returns a new Logger based on the current Logger.

func (Logger) Warnf

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

Warnf is equal to l.Warn(fmt.Sprintf(format, args...)).

type Lvl

type Lvl = log15.Lvl

Lvl is the level type.

type Record

type Record = log15.Record

Record represents a record, which is the alias of log15.Record.

type RecordKeyNames

type RecordKeyNames = log15.RecordKeyNames

RecordKeyNames are the predefined names of the log properties.

Jump to

Keyboard shortcuts

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