Usage of flag in New constructor of log package in Golang
Package log in Golang provides simple functionality for logging. In previous post we made several examples of usage New constructor for Logger. In this post will be exposed examples of usage New and meaning of third argument of New constructor.
Let's begin with the simplest example. We will make logger that logs messages to standard output:
package main
import (
"log"
"os"
)
func main() {
logger := log.New(os.Stdout, "", log.Ldate)
logger.Println("First log message")
}
Output:
2020/04/21 First log message
Let me remind signature of New:
func New(out io.Writer, prefix string, flag int) *Logger
In example as out io.Writer we used os.Stdout - standard output. As flag int used const from log package - log.Ldate - it is integer 1 - provides the date in the local time zone: 2020/04/21. Total there are 8 such constants in log package:
-
log.Ldate - integer 1 - the date in the local time zone: 2020/04/21
logger := log.New(os.Stdout, "", log.Ldate) logger.Println("Log message") // Output: // 2020/04/21 Log message
-
log.Ltime - integer 2 - the time in the local time zone: 2020/04/21
logger := log.New(os.Stdout, "", log.Ltime) logger.Println("Log message") // Output: // 22:56:53 Log message
-
log.Lmicroseconds - integer 4 - microsecond resolution: 23:00:15.552158. Assumes Ltime
logger := log.New(os.Stdout, "", log.Lmicroseconds) logger.Println("Log message") // Output: // 23:00:15.552158 Log message
-
log.Llongfile - integer 8 - full name of source code file (with absolute path) and line number: /home/user/go/src/examples/main.go:23
logger := log.New(os.Stdout, "", log.Llongfile) logger.Println("Log message") // Output: // /home/user/go/src/examples/main.go:23 Log message
-
log.Lshortfile - integer 16 - final name of source code file and line number: main.go:23. This constant overrides Llongfile.
logger := log.New(os.Stdout, "", log.Lshortfile) logger.Println("Log message") // Output: // main.go:23 Log message
-
log.LUTC - integer 32 - this flag produces action only when combined with Ldate or Ltime - sets UTC instead of the local time zone, without these flags it produce nothing
logger := log.New(os.Stdout, "", log.Ltime | log.LUTC) logger.Println("Log message") // Output: // 18:13:15 Log message
-
log.Lmsgprefix - integer 64 - this flag moves the "prefix" (second argument of New) from the beginning of the line to before the message
logger := log.New(os.Stdout, "This is prefix: ", log.Ldate | log.Lmsgprefix) logger.Println("Log message") // Output: // 2020/04/21 This is prefix: Log message
logger := log.New(os.Stdout, "This is prefix: ", log.Ldate) logger.Println("Log message") // Output: // This is prefix: 2020/04/21 Log message
-
log.LstdFlags - integer 3 - 1 | 2 - Ldate | Ltime - union of Ldate and Ltime - initial values for the standard logger
logger := log.New(os.Stdout, "", log.LstdFlags) logger.Println("Log message") // Output: // 2020/04/21 23:21:29 Log message
As you can see flags may be combined with single or operator ( | ) - result will be combined value also:
logger := log.New(os.Stdout, "",
log.LstdFlags | log.Llongfile | log.LUTC)
logger.Println("Log message")
// Output:
// 2020/04/21 18:41:41 /home/user/go/src/examples/main.go:23 Log message
Read also:
Comments
Post a Comment