log
log包提供对日志记录的功能
网站查询:
log使用方法
本地查询方法:
# 如果官网访问不了或时间太长可以查看本地的标准库 与官网没什么区别。只不过官网更直观而已ligz@DESKTOP-911J8B9 MINGW64 /e$ go doc logpackage log // import "log"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], andPanic[f|ln], which are easier to use than creating a Logger manually. Thatlogger writes to standard error and prints the date and time of each loggedmessage. Every log message is output on a separate line: if the messagebeing printed does not end in a newline, the logger will add one. The Fatalfunctions call os.Exit(1) after writing the log message. The Panic functionscall panic after writing the log message.const Ldate = 1 << iota ...func Fatal(v ...interface{})func Fatalf(format string, v ...interface{})func Fatalln(v ...interface{})func Flags() intfunc Output(calldepth int, s string) errorfunc Panic(v ...interface{})func Panicf(format string, v ...interface{})func Panicln(v ...interface{})func Prefix() stringfunc Print(v ...interface{})func Printf(format string, v ...interface{})func Println(v ...interface{})func SetFlags(flag int)func SetOutput(w io.Writer)func SetPrefix(prefix string)func Writer() io.Writertype Logger struct{ ... }func Default() *Loggerfunc New(out io.Writer, prefix string, flag int) *Logger
常用的方法:
- Fatal:记录日志并退出程序
- Fatalf: 按照指定格式记录日志并退出程序
- Fatalln:记录日志并退出程序,并在日志中写入换行
- Panic: 记录日志并调用panic
- Panicf: 按照指定格式记录日志并调用panic
- Panicln: 记录日志并调用panic,并在日志中写入换行
- Print: 记录日志
- Printf: 按照指定格式记录日志
- Println: 记录日志,并在日志中换行
- Output: 记录日志栈
- SetPrefix: 设置日志前缀
- SetFlags: 设置日志开头
- Flags: 获取日志开头
-
常用结构体
Logger
常用函数
- New: 用与创建logger对象
常用方法
import ( “bufio” “log” “os” )
func LearnLog(){ log.Println(“Line 1”)
log.SetPrefix("log:")log.Println("Line two")log.Println(log.Flags())log.SetFlags(log.Flags()| log.Lshortfile)log.Println("Line 3")file, _ := os.OpenFile("logone.log", os.O_APPEND| os.O_CREATE, 0755)defer func() {_ = file.Close()}()writer := bufio.NewWriter(file)defer func() {writer.Flush()}()log.SetOutput(writer)log.Println("Line 4")log.Println("Line five")log.Println("line six")log.Output(1, "line seven")log.Output(2, "Line 8")// loggerf2, _ := os.OpenFile("l2.log", os.O_APPEND| os.O_CREATE, os.ModePerm)defer func() {_ = f2.Close()}()w2 := bufio.NewWriter(f2)defer func() {_ = w2.Flush()}()logger := log.New(w2, "log2:", log.LstdFlags| log.Lshortfile)logger.Println("Line 1")logger.Println("Line 2")logger.Println("Line3")
}
<a name="lED7z"></a>### learnLog_test.go```gopackage learnLogimport "testing"func TestLearnLog(t *testing.T) {LearnLog()}
结果
package learnLogimport "testing"func TestLearnLog(t *testing.T) {LearnLog()}# vim logone.loglog:2021/08/29 20:39:58 learnLog.go:28: Line 4log:2021/08/29 20:39:58 learnLog.go:29: Line fivelog:2021/08/29 20:39:58 learnLog.go:30: line sixlog:2021/08/29 20:39:58 learnLog.go:32: line sevenlog:2021/08/29 20:39:58 learnLog_test.go:6: Line 8
