-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
55 lines (49 loc) · 1.15 KB
/
log.go
File metadata and controls
55 lines (49 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"sync"
"time"
)
var mutex sync.Mutex
func setLog(logPath string, discard bool) {
if discard {
log.SetOutput(io.Discard)
return
}
if logPath == "" {
homePath, err := os.UserHomeDir()
if err != nil {
logPath = "/tmp/chatbash"
}
if runtime.GOOS == "darwin" {
logPath = filepath.Join(homePath, "Library/Logs/chatbash")
} else if runtime.GOOS == "linux" {
logPath = filepath.Join(homePath, ".log/chatbash")
}
}
if err := os.MkdirAll(logPath, 0755); err != nil {
log.Fatal("Failed to create log dir: " + err.Error())
}
logFile, err := os.OpenFile(filepath.Join(logPath, time.Now().Format("20060102")+".log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("Failed to open log file: " + err.Error())
}
log.SetOutput(logFile)
}
func writeLog(level string, format string, v ...any) {
mutex.Lock()
defer mutex.Unlock()
msg := fmt.Sprintf(format, v...)
log.Printf("[%s] %s", level, msg)
}
func InfoLog(format string, v ...any) {
go writeLog("info", format, v...)
}
func ErrLog(format string, v ...any) {
go writeLog("error", format, v...)
}