log

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: Apache-2.0 Imports: 5 Imported by: 121

README

HOW TO USE

For simple usage

Import this package:

import "github.com/DSiSc/craft/log"

And log away:

log.DebugKV("This is a debug message.", map[string]interface{}{"hello": "world"})
log.Info("This is a info message.")

Logs printed as follows:

2018-09-03T16:21:57+08:00 |DEBUG| This is a debug message. caller=/home/kang/Workspace/go/src/hello/jtlog/jtlog_test.go:16 hello=world
2018-09-03T16:21:57+08:00 |INFO| This is a info message. caller=/home/kang/Workspace/go/src/hello/jtlog/jtlog_test.go:17

caller tells us where we log this record.

Add an appender

By default, the logs output to console, in TEXT format.

If you want to add a new appender which output into a specified log file:

log.AddFileAppender("/tmp/aaa/aaa.log", log.InfoLevel, log.JsonFmt, true, true)

The file params are: file path, log level, log format, whether to show caller, whether to show timestamp.

And do logging:

log.DebugKV("This is a debug message on console and file.", map[string]interface{}{"hello": "world"})
log.Info("This is a info message on console and file.")

In STDOUT, log output as always. And in log file /tmp/aaa/aaa.log:

cat /tmp/aaa/aaa.log
{"level":"info","caller":"/home/kang/Workspace/go/src/hello/jtlog/jtlog_test.go:68","time":"2018-09-03T16:26:31+08:00","message":"This is a info message on console and file."}

It only log out Info record, with JSON format.

You can use log.AddAppender to add appender with other io.Writer.

Change logging manners

If you want to change global-log-level:

log.SetGlobalConfig(config)

Or format of timestamp:

log.SetTimestampFormat(time.RFC3339Nano)

Multiple changes can be done as the following:

config := log.GetGlobalConfig()                 // first, get default configurations
config.TimeStampFormat = time.RFC3339Nano       // then, make changes, such as timestamp format
config.Appenders[0].Format = log.JsonFmt        //                          or logging format of the first Appender
log.SetGlobalConfig(config)                     // finally, refresh configurations with the modified config

For pros, just compose whole global Config is also OK (but not recommended):

config := &log.Config{
    ...
}
log.SetGlobalConfig(config)

Documentation

Index

Constants

View Source
const (
	// JsonFmt indicates that log output generated in form of JSON.
	JsonFmt string = "JSON"
	// TextFmt indicates that log output generated in form of TEXT.
	TextFmt string = "TEXT"
	// ConsoleLog indicates that log output to console.
	ConsoleLog string = "CONSOLE_LOG"
	// FileLog indicates that log output to console.
	FileLog string = "FILE_LOG"
	// ConsoleStdout indicates than console log output to os.Stdout
	ConsoleStdout string = "STDOUT"
	// ConsoleStderr indicates than console log output to os.Stderr
	ConsoleStderr string = "STDERR"
)

Variables

This section is empty.

Functions

func AddAppender

func AddAppender(appenderName string, output io.Writer, logLevel Level, format string, showCaller bool, showHostname bool)

AddAppender adds/replaces a new logging destination.

func AddFileAppender

func AddFileAppender(appenderName string, filePath string, logLevel Level, format string, showCaller bool, showHostname bool)

AddFileAppender adds/replaces a new logging destination that append logs to a specified file.

func Debug

func Debug(fmtmsg string, a ...interface{})

func DebugKV

func DebugKV(msg string, keyvals map[string]interface{})

func Disable

func Disable()

Disable to stop logging.

func Enable

func Enable()

Enable to start logging.

func Error

func Error(fmtmsg string, a ...interface{})

func ErrorKV

func ErrorKV(msg string, keyvals map[string]interface{})

func Fatal

func Fatal(fmtmsg string, a ...interface{})

func FatalKV

func FatalKV(msg string, keyvals map[string]interface{})

func Info

func Info(fmtmsg string, a ...interface{})

func InfoKV

func InfoKV(msg string, keyvals map[string]interface{})

func Panic

func Panic(fmtmsg string, a ...interface{})

func PanicKV

func PanicKV(msg string, keyvals map[string]interface{})

func RemoveAppender

func RemoveAppender(appenderNameToRemove string)

RemoveAppender removes a logging appender by name.

func SetAppenders

func SetAppenders(appenders map[string]*Appender)

SetAppenders sets a set of "Appenders".

func SetGlobalConfig

func SetGlobalConfig(config *Config)

SetGlobalConfig is used to refresh logging manners.

func SetGlobalLogLevel

func SetGlobalLogLevel(level Level)

SetGlobalLogLevel is used to restraint log-level of all "Appenders".

func SetOutputFlags

func SetOutputFlags(flags *OutputFlags)

SetOutputFlags is used to reconfig output flags.

func SetTimestampFormat

func SetTimestampFormat(format string)

func Warn

func Warn(fmtmsg string, a ...interface{})

func WarnKV

func WarnKV(msg string, keyvals map[string]interface{})

Types

type Appender

type Appender struct {
	Enabled      bool
	LogLevel     Level
	LogType      string
	LogPath      string
	Output       io.Writer
	Format       string
	ShowCaller   bool
	ShowHostname bool
}

Appender is responsible for delivering LogEvents to their destination.

type Config

type Config struct {
	Enabled         bool
	Provider        Provider
	GlobalLogLevel  Level
	TimeFieldFormat string
	Appenders       map[string]*Appender
	OutputFlags     *OutputFlags
}

Config includes configurations for our log, such as log-level. For more log destinations just add "Appender" into "Config.[]Appenders".

func GetGlobalConfig

func GetGlobalConfig() *Config

type Level

type Level uint8

Level defines log levels.

const (
	// DebugLevel defines debug log level.
	DebugLevel Level = iota
	// InfoLevel defines info log level.
	InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel
	// Disabled disables the logger.
	Disabled
)

func GetGlobalLogLevel

func GetGlobalLogLevel() Level

type Logger

type Logger interface {
	Debug(msg string)
	Info(msg string)
	Warn(msg string)
	Error(msg string)
	Fatal(msg string)
	Panic(msg string)

	DebugKV(msg string, keyvals map[string]interface{})
	InfoKV(msg string, keyvals map[string]interface{})
	WarnKV(msg string, keyvals map[string]interface{})
	ErrorKV(msg string, keyvals map[string]interface{})
	FatalKV(msg string, keyvals map[string]interface{})
	PanicKV(msg string, keyvals map[string]interface{})

	SetGlobalLogLevel(level Level)
	SetOutputFlags(flags *OutputFlags)
	SetTimeFieldFormat(format string)
}

Logger interface defines all behaviors of a backendLogger.

type OutputFlags

type OutputFlags struct {
	// TimestampFieldName is the field name used for the timestamp field.
	TimestampFieldName string
	// LevelFieldName is the field name used for the level field.
	LevelFieldName string
	// MessageFieldName is the field name used for the message field.
	MessageFieldName string
	// ErrorFieldName is the field name used for error fields.
	ErrorFieldName string
	// CallerFieldName is the field name used for caller field.
	CallerFieldName string
	// HostnameFieldName is the field name used for hostname field.
	HostnameFieldName string
}

OutputFlags are printed in log record that can be customized.

func GetOutputFlags

func GetOutputFlags() *OutputFlags

type Provider

type Provider uint8

Provider enumerates backend log libs.

const (
	Zerolog Provider = iota
)

Jump to

Keyboard shortcuts

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