Documentation
¶
Overview ¶
Package j implements a simple logging package. Easy to use.
It has more feature than standard logger pkg, for example:
· log files rotation
· background (tunnel) write
· ANSI color pre line
· temporarily disable
Index ¶
- Constants
- Variables
- func GetDefault() (m map[string]interface{})
- func SetDefault(k configKey, v interface{}) (ok bool)
- func UnsetDefault(k configKey)
- type Config
- type ErrorFunc
- type FileFunc
- type LineFunc
- type Logger
- func (o *Logger) BR() (err error)
- func (o *Logger) Close()
- func (o *Logger) Color(s string) (err error)
- func (o *Logger) ColorOnce(s string) (err error)
- func (o *Logger) ColorStop() (err error)
- func (o *Logger) Compact(a ...interface{}) (err error)
- func (o *Logger) Enable(is bool)
- func (o *Logger) GetFile() *os.File
- func (o *Logger) Log(a ...interface{}) (err error)
- func (o *Logger) Logf(format string, a ...interface{}) (err error)
- func (o *Logger) Print(a ...interface{}) (err error)
- func (o *Logger) Raw(a ...interface{}) (err error)
- func (o *Logger) SetFile(f *os.File)
Examples ¶
Constants ¶
const ( Echo = configKey(iota + 1) Append Prefix TimeFormat Tunnel LineFn Caller PermFile PermDir ErrorFn )
Config key, used by SetDefault()
const ( CallerNone = callerType(iota + 1) CallerShort // caller.go:42 CallerShorter // caller:42 CallerLong // /dir/caller.go:42 )
just like Lshortfile/Llongfile in pkg/log
const ( TimeMS = `15:04:05.000 ` TimeDayMS = `02 15:04:05.000 ` TimeMonthMS = `01-02 15:04:05.000 ` TimeYearMS = `2006-01-02 15:04:05.000 ` Time = `15:04:05 ` TimeDay = `02 15:04:05 ` TimeMonth = `01-02 15:04:05 ` TimeYear = `2006-01-02 15:04:05 ` TimeNS = `15:04:05.000000000 ` )
time format in config
Variables ¶
var (
ErrTunnelOverflow = errors.New(`Tunnel overflow, log dropped`)
)
Errors
Functions ¶
func SetDefault ¶ added in v1.0.2
func SetDefault(k configKey, v interface{}) (ok bool)
SetDefault for new logger
Types ¶
type Config ¶
type Config struct {
// fill only one of them
File *os.File
FileFn FileFunc
Filename string
Echo bool // stdout
Append bool
Prefix string
TimeFormat string
Tunnel int // channel buffer size, see also Close()
LineFn LineFunc
ErrorFn ErrorFunc
Caller callerType
PermFile os.FileMode
PermDir os.FileMode
}
Config for create logger
type ErrorFunc ¶ added in v1.0.3
type ErrorFunc func(o *Logger)
ErrorFunc is callback when trigger error
type FileFunc ¶ added in v1.0.2
FileFunc is the type of the function called for getting filename. If the filename is different from the previous one, a new log file will be created
type LineFunc ¶ added in v1.0.2
type LineFunc func(line *string)
LineFunc is the type of the function called by from Log / Logf / Print / Compact, but Raw.
Example ¶
package main
import (
"regexp"
"github.com/zhengkai/j"
)
func main() {
re := regexp.MustCompile(`(foo|bar)`)
repl := " [ $1 ] "
fn := func(s *string) {
r := re.ReplaceAllString(*s, repl)
*s = r
}
x := j.New(&j.Config{
LineFn: fn,
})
x.Log(`afoob`)
}
Output: 17:04:31.829 example_test.go:29 a [ foo ] b
type Logger ¶
type Logger struct {
Error error
// contains filtered or unexported fields
}
Logger will be always returned by New series func, if there is any error, recorded in Error
func NewFunc ¶
NewFunc create a new logger with FileFunc
Example ¶
package main
import (
"time"
"github.com/zhengkai/j"
)
func main() {
jx := j.NewFunc(func(t *time.Time) string {
return t.Format(`log/01-02/15.log`)
})
jx.Log(`file func`)
}
Output:
func (*Logger) Close ¶
func (o *Logger) Close()
Close permanently disable Log() series. log files will be closed, unless come from SetFile() or config File.
Note: if create by config Tunnel, must use Close() to wait that all logs have been processed