From d5022f549beb2eb0e111866468fb420f77ba4018 Mon Sep 17 00:00:00 2001 From: Tabuci Octavian Date: Tue, 9 Aug 2022 21:41:01 +0300 Subject: [PATCH] fixes --- core/helpers/filesystem/lock/flock.go | 16 ++--- .../websocket/server/server_client.go | 9 ++- core/logger/application/app.go | 29 +++++++++ core/logger/application/vars/logger.go | 4 ++ core/logger/constructor.go | 6 ++ core/logger/coreLog/logs.go | 63 +++++++++++++++++++ 6 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 core/logger/coreLog/logs.go diff --git a/core/helpers/filesystem/lock/flock.go b/core/helpers/filesystem/lock/flock.go index 9a81102..9915e26 100644 --- a/core/helpers/filesystem/lock/flock.go +++ b/core/helpers/filesystem/lock/flock.go @@ -7,7 +7,7 @@ import ( fsPath "github.com/kyaxcorp/go-core/core/helpers/filesystem/path" "github.com/kyaxcorp/go-core/core/helpers/folder" "github.com/kyaxcorp/go-core/core/helpers/hash" - "github.com/kyaxcorp/go-core/core/logger/appLog" + "github.com/kyaxcorp/go-core/core/logger/coreLog" "sync" ) @@ -33,7 +33,7 @@ func getLockPath(lockName string) (string, error) { func getLocksDir() (string, error) { var pathErr error locksPath := config.GetConfig().Application.LocksPath - appLog.Info().Str("application_locks_path", locksPath).Msg("application locks path") + coreLog.Info().Str("application_locks_path", locksPath).Msg("application locks path") locksPath, pathErr = fsPath.GenRealPath(locksPath, true) @@ -45,28 +45,28 @@ func getLocksDir() (string, error) { folder.MkDir(locksPath) } - appLog.Info().Str("locks_dir", locksPath).Msg("application generated real path") + coreLog.Info().Str("locks_dir", locksPath).Msg("application generated real path") return locksPath, nil } func FLock(lockName string, wait bool) (bool, error) { - appLog.Info(). + coreLog.Info(). Str("lock_name", lockName). Bool("wait", wait). Msg("FLock called") - defer appLog.Info().Msg("leaving...") + defer coreLog.Info().Msg("leaving...") lockNameHash := getLockName(lockName) - appLog.Info(). + coreLog.Info(). Str("lock_name_hashed", lockNameHash). Msg("lock name hashed, getting lock path") lockPath, lockPathErr := getLockPath(lockName) if lockPathErr != nil { - appLog.Error().Err(lockPathErr).Msg("failed to get lock path") + coreLog.Error().Err(lockPathErr).Msg("failed to get lock path") return false, lockPathErr } - appLog.Info().Str("lock_path", lockPath).Msg("lock path retrieved") + coreLog.Info().Str("lock_path", lockPath).Msg("lock path retrieved") // log.Println(lockPath) fileLock := flock.New(lockPath) diff --git a/core/listeners/websocket/server/server_client.go b/core/listeners/websocket/server/server_client.go index ef35d25..a277c99 100644 --- a/core/listeners/websocket/server/server_client.go +++ b/core/listeners/websocket/server/server_client.go @@ -69,9 +69,14 @@ func (c *Client) Disconnect() error { info().Msg("closing the client connection...") - c.pingTicker.Stop() + if c.pingTicker != nil { + c.pingTicker.Stop() + } + // TODO: check if channel closed c.closeWritePump <- true // Close the write pump! - c.isDisconnecting.True() + if c.isDisconnecting != nil { + c.isDisconnecting.True() + } c.server.WSRegistrationHub.unregister <- c // This is a force Close! return c.conn.Close() diff --git a/core/logger/application/app.go b/core/logger/application/app.go index 24bfdb2..742b5e1 100644 --- a/core/logger/application/app.go +++ b/core/logger/application/app.go @@ -2,14 +2,17 @@ package application import ( configEvents "github.com/kyaxcorp/go-core/core/config/events" + "github.com/kyaxcorp/go-core/core/helpers/conv" "github.com/kyaxcorp/go-core/core/logger" "github.com/kyaxcorp/go-core/core/logger/application/vars" loggerConfig "github.com/kyaxcorp/go-core/core/logger/config" loggerPaths "github.com/kyaxcorp/go-core/core/logger/paths" + "os" ) // Define variables var applicationLoggerConfig loggerConfig.Config +var coreLoggerConfig loggerConfig.Config type MainLogOptions struct { Level int @@ -35,3 +38,29 @@ func RegisterAppLogger() { CreateAppLogger(MainLogOptions{}) }) } + +func CreateCoreLogger() bool { + logLevel := os.Getenv("GO_CORE_LOG_LEVEL") + var lvl int + if logLevel == "" { + lvl = 4 + } else { + lvl = conv.StrToInt(logLevel) + } + + coreLoggerConfig, _ = loggerConfig.DefaultConfig(&loggerConfig.Config{ + IsEnabled: "yes", + Name: "core", + ModuleName: "Core", + Description: "saving all core logs...", + Level: lvl, // take from the environment + + FileIsEnabled: "no", + ConsoleIsEnabled: "yes", + }) + // This is the Application Logger, it will save all logs + vars.CoreLogger = logger.New(coreLoggerConfig) + return true +} + +var _ = CreateCoreLogger() diff --git a/core/logger/application/vars/logger.go b/core/logger/application/vars/logger.go index 3de9ffa..9dbfd2f 100644 --- a/core/logger/application/vars/logger.go +++ b/core/logger/application/vars/logger.go @@ -4,3 +4,7 @@ import "github.com/kyaxcorp/go-core/core/logger/model" // ApplicationLogger -> This is the app logger which handles all logs writing to a single file var ApplicationLogger *model.Logger + +// CoreLogger -> this is the first logger which is been created... +// it's more for debugging lib things +var CoreLogger *model.Logger diff --git a/core/logger/constructor.go b/core/logger/constructor.go index db26f6b..982bf4c 100644 --- a/core/logger/constructor.go +++ b/core/logger/constructor.go @@ -23,6 +23,12 @@ func GetAppLogger() *model.Logger { return vars.ApplicationLogger } +// GetCoreLogger -> it returns the instance which is the main logger of the app, it centralizes all the data together +func GetCoreLogger() *model.Logger { + // TODO: we can switch loggers when the app logger has started... but there should be a logic of levels... + return vars.CoreLogger +} + //func New(ctx context.Context, config config.Config) *Logger { // Here we will store writers which handle writing to file, if we don't want to create multiple handlers and have conflict, diff --git a/core/logger/coreLog/logs.go b/core/logger/coreLog/logs.go new file mode 100644 index 0000000..427f5ab --- /dev/null +++ b/core/logger/coreLog/logs.go @@ -0,0 +1,63 @@ +package coreLog + +import ( + "github.com/kyaxcorp/go-core/core/logger" + "github.com/kyaxcorp/go-core/core/logger/model" + "github.com/rs/zerolog" +) + +func getApp() *model.Logger { + return logger.GetCoreLogger() +} + +//--------------------------\\ + +func Info() *zerolog.Event { + return getApp().Info() +} + +func Warn() *zerolog.Event { + return getApp().Warn() +} + +func Error() *zerolog.Event { + return getApp().Error() +} + +func Debug() *zerolog.Event { + return getApp().Debug() +} + +func Fatal() *zerolog.Event { + return getApp().Fatal() +} + +func Panic() *zerolog.Event { + return getApp().Panic() +} + +//--------------------------\\ + +func InfoF(functionName string) *zerolog.Event { + return getApp().InfoF(functionName) +} + +func WarnF(functionName string) *zerolog.Event { + return getApp().WarnF(functionName) +} + +func ErrorF(functionName string) *zerolog.Event { + return getApp().ErrorF(functionName) +} + +func DebugF(functionName string) *zerolog.Event { + return getApp().DebugF(functionName) +} + +func FatalF(functionName string) *zerolog.Event { + return getApp().FatalF(functionName) +} + +func PanicF(functionName string) *zerolog.Event { + return getApp().PanicF(functionName) +}