]> Zhao Yanbai Git Server - acecode.git/commitdiff
add logger
authorAceVest <zhaoyanbai@126.com>
Tue, 11 Dec 2018 10:50:34 +0000 (18:50 +0800)
committerAceVest <zhaoyanbai@126.com>
Tue, 11 Dec 2018 10:50:34 +0000 (18:50 +0800)
learn/go/logger.go [new file with mode: 0644]

diff --git a/learn/go/logger.go b/learn/go/logger.go
new file mode 100644 (file)
index 0000000..e623a7a
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: logger.go
+ *      Author: Zhao Yanbai
+ *              2018-12-11 16:55:07 ζ˜ŸζœŸδΊŒ CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+
+import (
+       "fmt"
+       "io"
+       "log"
+       "os"
+)
+
+var logger *log.Logger
+var logfile *os.File
+
+func init() {
+       logfile, err := os.OpenFile("./log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       logger = log.New(logfile, "LogPrefix ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
+       logger.Println("hello log file")
+
+       logger.SetOutput(io.MultiWriter(os.Stdout, logfile))
+       logger.Println("hello stdout & log file multi writer")
+
+       logger.SetOutput(io.Writer(logfile))
+       logger.Println("hello log file writer")
+}
+
+func TryLogger() {
+       logger.Println("test log")
+}
+
+func main() {
+       defer func() {
+               logfile.Sync()
+               logfile.Close()
+       }()
+
+       defer fmt.Println("Program Exited...")
+
+       TryLogger()
+
+}