-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
83 lines (69 loc) · 2.01 KB
/
init.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"prox/envs"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
type gooseLogger struct {
l *zerolog.Logger
}
func (l *gooseLogger) Fatalf(format string, v ...interface{}) {
l.l.Fatal().Msgf(strings.ReplaceAll(format, "\n", ""), v...)
}
func (l *gooseLogger) Printf(format string, v ...interface{}) {
l.l.Info().Msgf(strings.ReplaceAll(format, "\n", ""), v...)
}
func printVersion() error {
bi, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("ReadBuildInfo can't read")
}
fmt.Printf("%s %s (commit/%s, %s, %s/%s)", envs.AppName, envs.Version, envs.BuildTime, bi.GoVersion, runtime.GOOS, runtime.GOARCH)
return nil
}
func initLogging() error {
logLevel := os.Getenv("LOG_LEVEL")
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.SetGlobalLevel(zerolog.DebugLevel)
if logLevel == "info" {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else if logLevel == "trace" {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}
if !envs.IsDev && args.DaemonService {
var err error
execFilename, err := os.Executable()
if err != nil {
log.Fatal().Err(err)
}
baseFilename := strings.ReplaceAll(filepath.Base(execFilename), filepath.Ext(execFilename), "")
dirname := filepath.Dir(execFilename)
logPath := path.Join(dirname, fmt.Sprintf("%s.%s", baseFilename, logExt))
logFile, err = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal().Err(err)
}
log.Logger = log.Output(logFile)
log.Info().Msgf("%s starting...", envs.AppName)
} else {
timeFormat := time.DateTime
if envs.IsDev {
timeFormat = time.TimeOnly
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: timeFormat})
log.Info().Msgf("%s is Development mode.", envs.AppName)
}
log.Info().Msgf("os: %s arch: %s", runtime.GOOS, runtime.GOARCH)
return nil
}