-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
54 lines (47 loc) · 1.19 KB
/
logging.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
// Copyright (c) 2023 Joshua Rich <joshua.rich@gmail.com>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package logging
import (
"fmt"
"net/http"
"os"
_ "net/http/pprof"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
func init() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func setProfiling() {
go func() {
for i := 6060; i < 6070; i++ {
log.Debug().
Msgf("Starting profiler web interface on localhost:" + fmt.Sprint(i))
err := http.ListenAndServe("localhost:"+fmt.Sprint(i), nil)
if err != nil {
log.Debug().Err(err).
Msg("Trouble starting profiler, trying again.")
}
}
}()
}
// SetLogging sets an appropriate log level and enables profiling if requested.
func SetLogging(trace, debug, profile bool) {
switch {
case trace:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
log.Debug().Msg("Trace logging enabled.")
case debug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Debug logging enabled.")
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
if profile {
setProfiling()
}
}