-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
181 lines (169 loc) · 4.38 KB
/
init.go
File metadata and controls
181 lines (169 loc) · 4.38 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"io"
"log/slog"
"os"
"path"
"runtime"
"runtime/pprof"
"strings"
"github.com/dihedron/overlay/metadata"
"github.com/joho/godotenv"
)
var (
cpuprof *os.File
memprof *os.File
)
// init is a function that is automatically called when the program is starting;
// it is used to set up the logging and profiling, based on the relevant environment
// variables. The environment variables are:
//
// - <binary-name>_LOG_LEVEL: the log level (debug, info, warn, error, off)
// - <binary-name>_LOG_STREAM: the log stream (stderr, stdout, file)
// - <binary-name>_CPU_PROFILE: the CPU profile file name
// - <binary-name>_MEM_PROFILE: the memory profile file name
//
// where <binary-name> is the name of the binary, in uppercase, with hyphens replaced by
// underscores. The default values are:
//
// - <binary-name>_LOG_LEVEL: "info"
// - <binary-name>_LOG_STREAM: "stderr"
// - <binary-name>_CPU_PROFILE: ""
// - <binary-name>_MEM_PROFILE: ""
// Environment variables can be loaded from a .env file by setting the
// <binary-name>_DOTENV environment variable to the path of the .env file.
func init() {
const LevelNone = slog.Level(1000)
options := &slog.HandlerOptions{
Level: LevelNone,
AddSource: true,
}
// load .env file if specified and present
if dotenv, ok := os.LookupEnv(metadata.DotEnvVarName); ok {
slog.Info("loading .env file", "path", dotenv)
if err := godotenv.Load(dotenv); err != nil {
slog.Error("error loading .env file", "error", err)
}
slog.Info("successfully loaded .env file", "path", dotenv)
}
// get log level from environment variable where, given
// the binary name "my-app", the environment variable is "MY_APP_LOG_LEVEL"
level, ok := os.LookupEnv(
fmt.Sprintf(
"%s_LOG_LEVEL",
strings.ReplaceAll(
strings.ToUpper(
path.Base(os.Args[0]),
),
"-",
"_",
),
),
)
if ok {
switch strings.ToLower(level) {
case "debug", "dbg", "d", "trace", "trc", "t":
options.Level = slog.LevelDebug
case "informational", "info", "inf", "i":
options.Level = slog.LevelInfo
case "warning", "warn", "wrn", "w":
options.Level = slog.LevelWarn
case "error", "err", "e", "fatal", "ftl", "f":
options.Level = slog.LevelError
case "off", "none", "null", "nil", "no", "n":
options.Level = LevelNone
return
}
}
// get the name of the file to log to from environment variable where, given
// the binary name "my-app", the environment variable is "MY_APP_LOG_STREAM"
var writer io.Writer = os.Stderr
stream, ok := os.LookupEnv(
fmt.Sprintf(
"%s_LOG_STREAM",
strings.ReplaceAll(
strings.ToUpper(
path.Base(os.Args[0]),
),
"-",
"_",
),
),
)
if ok {
switch strings.ToLower(stream) {
case "stderr", "error", "err", "e":
writer = os.Stderr
case "stdout", "output", "out", "o":
writer = os.Stdout
case "file":
filename := fmt.Sprintf("%s-%d.log", path.Base(os.Args[0]), os.Getpid())
var err error
writer, err = os.Create(path.Clean(filename))
if err != nil {
writer = os.Stderr
}
}
}
// initialise the logger
handler := slog.NewTextHandler(writer, options)
slog.SetDefault(slog.New(handler))
// check if CPU profiling should be enabled
filename, ok := os.LookupEnv(
fmt.Sprintf(
"%s_CPU_PROFILE",
strings.ReplaceAll(
strings.ToUpper(
path.Base(os.Args[0]),
),
"-",
"_",
),
),
)
if ok && filename != "" {
f, err := os.Create(path.Clean(filename))
if err != nil {
slog.Error("could not create CPU profile", "error", err)
}
cpuprof = f
if err := pprof.StartCPUProfile(f); err != nil {
slog.Error("could not start CPU profile", "error", err)
}
}
// check if CPU profiling should be enabled
filename, ok = os.LookupEnv(
fmt.Sprintf(
"%s_MEM_PROFILE",
strings.ReplaceAll(
strings.ToUpper(
path.Base(os.Args[0]),
),
"-",
"_",
),
),
)
if ok && filename != "" {
f, err := os.Create(path.Clean(filename))
if err != nil {
slog.Error("could not create memory profile", "error", err)
}
memprof = f
}
}
// cleanup is a function that is called when the program is exiting.
func cleanup() {
if cpuprof != nil {
defer cpuprof.Close()
defer pprof.StopCPUProfile()
}
if memprof != nil {
defer memprof.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(memprof); err != nil {
slog.Error("could not write memory profile", "error", err)
}
}
}