log
Go language standard library log package implements simple logging.
go
func main() {
log.Println("Log message")
log.Panicln("Panic log")
log.Fatalln("Error log")
}Fatal will call os.Exit(1) after writing the log message, while panic will throw a panic.
Prefix
go
func (l *Logger) Prefix() string // Get prefix
func (l *Logger) SetPrefix(prefix string) // Set prefixExample:
go
func main() {
log.SetPrefix("[main]")
log.Println("Log message")
log.Panicln("Panic log")
}Flag
Methods:
go
func (l *Logger) Flags() int // Access
func (l *Logger) SetFlags(flag int) // SetConstants:
go
const (
Ldate = 1 << iota // Date
Ltime // Time
Lmicroseconds // Microseconds
Llongfile // Full file name
Lshortfile // Short file name
LUTC // Timezone
Lmsgprefix // Prefix
LstdFlags = Ldate | Ltime // Initial value
)Example:
go
func main() {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Lmsgprefix | log.Ldate | log.Ltime)
log.Println("Log message")
log.Panicln("Panic log")
}Of course, you can also use log.SetOutput(w io.Writer) to set the log output path, or create your own instance through the New method.
go
func New(out io.Writer, prefix string, flag int) *LoggerIn short, the standard library's log package does not provide complete functionality. We usually use more complete third-party logging packages, such as zap, etc.
