on main help sign in
feat(log): add atomic check for initialization
f884ff1Ayman Bagabas
internal/log/log.go+10−1
@@ -6,12 +6,16 @@ import (
6 "os"
6 "os"
7 "runtime/debug"
7 "runtime/debug"
8 "sync"
8 "sync"
9+ "sync/atomic"
9 "time"
10 "time"
10
11
11 "gopkg.in/natefinch/lumberjack.v2"
12 "gopkg.in/natefinch/lumberjack.v2"
12)
13)
13
14
15+var (
14−var initOnce sync.Once
16+ initOnce sync.Once
17+ initialized atomic.Bool
18+)
15
19
16func Init(logFile string, debug bool) {
20func Init(logFile string, debug bool) {
17 initOnce.Do(func() {
21 initOnce.Do(func() {
@@ -34,9 +38,14 @@ func Init(logFile string, debug bool) {
34 })
38 })
35
39
36 slog.SetDefault(slog.New(logger))
40 slog.SetDefault(slog.New(logger))
41+ initialized.Store(true)
37 })
42 })
38}
43}
39
44
45+func IsInitialized() bool {
46+ return initialized.Load()
47+}
48+
40func RecoverPanic(name string, cleanup func()) {
49func RecoverPanic(name string, cleanup func()) {
41 if r := recover(); r != nil {
50 if r := recover(); r != nil {
42 // Create a timestamped panic log file
51 // Create a timestamped panic log file