Documentation ¶
Index ¶
- func Alert(v interface{})
- func Crit(v interface{})
- func Debug(v interface{})
- func Emerg(v interface{})
- func Err(v interface{})
- func Fatal(v interface{})
- func FprintWithColor(w io.Writer, str string, code ColorType) (int, error)
- func Info(v interface{})
- func Notice(v interface{})
- func Panic(v interface{})
- func Print(args ...interface{})
- func Printf(format string, args ...interface{})
- func Println(args ...interface{})
- func Warning(v interface{})
- type ColorType
- type Level
- type Log
- type Logger
- func (l *Logger) Alert(v interface{})
- func (l *Logger) Crit(v interface{})
- func (l *Logger) Debug(v interface{})
- func (l *Logger) Emerg(v interface{})
- func (l *Logger) Err(v interface{})
- func (l *Logger) Fatal(v interface{})
- func (l *Logger) FromCtx(ctx *gear.Context) Log
- func (l *Logger) Info(v interface{})
- func (l *Logger) New(ctx *gear.Context) (interface{}, error)
- func (l *Logger) Notice(v interface{})
- func (l *Logger) Output(t time.Time, level Level, s string) (err error)
- func (l *Logger) Panic(v interface{})
- func (l *Logger) Print(args ...interface{})
- func (l *Logger) Printf(format string, args ...interface{})
- func (l *Logger) Println(args ...interface{})
- func (l *Logger) Serve(ctx *gear.Context) error
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) SetLogConsume(fn func(Log, *gear.Context))
- func (l *Logger) SetLogFormat(logFormat string)
- func (l *Logger) SetLogInit(fn func(Log, *gear.Context))
- func (l *Logger) SetTimeFormat(timeFormat string)
- func (l *Logger) Warning(v interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fatal ¶
func Fatal(v interface{})
Fatal produce a "Emergency" log with the default logger and then calls os.Exit(1)
func FprintWithColor ¶
FprintWithColor formats string with terminal colors and writes to w. It returns the number of bytes written and any write error encountered.
func Panic ¶
func Panic(v interface{})
Panic produce a "Emergency" log with the default logger and then calls panic with the message
func Print ¶
func Print(args ...interface{})
Print produce a log in the manner of fmt.Print with the default logger, without timestamp and log level
func Printf ¶
func Printf(format string, args ...interface{})
Printf produce a log in the manner of fmt.Printf with the default logger, without timestamp and log level
Types ¶
type ColorType ¶
type ColorType uint16
ColorType represents terminal color
const ( ColorRed ColorType = 31 ColorGreen ColorType = 32 ColorYellow ColorType = 33 ColorBlue ColorType = 34 ColorMagenta ColorType = 35 ColorCyan ColorType = 36 ColorWhite ColorType = 37 ColorGray ColorType = 90 )
Color Code https://en.wikipedia.org/wiki/ANSI_escape_code 30–37: set text color to one of the colors 0 to 7, 40–47: set background color to one of the colors 0 to 7, 39: reset text color to default, 49: reset background color to default, 1: make text bold / bright (this is the standard way to access the bright color variants), 22: turn off bold / bright effect, and 0: reset all text properties (color, background, brightness, etc.) to their default values. For example, one could select bright purple text on a green background (eww!) with the code `\x1B[35;1;42m`
type Level ¶
type Level uint8
Level represents logging level https://tools.ietf.org/html/rfc5424 https://en.wikipedia.org/wiki/Syslog
const ( // EmergLevel is 0, "Emergency", system is unusable EmergLevel Level = iota // AlertLevel is 1, "Alert", action must be taken immediately AlertLevel // CritiLevel is 2, "Critical", critical conditions CritiLevel // ErrLevel is 3, "Error", error conditions ErrLevel // WarningLevel is 4, "Warning", warning conditions WarningLevel // NoticeLevel is 5, "Notice", normal but significant condition NoticeLevel // InfoLevel is 6, "Informational", informational messages InfoLevel // DebugLevel is 7, "Debug", debug-level messages DebugLevel )
type Log ¶
type Log map[string]interface{}
Log records key-value pairs for structured logging.
type Logger ¶
type Logger struct { // Destination for output, It's common to set this to a // file, or `os.Stderr`. You can also set this to // something more adventorous, such as logging to Kafka. Out io.Writer // contains filtered or unexported fields }
A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.
A custom logger example:
app := gear.New() logger := logging.New(os.Stdout) logger.SetLevel(logging.InfoLevel) logger.SetLogInit(func(log Log, ctx *gear.Context) { log["IP"] = ctx.IP() log["Method"] = ctx.Method log["URL"] = ctx.Req.URL.String() log["Start"] = time.Now() log["UserAgent"] = ctx.Get(gear.HeaderUserAgent) }) logger.SetLogConsume(func(log Log, _ *gear.Context) { end := time.Now() log["Time"] = end.Sub(log["Start"].(time.Time)) / 1e6 delete(log, "Start") if res, err := log.JSON(); err == nil { logger.Output(end, InfoLevel, res) } else { logger.Output(end, WarningLevel, log.String()) } }) app.UseHandler(logger) app.Use(func(ctx *gear.Context) error { log := logger.FromCtx(ctx) log["Data"] = []int{1, 2, 3} return ctx.HTML(200, "OK") })
func New ¶
New creates a Logger instance with given io.Writer and DebugLevel log level. the logger timestamp format is "2006-01-02T15:04:05.999Z"(JavaScript ISO date string), log format is "%s %s %s"
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v interface{})
Fatal produce a "Emergency" log and then calls os.Exit(1)
func (*Logger) FromCtx ¶
FromCtx retrieve the Log instance from the ctx with ctx.Any. Logger.New and ctx.Any will guarantee it exists.
func (*Logger) New ¶
New implements gear.Any interface,then we can use ctx.Any to retrieve a Log instance from ctx. Here also some initialization work after created.
func (*Logger) Output ¶
Output writes a string log with timestamp and log level to the output. If the level is greater than logger level, the log will be omitted. The log will be format by timeFormat and logFormat.
func (*Logger) Panic ¶
func (l *Logger) Panic(v interface{})
Panic produce a "Emergency" log and then calls panic with the message
func (*Logger) Print ¶
func (l *Logger) Print(args ...interface{})
Print produce a log in the manner of fmt.Print, without timestamp and log level
func (*Logger) Printf ¶
Printf produce a log in the manner of fmt.Printf, without timestamp and log level
func (*Logger) Println ¶
func (l *Logger) Println(args ...interface{})
Println produce a log in the manner of fmt.Println, without timestamp and log level
func (*Logger) Serve ¶
Serve implements gear.Handler interface, we can use logger as gear middleware.
app := gear.New() app.UseHandler(logging.Default()) app.Use(func(ctx *gear.Context) error { log := logging.FromCtx(ctx) log["Data"] = []int{1, 2, 3} return ctx.HTML(200, "OK") })
func (*Logger) SetLevel ¶
SetLevel set the logger's log level The default logger level is DebugLevel
func (*Logger) SetLogConsume ¶ added in v0.19.0
SetLogConsume set a log consumer handle to the logger. It will be called on a "end hook" and should write the log to underlayer logging system. The default implements is for development, the output log format:
127.0.0.1 GET /text 200 6500 - 0.765 ms
Please implements a WriteLog for your production.
func (*Logger) SetLogFormat ¶
SetLogFormat set the logger log format it should accept 3 string values: timestamp, log level and log message The default logger log format is "%s %s %s"
func (*Logger) SetLogInit ¶ added in v0.19.0
SetLogInit set a log init handle to the logger. It will be called when log created.
func (*Logger) SetTimeFormat ¶
SetTimeFormat set the logger timestamp format The default logger timestamp format is "2006-01-02T15:04:05.999Z"(JavaScript ISO date string)