log
log包提供对日志记录的功能
网站查询:
log使用方法
本地查询方法:
# 如果官网访问不了或时间太长可以查看本地的标准库 与官网没什么区别。只不过官网更直观而已
ligz@DESKTOP-911J8B9 MINGW64 /e
$ go doc log
package 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], 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. Every log message is output on a separate line: if the message
being printed does not end in a newline, the logger will add one. The Fatal
functions call os.Exit(1) after writing the log message. The Panic functions
call 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() int
func Output(calldepth int, s string) error
func Panic(v ...interface{})
func Panicf(format string, v ...interface{})
func Panicln(v ...interface{})
func Prefix() string
func 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.Writer
type Logger struct{ ... }
func Default() *Logger
func 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")
// logger
f2, _ := 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
```go
package learnLog
import "testing"
func TestLearnLog(t *testing.T) {
LearnLog()
}
结果
package learnLog
import "testing"
func TestLearnLog(t *testing.T) {
LearnLog()
}
# vim logone.log
log:2021/08/29 20:39:58 learnLog.go:28: Line 4
log:2021/08/29 20:39:58 learnLog.go:29: Line five
log:2021/08/29 20:39:58 learnLog.go:30: line six
log:2021/08/29 20:39:58 learnLog.go:32: line seven
log:2021/08/29 20:39:58 learnLog_test.go:6: Line 8