Documentation
¶
Index ¶
- Constants
- func GetFlags()
- func Log(calldepth int, level Level, prefix, format string, args ...interface{})
- func Printf(level Level, format string, args ...interface{})
- func SetFlags(flags int)
- func SetLevel(level Level)
- func Shutdown()
- func Start(options ...Option) error
- type FS
- type Fields
- func (fields *Fields) Any(key string, value interface{}) *Fields
- func (fields *Fields) Bool(key string, value bool) *Fields
- func (fields *Fields) Byte(key string, value byte) *Fields
- func (fields *Fields) Complex128(key string, value complex128) *Fields
- func (fields *Fields) Complex64(key string, value complex64) *Fields
- func (fields *Fields) Date(key string, value time.Time) *Fields
- func (fields *Fields) Duration(key string, value time.Duration) *Fields
- func (fields *Fields) Error(key string, value error) *Fields
- func (fields *Fields) Exec(key string, stringer func() string) *Fields
- func (fields *Fields) Float32(key string, value float32) *Fields
- func (fields *Fields) Float64(key string, value float64) *Fields
- func (fields *Fields) Int(key string, value int) *Fields
- func (fields *Fields) Int16(key string, value int16) *Fields
- func (fields *Fields) Int32(key string, value int32) *Fields
- func (fields *Fields) Int64(key string, value int64) *Fields
- func (fields *Fields) Int8(key string, value int8) *Fields
- func (fields *Fields) Microseconds(key string, value time.Time) *Fields
- func (fields *Fields) Milliseconds(key string, value time.Time) *Fields
- func (fields *Fields) Print(s string)
- func (fields *Fields) Rune(key string, value rune) *Fields
- func (fields *Fields) Seconds(key string, value time.Time) *Fields
- func (fields *Fields) String(key string, value string) *Fields
- func (fields *Fields) Time(key string, value time.Time) *Fields
- func (fields *Fields) Type(key string, value interface{}) *Fields
- func (fields *Fields) Uint(key string, value uint) *Fields
- func (fields *Fields) Uint16(key string, value uint16) *Fields
- func (fields *Fields) Uint32(key string, value uint32) *Fields
- func (fields *Fields) Uint64(key string, value uint64) *Fields
- func (fields *Fields) Uint8(key string, value uint8) *Fields
- type File
- type FileHeader
- type FileOptions
- type Level
- type MultiFileOptions
- type Option
- func WithConsole() Option
- func WithFile(fileOptions FileOptions) Option
- func WithFlags(flags int) Option
- func WithHTTPHandler(yes bool) Option
- func WithLevel(level Level) Option
- func WithMultiFile(multiFileOptions MultiFileOptions) Option
- func WithOutput(w io.Writer) Option
- func WithPrefix(prefix string) Option
- func WithPrinter(printer Printer) Option
- func WithSync(yes bool) Option
- func WithWriters(writers ...Writer) Option
- type Prefix
- func (p Prefix) Debug() *Fields
- func (p Prefix) Error() *Fields
- func (p Prefix) Fatal() *Fields
- func (p Prefix) Info() *Fields
- func (p Prefix) Prefix(prefix string) Prefix
- func (p Prefix) Printf(level Level, format string, args ...interface{})
- func (p Prefix) Trace() *Fields
- func (p Prefix) Warn() *Fields
- type Printer
- type Writer
Examples ¶
Constants ¶
const ( Ldatetime = 1 << iota // the datetime in the local time zone: 2001/02/03 01:23:23 Llongfile // full file name and line number: /a/b/c/d.go:23 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile LUTC // if Ldatetime is set, use UTC rather than the local time zone LdefaultFlags = Ldatetime | Lshortfile // default values for the standard logger )
These flags define which text to prefix to each log entry generated by the Logger. Bits are or'ed together to control what's printed.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FS ¶ added in v1.1.5
type FS interface {
OpenFile(name string, flag int, perm os.FileMode) (File, error) // OpenFile opens the file
Remove(name string) error // Remove removes the file
Symlink(oldname, newname string) error // Symlink creates file symlink
MkdirAll(path string, perm os.FileMode) error // MkdirAll creates a directory
}
FS wraps the basic fs operations for logging
type Fields ¶ added in v1.1.5
type Fields struct {
// contains filtered or unexported fields
}
Fields holds context fields
Example ¶
package main
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/mkideal/log"
)
type testingLogWriter struct {
discard bool
buf bytes.Buffer
}
func (w *testingLogWriter) Write(level log.Level, data []byte, headerLen int) error {
if !w.discard {
w.buf.WriteByte('[')
w.buf.WriteString(level.String())
w.buf.WriteByte(']')
w.buf.WriteByte(' ')
w.buf.Write(data[headerLen:])
}
return nil
}
func (w *testingLogWriter) Close() error { return nil }
func main() {
writer := new(testingLogWriter)
log.Start(log.WithWriters(writer), log.WithLevel(log.LvINFO), log.WithPrefix("testing"))
log.Info().Int("int", 123456).Print("fields")
log.Info().Int8("int8", -12).Print("fields")
log.Info().Int16("int16", 1234).Print("fields")
log.Info().Int32("int32", -12345678).Print("fields")
log.Info().Int64("int64", 1234567890).Print("fields")
log.Info().Uint("uint", 123456).Print("fields")
log.Info().Uint8("uint8", 120).Print("fields")
log.Info().Uint16("uint16", 12340).Print("fields")
log.Info().Uint32("uint32", 123456780).Print("fields")
log.Info().Uint64("uint64", 12345678900).Print("fields")
log.Info().Float32("float32", 1234.5678).Print("fields")
log.Info().Float64("float64", 0.123456789).Print("fields")
log.Info().Complex64("complex64", 1+2i).Print("fields")
log.Info().Complex128("complex128", 1).Print("fields")
log.Info().Complex128("complex128", 2i).Print("fields")
log.Info().Byte("byte", 'h').Print("fields")
log.Info().Rune("rune", 'Å').Print("fields")
log.Info().Bool("bool", true).Print("fields")
log.Info().Bool("bool", false).Print("fields")
log.Info().String("string", "hello").Print("fields")
log.Info().Error("error", nil).Print("fields")
log.Info().Error("error", errors.New("err")).Print("fields")
log.Info().Any("any", nil).Print("fields")
log.Info().Any("any", "nil").Print("fields")
log.Info().Any("any", struct {
x int
y string
}{1, "hello"}).Print("fields")
log.Info().Type("type", nil).Print("fields")
log.Info().Type("type", "string").Print("fields")
log.Info().Type("type", new(int)).Print("fields")
const (
year = 2020
month = time.May
day = 1
hour = 12
min = 20
sec = 30
nsec = 123456789
)
t := time.Date(year, month, day, hour, min, sec, nsec, time.Local)
log.Info().Date("date", t).Print("fields")
log.Info().Time("time", t).Print("fields")
log.Info().Duration("duration", time.Millisecond*1200).Print("fields")
log.Info().String("$name", "hello").Print("fields")
log.Info().String("name of", "hello").Print("fields")
log.Prefix("prefix").Info().
String("k1", "v1").
Int("k2", 2).
Print("prefix logging")
log.Debug().String("key", "value").Print("not output")
log.Shutdown()
fmt.Print(writer.buf.String())
}
Output: [INFO] (testing) {int:123456} fields [INFO] (testing) {int8:-12} fields [INFO] (testing) {int16:1234} fields [INFO] (testing) {int32:-12345678} fields [INFO] (testing) {int64:1234567890} fields [INFO] (testing) {uint:123456} fields [INFO] (testing) {uint8:120} fields [INFO] (testing) {uint16:12340} fields [INFO] (testing) {uint32:123456780} fields [INFO] (testing) {uint64:12345678900} fields [INFO] (testing) {float32:1234.5677} fields [INFO] (testing) {float64:0.123456789} fields [INFO] (testing) {complex64:1+2i} fields [INFO] (testing) {complex128:1} fields [INFO] (testing) {complex128:2i} fields [INFO] (testing) {byte:'h'} fields [INFO] (testing) {rune:'Å'} fields [INFO] (testing) {bool:true} fields [INFO] (testing) {bool:false} fields [INFO] (testing) {string:"hello"} fields [INFO] (testing) {error:nil} fields [INFO] (testing) {error:"err"} fields [INFO] (testing) {any:nil} fields [INFO] (testing) {any:"nil"} fields [INFO] (testing) {any:"{1 hello}"} fields [INFO] (testing) {type:"nil"} fields [INFO] (testing) {type:"string"} fields [INFO] (testing) {type:"*int"} fields [INFO] (testing) {date:2020-05-01+08:00} fields [INFO] (testing) {time:2020-05-01T12:20:30.123456789+08:00} fields [INFO] (testing) {duration:1.2s} fields [INFO] (testing) {$name:"hello"} fields [INFO] (testing) {"name of":"hello"} fields [INFO] (testing/prefix) {k1:"v1" k2:2} prefix logging
func (*Fields) Complex128 ¶ added in v1.1.14
func (fields *Fields) Complex128(key string, value complex128) *Fields
func (*Fields) Microseconds ¶ added in v1.1.14
func (*Fields) Milliseconds ¶ added in v1.1.14
func (*Fields) Print ¶ added in v1.1.8
Print prints logging with context fields. After this call, the fields not available.
type File ¶ added in v1.1.4
type File interface {
io.WriteCloser
// Sync commits the current contents of the file to stable storage.
// Typically, this means flushing the file system's in-memory copy
// of recently written data to disk.
Sync() error
}
File contains the basic writable file operations for logging
type FileHeader ¶ added in v1.1.0
type FileHeader int
FileHeader represents header type of file
const ( NoHeader FileHeader = 0 // no header in file HTMLHeader FileHeader = 1 // append html header in file )
FileHeader constants
type FileOptions ¶ added in v1.1.0
type FileOptions struct {
Dir string `json:"dir"` // log directory (default: .)
Filename string `json:"filename"` // log filename (default: <appName>.log)
SymlinkedDir string `json:"symlinkeddir"` // symlinked directory is symlink enabled (default: symlinked)
NoSymlink bool `json:"nosymlink"` // doesn't create symlink to latest log file (default: false)
MaxSize int `json:"maxsize"` // max bytes number of every log file(default: 64M)
Rotate bool `json:"rotate"` // enable log rotate (default: no)
Suffix string `json:"suffix"` // filename suffixa(default: .log)
DateFormat string `json:"dateformat"` // date format string for filename (default: %04d%02d%02d)
Header FileHeader `json:"header"` // header type of file (default: NoHeader)
FS FS `json:"-"` // custom filesystem (default: stdFS)
}
FileOptions represents options of file writer
type Level ¶ added in v1.1.0
type Level int32
Level represents log level
const ( LvFATAL Level // 1 LvERROR // 2 LvWARN // 3 LvINFO // 4 LvDEBUG // 5 LvTRACE // 6 )
Level constants
func ParseLevel ¶
ParseLevel parses log level from string
func (Level) MarshalJSON ¶ added in v1.1.0
MarshalJSON implements json.Marshaler
func (Level) MoreVerboseThan ¶ added in v1.1.0
MoreVerboseThan returns whether level more verbose than other
func (*Level) Set ¶ added in v1.1.0
Set implements flag.Value interface such that you can use level as a command as following:
var level logger.Level flag.Var(&level, "log_level", "log level: trace/debug/info/warn/error/fatal")
func (*Level) UnmarshalJSON ¶ added in v1.1.0
UnmarshalJSON implements json.Unmarshaler
type MultiFileOptions ¶ added in v1.1.0
type MultiFileOptions struct {
FileOptions
FatalDir string `json:"fataldir"` // fatal subdirectory (default: fatal)
ErrorDir string `json:"errordir"` // error subdirectory (default: error)
WarnDir string `json:"warndir"` // warn subdirectory (default: warn)
InfoDir string `json:"infodir"` // info subdirectory (default: info)
DebugDir string `json:"debugdir"` // debug subdirectory (default: debug)
TraceDir string `json:"tracedir"` // trace subdirectory (default: trace)
}
MultiFileOptions represents options for multi file writer
type Option ¶ added in v1.1.0
type Option func(*startOptions)
Option is option for Start
func WithFile ¶ added in v1.1.0
func WithFile(fileOptions FileOptions) Option
WithFile appends a file writer
func WithHTTPHandler ¶ added in v1.1.0
WithHTTPHandler enable or disable http handler for settting level
func WithMultiFile ¶ added in v1.1.0
func WithMultiFile(multiFileOptions MultiFileOptions) Option
WithMultiFile appends a multifile writer
func WithOutput ¶ added in v1.1.15
WithOutput appends a console writer with specified io.Writer
func WithPrinter ¶ added in v1.1.0
WithPrinter specify custom printer
func WithWriters ¶ added in v1.1.4
WithWriters appends the writers
type Prefix ¶ added in v1.1.6
type Prefix string
Prefix wraps a string as a prefixed logger
type Printer ¶ added in v1.1.0
type Printer interface {
// Start starts the printer
Start()
// Quit quits the printer
Shutdown()
// Flush flushs all queued logs
Flush()
// GetFlags returns the flags
GetFlags() int
// SetFlags sets the flags
SetFlags(flags int)
// GetLevel returns log level
GetLevel() Level
// SetLevel sets log level
SetLevel(Level)
// SetPrefix sets log prefix
SetPrefix(string)
// Printf outputs leveled logs with specified calldepth and extra prefix
Printf(calldepth int, level Level, prefix, format string, args ...interface{})
}
Printer represents the printer for logging