package main import ( "fmt" "io" "log" "os" "path" ) func main() { flag := os.O_APPEND | os.O_CREATE | os.O_WRONLY logFile := path.Join(os.TempDir(), "mGo.log") fmt.Println(logFile) f, err := os.OpenFile(logFile, flag, 0644) // The call to os.OpenFile() creates the log file for writing, // if it does not already exist, or opens it for writing by appending // new data at the end of it. if err != nil { fmt.Println(err) return } defer f.Close() lFlags := log.Ldate | log.Lshortfile infoLogger := log.New(f, "INFO: ", lFlags) infoLogger.Println("Hello there!") infoLogger.SetFlags(log.Lshortfile | log.LstdFlags) infoLogger.Println("Another log entry!") file, err := os.OpenFile("mylog.log", flag, 0644) if err != nil { fmt.Println(err) os.Exit(0) } defer file.Close() w := io.MultiWriter(file, os.Stderr) mLogger := log.New(w, "myApp: ", log.LstdFlags) mLogger.Printf("BOOK %d", os.Getpid()) }
标签:log,err,fmt,custom,Println,file,go,os From: https://www.cnblogs.com/zhangzhihui/p/18240712