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