Documentation
¶
Overview ¶
Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined 'standard' Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.
Index ¶
Constants ¶
View Source
const ( // Bits or'ed together to control what's printed. // There is no control over the order they appear (the order listed // here) or the format they present (as described in the comments). // The prefix is followed by a colon only when Llongfile or Lshortfile // is specified. // For example, flags Ldate | Ltime (or LstdFlags) produce, // 2009/01/23 01:23:23 message // while flags Ldate | Ltime | Lmicroseconds | Llongfile produce, // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message Ldate = 1 << iota // the date in the local time zone: 2009/01/23 Ltime // the time in the local time zone: 01:23:23 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. 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 Ldate or Ltime is set, use UTC rather than the local time zone LstdFlags = Ldate | Ltime // initial values for the standard logger )
These flags define which text to prefix to each log entry generated by the Logger.
Variables ¶
View Source
var GetExeBaseName func() string
View Source
var GetLogDir func() string
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger interface { // Name 获取日志的文件名 Name() string // Output writes the output for a logging event. The string s contains // the text to print after the prefix specified by the flags of the // Logger. A newline is appended if the last character of s is not // already a newline. Calldepth is used to recover the PC and is // provided for generality, although at the moment on all pre-defined // paths it will be 2. Output(loglv LOGlevl, newln bool, calldepth int, s string) error // RecoverCtx 调用recover捕获异常,并在标题下打印异常栈,用于defer语句 // panicfunc func()以作异常最后特殊处理比如恢复数据等,set_context函数可以返回异常函数的一些变量数据以打印到栈标题处 RecoverCtx(loglv LOGlevl, panicfun func(err any), set_context func() string) (recover_err any) // Recoverf 调用recover捕获异常,并在标题下打印异常栈,用于defer语句 // v可以额外最后传递一个参数panicfunc func()以作异常最后特殊处理比如恢复数据等 Recoverf(loglv LOGlevl, format string, v ...any) (recover_err any) // Recoverln 最简单的在标题下打印异常函数,用于defer语句 Recoverln(loglv LOGlevl, format string, v ...any) (recover_err any) // Stackf 在日志中打印任何调用Stackf的回溯调用栈 Stackf(loglv LOGlevl, format string, v ...any) // SetOutput sets the output destination for the logger. // 现有日志文件会自动更名备份为bakName,如果bakName为空,会自动产生一时间标记的唯一文件名; // w只有在newName为""的时候才会生效 SetOutput(bakName string, newName string, w io.Writer) (err error) }
Logger 基础的日志类型 要使用本Logger,请在其他package中复刻一个loglv/loglv.go文件
type Loglv ¶
type Loglv interface { // Print calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Print. Print(v ...any) // Printf calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Printf. Printf(format string, v ...any) // Println calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Println. Println(v ...any) // Stackf 在日志中打印任何调用Stackf的回溯调用栈 Stackf(format string, v ...any) // RecoverCtx 调用recover捕获异常,并在标题下打印异常栈,用于defer语句 // panicfunc func()以作异常最后特殊处理比如恢复数据等,set_context函数可以返回异常函数的一些变量数据以打印到栈标题处 RecoverCtx(panicfun func(err any), set_context func() string) (recover_err any) // Recoverf 调用recover捕获异常,并在标题下打印异常栈,用于defer语句 // v可以额外最后传递一个参数panicfunc func()以作异常最后特殊处理比如恢复数据等 Recoverf(format string, v ...any) (recover_err any) // Recoverln 最简单的在标题下打印异常函数,用于defer语句 Recoverln(format string, v ...any) (recover_err any) // SetOutput sets the output destination for the logger. // 现有日志文件会自动更名备份为bakName,如果bakName为空,会自动产生一时间标记的唯一文件名; // w只有在newName为""的时候才会生效 SetOutput(bakName string, newName string, w io.Writer) (err error) }
Click to show internal directories.
Click to hide internal directories.