Skip to content

Commit b2d8db5

Browse files
committed
恢复
1 parent 24d395c commit b2d8db5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

cmd/logs/LogToFile.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package logs
2+
3+
import (
4+
"github.com/SummerSec/SpringExploit/cmd/commons/utils"
5+
log "github.com/sirupsen/logrus"
6+
"io"
7+
"os"
8+
"path"
9+
"time"
10+
)
11+
12+
func SaveLogs(logfile string) {
13+
if logfile != "" {
14+
15+
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
16+
if err == nil {
17+
mw := io.MultiWriter(os.Stdout, f)
18+
log.SetOutput(mw)
19+
log.SetFormatter(&log.TextFormatter{
20+
ForceColors: true,
21+
FullTimestamp: true,
22+
//TimestampFormat: "2006-01-02 15:04:05",
23+
})
24+
log.SetReportCaller(true)
25+
log.Debugln("Saving logs to file " + logfile)
26+
} else {
27+
log.Info("Failed to log to file, using default stderr")
28+
}
29+
30+
} else {
31+
logfile := "logs/log-" + time.Now().Format("202204221616") + ".txt"
32+
33+
err := utils.Mkdir(path.Dir(logfile))
34+
if err != nil {
35+
log.Debugln("Failed to create logs directory")
36+
return
37+
}
38+
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
39+
if err == nil {
40+
mw := io.MultiWriter(os.Stdout, f)
41+
42+
log.SetOutput(mw)
43+
format := log.TextFormatter{
44+
ForceColors: true,
45+
FullTimestamp: true,
46+
//TimestampFormat: "2006-01-02 15:04:05",
47+
}
48+
49+
log.SetFormatter(&format)
50+
log.SetReportCaller(true)
51+
log.Debugln("Saving logs to file " + logfile)
52+
} else {
53+
log.Info("Failed to log to file, using default stderr")
54+
}
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)