feat: 日志改为同时输出到GUI窗口和log/日期.log文件
This commit is contained in:
parent
05bec7f1d0
commit
2db2f413a1
@ -4,11 +4,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -139,6 +141,47 @@ func (w *guiLogWriter) Write(p []byte) (n int, err error) {
|
|||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logFileWriter 实现 io.Writer,将日志写入 log/日期.log 文件
|
||||||
|
type logFileWriter struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
curDate string
|
||||||
|
curFile *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *logFileWriter) Write(p []byte) (n int, err error) {
|
||||||
|
w.mu.Lock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
|
date := time.Now().Format("2006-01-02")
|
||||||
|
if date != w.curDate || w.curFile == nil {
|
||||||
|
if w.curFile != nil {
|
||||||
|
w.curFile.Close()
|
||||||
|
w.curFile = nil
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll("./log", 0755); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
filename := fmt.Sprintf("./log/%s.log", date)
|
||||||
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
w.curDate = date
|
||||||
|
w.curFile = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.curFile.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initFileLog 初始化文件日志,返回 io.Writer
|
||||||
|
func initFileLog() io.Writer {
|
||||||
|
w := &logFileWriter{}
|
||||||
|
// 写入一条启动标记
|
||||||
|
now := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
w.Write([]byte(fmt.Sprintf("====== %s 日志文件初始化完成 ======\n", now)))
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
func appendText(text string) {
|
func appendText(text string) {
|
||||||
if hWndEdit == 0 {
|
if hWndEdit == 0 {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -14,8 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 日志输出到GUI窗口
|
// 日志同时输出到GUI窗口和本地文件
|
||||||
log.SetOutput(&guiLogWriter{})
|
log.SetOutput(io.MultiWriter(&guiLogWriter{}, initFileLog()))
|
||||||
|
|
||||||
log.Printf("孔网商品定价 %s 启动中...", version)
|
log.Printf("孔网商品定价 %s 启动中...", version)
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ func main() {
|
|||||||
|
|
||||||
global := config.GetGlobal()
|
global := config.GetGlobal()
|
||||||
marshal, _ := json.Marshal(global)
|
marshal, _ := json.Marshal(global)
|
||||||
fmt.Println("config:", string(marshal))
|
log.Printf("config: %s", string(marshal))
|
||||||
|
|
||||||
// 初始化数据库
|
// 初始化数据库
|
||||||
if err := database.InitDB("./data/goods_pricing.db"); err != nil {
|
if err := database.InitDB("./data/goods_pricing.db"); err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user