-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.go
45 lines (40 loc) · 1.13 KB
/
log.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
37
38
39
40
41
42
43
44
45
package edrint
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func SetupLogging(l string) {
zerolog.TimeFieldFormat = "2006-01-02 15:04:05.000000"
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05.000"}
logpath := filepath.Join("files", "logs", fmt.Sprintf("%s.log", time.Now().Format("2006-01-02")))
file := createFile(logpath)
multi := zerolog.MultiLevelWriter(consoleWriter, file)
log.Logger = zerolog.New(multi).With().Timestamp().Logger()
lvl, err := zerolog.ParseLevel(l)
if err != nil {
log.Fatal().Err(err).Msg("incorrect level")
}
zerolog.SetGlobalLevel(lvl)
log.Info().Str("Level", l).Msg("Logging setup done")
}
func createDirs(filePath string) {
//Creating directories
directorystring := filepath.Dir(filePath)
err := os.MkdirAll(directorystring, os.ModePerm)
if err != nil {
log.Fatal().Err(err).Msg("cannot create directories")
}
}
func createFile(filePath string) *os.File {
//Creating file
createDirs(filePath)
file, err := os.Create(filePath)
if err != nil {
log.Fatal().Err(err).Msg("cannot create file")
}
return file
}