planetmint-go/util/logger.go
Jürgen Eckel e99b614e4a
added connection loss handler, extended logging, mqtt msg updates (#400)
* added a bit of logging
* added update messages to mqtt (every second with a timestamp)
* added isConnectionOpen to the interface and analysis this during execution
* added connection loss handler and status check
* put some global objects into an objects to avoid inter service testing issues
* TestingLocker Mutex to RWMutex (increase performance)
* termintationMutex became a sync.RWMutex
* added logging to the mqtt mock client
* made monitor Mutex a RWMutex
* added Mutex protection to the numberOfElements varialbe
* added another Waiting block to the machine attestation methods (CI tests)
* had to adjust the test cases to the impact of that change.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2024-05-16 17:06:19 +02: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.RWMutex
)
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{}) {
syncTestingLog.RLock()
defer syncTestingLog.RUnlock()
if logger.testingLogger == nil {
return
}
msg = format(msg, keyvals...)
logger.testingLogger.Logf(msg)
}
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)
}