forked from sadlil/gologger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
36 lines (32 loc) · 936 Bytes
/
file.go
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
package gologger
import (
"fmt"
"os"
"path"
"time"
)
func FilePrinter(log LogInstance, packageName string, fileName string, lineNumber int, funcName string, time time.Time) {
logFileName := log.LoggerInit.Location
if logFileName == "" {
logFileName = "log.txt"
}
basePath := path.Dir(logFileName)
filePath := path.Base(logFileName)
if os.MkdirAll(basePath, 0777) != nil {
fmt.Println("Unable to create directory")
os.Exit(1)
}
logFileName = path.Join(basePath, filePath)
file, err := os.OpenFile(logFileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
defer file.Close()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
logString := fmt.Sprintf("[%s] [%s] [%s::%s::%s] [%d] %s\n", log.LogType, time.Format("2006-01-02 15:04:05"), packageName, fileName, funcName, lineNumber, log.Message)
_, fileWriteErr := file.WriteString(logString)
if fileWriteErr != nil {
fmt.Println(fileWriteErr)
os.Exit(1)
}
}