This repository was archived by the owner on Jan 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
62 lines (53 loc) · 1.28 KB
/
logger.go
File metadata and controls
62 lines (53 loc) · 1.28 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
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime/debug"
)
func setupLogs(sessionUuidString string) *os.File {
err := createDirectory(sessionUuidString)
if err != nil {
log.Fatalf("Error creating logs directory: %v;", err)
}
filePath := fmt.Sprintf("%s/main.log", sessionUuidString)
logFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o0666)
if err != nil {
log.Fatalf("Error creating/opening file .log: %v;", err)
}
log.SetOutput(logFile)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
return logFile
}
// createDirectory creates a logs directory in the current working directory if it does not exist.
func createDirectory(sessionUuidString string) error {
// Get current working directory
dir, err := os.Getwd()
if err != nil {
return err
}
// Forming the full path to the logs directory
logsDir := filepath.Join(dir, sessionUuidString)
// Check for directory existence
if _, err := os.Stat(logsDir); os.IsNotExist(err) {
err := os.Mkdir(logsDir, 0o0755)
if err != nil {
return err
}
} else if err != nil {
// Handling other Stat errors
return err
}
return nil
}
func logPanic() {
if r := recover(); r != nil {
log.Printf(
"PANIC: %v\nStack trace:\n%s;",
r,
string(debug.Stack()),
)
os.Exit(1)
}
}