planetmint-go/util/logger.go
Jürgen Eckel 1e1e19bb19
added more sync objects to avoid accidential data races (#345)
* Added more sync objects to avoid accidental data races
* made lib.config.Config variables are private, so they cannot be tampered with (multithreading).
Please introduce Get-methods to retrieve the varialbes outside of the package if you need it.
This way, the race conditions and unexpected change of the global object state can be protected
* added reverse takedown of the validators to avoid the following error
 [app] PoP broadcast tx failed: node0.info: key not found
* moved util.TerminationWaitGroup.Wait()
to the central network cleanup method
* removed mappings for private variables


Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2024-03-11 11:11:56 +01:00

77 lines
1.7 KiB
Go

package util
import (
"fmt"
"sync"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type AppLogger struct {
testingLogger *testing.T
}
var (
globalApplicationLoggerTag string
appLogger *AppLogger
initAppLogger sync.Once
syncTestingLog sync.Mutex
)
func init() {
// Initialize the package-level variable
globalApplicationLoggerTag = "[app] "
}
func GetAppLogger() *AppLogger {
initAppLogger.Do(func() {
appLogger = &AppLogger{
testingLogger: nil,
}
})
return appLogger
}
func (logger *AppLogger) SetTestingLogger(testingLogger *testing.T) *AppLogger {
syncTestingLog.Lock()
logger.testingLogger = testingLogger
syncTestingLog.Unlock()
return logger
}
func format(msg string, keyvals ...interface{}) string {
if len(keyvals) == 0 {
return msg
}
return fmt.Sprintf(msg, keyvals...)
}
func (logger *AppLogger) testingLog(msg string, keyvals ...interface{}) {
if logger.testingLogger == nil {
return
}
msg = format(msg, keyvals...)
syncTestingLog.Lock()
logger.testingLogger.Logf(msg)
syncTestingLog.Unlock()
}
func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) {
msg = format(msg, keyvals...)
logger.testingLog(globalApplicationLoggerTag + msg)
ctx.Logger().Info(globalApplicationLoggerTag + msg)
}
func (logger *AppLogger) Debug(ctx sdk.Context, msg string, keyvals ...interface{}) {
msg = format(msg, keyvals...)
logger.testingLog(globalApplicationLoggerTag + msg)
ctx.Logger().Debug(globalApplicationLoggerTag + msg)
}
func (logger *AppLogger) Error(ctx sdk.Context, msg string, keyvals ...interface{}) {
msg = format(msg, keyvals...)
logger.testingLog(globalApplicationLoggerTag + msg)
ctx.Logger().Error(globalApplicationLoggerTag + msg)
}