log

log包提供对日志记录的功能

网站查询:
log使用方法
本地查询方法:

  1. # 如果官网访问不了或时间太长可以查看本地的标准库 与官网没什么区别。只不过官网更直观而已
  2. ligz@DESKTOP-911J8B9 MINGW64 /e
  3. $ go doc log
  4. package log // import "log"
  5. Package log implements a simple logging package. It defines a type, Logger,
  6. with methods for formatting output. It also has a predefined 'standard'
  7. Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and
  8. Panic[f|ln], which are easier to use than creating a Logger manually. That
  9. logger writes to standard error and prints the date and time of each logged
  10. message. Every log message is output on a separate line: if the message
  11. being printed does not end in a newline, the logger will add one. The Fatal
  12. functions call os.Exit(1) after writing the log message. The Panic functions
  13. call panic after writing the log message.
  14. const Ldate = 1 << iota ...
  15. func Fatal(v ...interface{})
  16. func Fatalf(format string, v ...interface{})
  17. func Fatalln(v ...interface{})
  18. func Flags() int
  19. func Output(calldepth int, s string) error
  20. func Panic(v ...interface{})
  21. func Panicf(format string, v ...interface{})
  22. func Panicln(v ...interface{})
  23. func Prefix() string
  24. func Print(v ...interface{})
  25. func Printf(format string, v ...interface{})
  26. func Println(v ...interface{})
  27. func SetFlags(flag int)
  28. func SetOutput(w io.Writer)
  29. func SetPrefix(prefix string)
  30. func Writer() io.Writer
  31. type Logger struct{ ... }
  32. func Default() *Logger
  33. 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: 获取日志开头
  • SetOutput:设置日志输出流

    常用结构体

  • Logger

常用函数

  • New: 用与创建logger对象

常用方法

  • 见{常用的方法} 上面👍

    实操

    learnLog.go

    ```go package learnLog

import ( “bufio” “log” “os” )

func LearnLog(){ log.Println(“Line 1”)

  1. log.SetPrefix("log:")
  2. log.Println("Line two")
  3. log.Println(log.Flags())
  4. log.SetFlags(log.Flags()| log.Lshortfile)
  5. log.Println("Line 3")
  6. file, _ := os.OpenFile("logone.log", os.O_APPEND| os.O_CREATE, 0755)
  7. defer func() {
  8. _ = file.Close()
  9. }()
  10. writer := bufio.NewWriter(file)
  11. defer func() {
  12. writer.Flush()
  13. }()
  14. log.SetOutput(writer)
  15. log.Println("Line 4")
  16. log.Println("Line five")
  17. log.Println("line six")
  18. log.Output(1, "line seven")
  19. log.Output(2, "Line 8")
  20. // logger
  21. f2, _ := os.OpenFile("l2.log", os.O_APPEND| os.O_CREATE, os.ModePerm)
  22. defer func() {
  23. _ = f2.Close()
  24. }()
  25. w2 := bufio.NewWriter(f2)
  26. defer func() {
  27. _ = w2.Flush()
  28. }()
  29. logger := log.New(w2, "log2:", log.LstdFlags| log.Lshortfile)
  30. logger.Println("Line 1")
  31. logger.Println("Line 2")
  32. logger.Println("Line3")

}

  1. <a name="lED7z"></a>
  2. ### learnLog_test.go
  3. ```go
  4. package learnLog
  5. import "testing"
  6. func TestLearnLog(t *testing.T) {
  7. LearnLog()
  8. }

结果

  1. package learnLog
  2. import "testing"
  3. func TestLearnLog(t *testing.T) {
  4. LearnLog()
  5. }
  6. # vim logone.log
  7. log:2021/08/29 20:39:58 learnLog.go:28: Line 4
  8. log:2021/08/29 20:39:58 learnLog.go:29: Line five
  9. log:2021/08/29 20:39:58 learnLog.go:30: line six
  10. log:2021/08/29 20:39:58 learnLog.go:32: line seven
  11. log:2021/08/29 20:39:58 learnLog_test.go:6: Line 8