Skip to content

Commit 65556e6

Browse files
committed
Cache log messages
1 parent fabe25f commit 65556e6

2 files changed

Lines changed: 102 additions & 4 deletions

File tree

logging.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 Thorsten A. Knieling
2+
* Copyright 2022-2024 Thorsten A. Knieling
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,30 +13,65 @@ package log
1313

1414
import (
1515
"fmt"
16+
"os"
1617
"strings"
1718
"time"
1819
)
1920

21+
const maxBufferEntries = 100
22+
23+
type errorLevel byte
24+
25+
const (
26+
fataL errorLevel = iota
27+
errorL
28+
infoL
29+
debugL
30+
)
31+
32+
type tempStoreElement struct {
33+
level errorLevel
34+
message string
35+
}
36+
37+
var tempStore = make([]tempStoreElement, 0)
38+
2039
type nilLogger struct {
2140
}
2241

2342
func lognil() *nilLogger {
2443
return &nilLogger{}
2544
}
2645

46+
func disableLog() {
47+
Log = lognil()
48+
}
49+
50+
func shrinkTempStore() {
51+
if len(tempStore) > maxBufferEntries {
52+
tempStore = tempStore[len(tempStore)-maxBufferEntries:]
53+
}
54+
}
55+
2756
func (*nilLogger) Debugf(format string, args ...interface{}) {
2857
}
2958

3059
func (*nilLogger) Infof(format string, args ...interface{}) {
60+
tempStore = append(tempStore, tempStoreElement{infoL, fmt.Sprintf(format, args...)})
61+
shrinkTempStore()
3162
}
3263

3364
func (*nilLogger) Errorf(format string, args ...interface{}) {
65+
tempStore = append(tempStore, tempStoreElement{errorL, fmt.Sprintf(format, args...)})
66+
shrinkTempStore()
3467
}
3568

3669
func (*nilLogger) Fatal(args ...interface{}) {
3770
}
3871

3972
func (*nilLogger) Fatalf(format string, args ...interface{}) {
73+
tempStore = append(tempStore, tempStoreElement{fataL, fmt.Sprintf(format, args...)})
74+
os.Exit(1)
4075
}
4176

4277
// Log defines the log interface to manage other Log output frameworks
@@ -52,6 +87,24 @@ type LogI interface {
5287
var Log = LogI(lognil())
5388
var debug = false
5489

90+
func InitLog(newLog LogI) {
91+
Log = newLog
92+
if len(tempStore) > 0 {
93+
for _, t := range tempStore {
94+
switch t.level {
95+
case fataL:
96+
Log.Fatalf(t.message)
97+
case errorL:
98+
Log.Errorf(t.message)
99+
case infoL:
100+
Log.Infof(t.message)
101+
default:
102+
}
103+
}
104+
}
105+
tempStore = make([]tempStoreElement, 0)
106+
}
107+
55108
func IsDebugLevel() bool {
56109
return debug
57110
}

logging_test.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 Thorsten A. Knieling
2+
* Copyright 2022-2024 Thorsten A. Knieling
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,7 +80,7 @@ func initLogLevelWithFile(fileName string, level zapcore.Level) (err error) {
8080
defer logger.Sync()
8181

8282
sugar := logger.Sugar()
83-
Log = sugar
83+
InitLog(sugar)
8484

8585
sugar.Infof("AdabasGoApi logger initialization succeeded")
8686
return nil
@@ -150,7 +150,7 @@ func TestLogrus(t *testing.T) {
150150
}
151151
log.SetOutput(f)
152152
log.Infof("Init logrus")
153-
Log = log
153+
InitLog(log)
154154
fmt.Println("Logging running")
155155

156156
flog, err := os.Open(os.TempDir() + "/" + fileName)
@@ -163,3 +163,48 @@ func TestLogrus(t *testing.T) {
163163
}
164164
assert.Regexp(t, "time=\"20..-..-..T..:..:..\" level=info msg=\"Init logrus\"\n", string(logInfo))
165165
}
166+
167+
const testResult = `info:INFO: Pre-log information
168+
error:ERROR: Pre-log information
169+
debug:DEBUG: Post-log information
170+
info:INFO: Post-log information
171+
error:ERROR: Post-log information
172+
`
173+
174+
type testLogger struct {
175+
testLog string
176+
}
177+
178+
var testLog = &testLogger{}
179+
180+
func (t *testLogger) Debugf(format string, args ...interface{}) {
181+
t.testLog += "debug:" + fmt.Sprintf(format+"\n", args...)
182+
}
183+
184+
func (t *testLogger) Infof(format string, args ...interface{}) {
185+
t.testLog += "info:" + fmt.Sprintf(format+"\n", args...)
186+
}
187+
188+
func (t *testLogger) Errorf(format string, args ...interface{}) {
189+
t.testLog += "error:" + fmt.Sprintf(format+"\n", args...)
190+
}
191+
192+
func (t *testLogger) Fatal(args ...interface{}) {
193+
}
194+
195+
func (t *testLogger) Fatalf(format string, args ...interface{}) {
196+
t.testLog += "fatal:" + fmt.Sprintf(format+"\n", args...)
197+
os.Exit(1)
198+
}
199+
200+
func TestCache(t *testing.T) {
201+
disableLog()
202+
Log.Debugf("DEBUG: Pre-log information")
203+
Log.Infof("INFO: Pre-log information")
204+
Log.Errorf("ERROR: Pre-log information")
205+
InitLog(testLog)
206+
Log.Debugf("DEBUG: Post-log information")
207+
Log.Infof("INFO: Post-log information")
208+
Log.Errorf("ERROR: Post-log information")
209+
assert.Equal(t, testResult, testLog.testLog)
210+
}

0 commit comments

Comments
 (0)