diff --git a/gosnappi/common.go b/gosnappi/common.go index b8ebacb1..f070c4b3 100644 --- a/gosnappi/common.go +++ b/gosnappi/common.go @@ -11,7 +11,6 @@ import ( "time" "github.com/Masterminds/semver/v3" - "github.com/rs/zerolog" "google.golang.org/grpc" ) @@ -141,12 +140,6 @@ type api interface { deprecated(message string) under_review(message string) addWarnings(message string) - SetLogger(logger zerolog.Logger) - SetLogLevel(loglevel zerolog.Level) - // By default choice is false which outputs the log to Console. - // To set output to file assign choice to true - // and to reset back to console set choice to false - SetLogOutputToFile(choice bool) } // NewGrpcTransport sets the underlying transport of the Api as grpc @@ -203,24 +196,6 @@ func (api *apiSt) under_review(message string) { logs.Warn().Msg(message) } -func (api *apiSt) SetLogger(logger zerolog.Logger) { - SetUserLogger(logger) -} - -func (api *apiSt) SetLogOutputToFile(choice bool) { - logs = SetUserLogOutputToFile(choice) - if choice { - logs.Info().Str("Logging to", "file - openapiartlog.log").Msg("") - } else { - logs.Info().Str("Logging to", "console").Msg("") - } -} - -func (api *apiSt) SetLogLevel(logLevel zerolog.Level) { - SetUserLogLevel(logLevel) - logs.Info().Str("Log Level set to", logLevel.String()).Msg("") -} - // Returns instance of telemetry operations func (api *apiSt) Telemetry() Telemetry { return api.tracer diff --git a/gosnappi/gosnappi.go b/gosnappi/gosnappi.go index b0c5f2a9..f013adee 100644 --- a/gosnappi/gosnappi.go +++ b/gosnappi/gosnappi.go @@ -164,7 +164,7 @@ func NewApi() Api { api := gosnappiApi{} api.tracer = &telemetry{transport: "HTTP", serviceName: "go-snappi"} api.versionMeta = &versionMeta{checkVersion: false} - logs = GetLogger("otg") + logs = getLogger("otg") logs.Debug().Str("Logger Initialized", "log").Msg("") return &api } @@ -401955,14 +401955,14 @@ type BgpAttributesCommunity interface { Choice() BgpAttributesCommunityChoiceEnum // setChoice assigns BgpAttributesCommunityChoiceEnum provided by user to BgpAttributesCommunity setChoice(value BgpAttributesCommunityChoiceEnum) BgpAttributesCommunity - // getter for NoExportSubconfed to set choice. - NoExportSubconfed() - // getter for NoAdvertised to set choice. - NoAdvertised() // getter for NoLlgr to set choice. NoLlgr() + // getter for NoAdvertised to set choice. + NoAdvertised() // getter for NoExport to set choice. NoExport() + // getter for NoExportSubconfed to set choice. + NoExportSubconfed() // getter for LlgrStale to set choice. LlgrStale() // CustomCommunity returns BgpAttributesCustomCommunity, set in BgpAttributesCommunity. @@ -401999,9 +401999,9 @@ func (obj *bgpAttributesCommunity) Choice() BgpAttributesCommunityChoiceEnum { return BgpAttributesCommunityChoiceEnum(obj.obj.Choice.Enum().String()) } -// getter for NoExportSubconfed to set choice -func (obj *bgpAttributesCommunity) NoExportSubconfed() { - obj.setChoice(BgpAttributesCommunityChoice.NO_EXPORT_SUBCONFED) +// getter for NoLlgr to set choice +func (obj *bgpAttributesCommunity) NoLlgr() { + obj.setChoice(BgpAttributesCommunityChoice.NO_LLGR) } // getter for NoAdvertised to set choice @@ -402009,16 +402009,16 @@ func (obj *bgpAttributesCommunity) NoAdvertised() { obj.setChoice(BgpAttributesCommunityChoice.NO_ADVERTISED) } -// getter for NoLlgr to set choice -func (obj *bgpAttributesCommunity) NoLlgr() { - obj.setChoice(BgpAttributesCommunityChoice.NO_LLGR) -} - // getter for NoExport to set choice func (obj *bgpAttributesCommunity) NoExport() { obj.setChoice(BgpAttributesCommunityChoice.NO_EXPORT) } +// getter for NoExportSubconfed to set choice +func (obj *bgpAttributesCommunity) NoExportSubconfed() { + obj.setChoice(BgpAttributesCommunityChoice.NO_EXPORT_SUBCONFED) +} + // getter for LlgrStale to set choice func (obj *bgpAttributesCommunity) LlgrStale() { obj.setChoice(BgpAttributesCommunityChoice.LLGR_STALE) diff --git a/gosnappi/loggers.go b/gosnappi/loggers.go index 53fc071a..6b9d5d79 100644 --- a/gosnappi/loggers.go +++ b/gosnappi/loggers.go @@ -11,98 +11,102 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) -type openapiartlog struct { - RootDir *string - LogDir *string - Debug *bool - DisableStdOutLogging *bool - MaxLogSizeMB *int - MaxLogBackups *int +type logInfo struct { + rootDir *string + logDir *string + maxLogSizeMB *int + maxLogBackups *int } var ( - Openapiartlog *openapiartlog - GlobalLogger *zerolog.Logger - GlobalLogLevel zerolog.Level = zerolog.InfoLevel - LoggerFile bool = false - LogFileName string - GlobalCtx string + logSt *logInfo + Logger *zerolog.Logger + logLevel zerolog.Level = zerolog.InfoLevel + logToFile bool = false + logFileName string + ModuleName string ) -func initOpenapiartlog() error { - Openapiartlog = &openapiartlog{ - RootDir: new(string), - LogDir: new(string), - MaxLogSizeMB: new(int), - MaxLogBackups: new(int), +func initlog() error { + logSt = &logInfo{ + rootDir: new(string), + logDir: new(string), + maxLogSizeMB: new(int), + maxLogBackups: new(int), } - *Openapiartlog.MaxLogSizeMB = 25 - *Openapiartlog.MaxLogBackups = 5 - if *Openapiartlog.RootDir = os.Getenv("SRC_ROOT"); *Openapiartlog.RootDir == "" { - *Openapiartlog.RootDir = "." + *logSt.maxLogSizeMB = 25 + *logSt.maxLogBackups = 5 + if *logSt.rootDir = os.Getenv("SRC_ROOT"); *logSt.rootDir == "" { + *logSt.rootDir = "." } - *Openapiartlog.LogDir = path.Join(*Openapiartlog.RootDir, "logs") - GlobalLogger = nil - GlobalCtx = "" + *logSt.logDir = path.Join(*logSt.rootDir, "logs") + Logger = nil + ModuleName = "" return nil } -func SetUserLogger(logger zerolog.Logger) { - GlobalLogger = &logger +func SetLogger(usrDefinedLogger zerolog.Logger) { + Logger = &usrDefinedLogger } -func SetUserLogOutputToFile(choice bool) zerolog.Logger { - LoggerFile = choice - return GetLogger(GlobalCtx) +func SetLogOutputToFile(choice bool) zerolog.Logger { + logToFile = choice + return getLogger(ModuleName) } -func SetUserLogLevel(logLevel zerolog.Level) { - GlobalLogLevel = logLevel - RefreshLogLevel(GlobalLogLevel) +func SetLogLevel(level zerolog.Level) { + logLevel = level + refreshLogLevel(logLevel) } -func GetLogger(ctx string) zerolog.Logger { - if GlobalCtx == "" { - if err := initOpenapiartlog(); err != nil { +func SetLogFileName(fileName string) { + logFileName = fileName +} + +func getLogger(name string) zerolog.Logger { + if ModuleName == "" { + if err := initlog(); err != nil { panic(fmt.Errorf("Logger helper set failed: %v", err)) } } - GlobalCtx = ctx - LogFileName = ctx + ModuleName = name + if logFileName == "" { + logFileName = name + } var localLogger zerolog.Logger - if !LoggerFile { + if !logToFile { zerolog.TimestampFunc = func() time.Time { return time.Now().In(time.Local) } zerolog.TimeFieldFormat = time.RFC3339Nano consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-1-02T15:04:05.000Z"} - localLogger = zerolog.New(consoleWriter).Level(GlobalLogLevel).With().Timestamp().Str("Module", ctx).Logger() + localLogger = zerolog.New(consoleWriter).Level(logLevel).With().Timestamp().Str("Module", name).Logger() } else { if err := initFileLogger(); err != nil { panic(fmt.Errorf("Logger init failed: %v", err)) } - localLogger = log.With().Str("Module", ctx).Logger() + localLogger = log.With().Str("Module", name).Logger() } - GlobalLogger = &localLogger - return *GlobalLogger + Logger = &localLogger + return *Logger } func initLogger() (err error) { writer := lumberjack.Logger{ - Filename: path.Join(*Openapiartlog.LogDir, fmt.Sprintf("%s.log", LogFileName)), - MaxSize: *Openapiartlog.MaxLogSizeMB, - MaxBackups: *Openapiartlog.MaxLogBackups, + Filename: path.Join(*logSt.logDir, fmt.Sprintf("%s.log", logFileName)), + MaxSize: *logSt.maxLogSizeMB, + MaxBackups: *logSt.maxLogBackups, } zerolog.TimestampFieldName = "ts" zerolog.MessageFieldName = "msg" zerolog.TimeFieldFormat = time.RFC3339Nano - log.Logger = zerolog.New(&writer).Level(GlobalLogLevel).With().Timestamp().Logger() + log.Logger = zerolog.New(&writer).Level(logLevel).With().Timestamp().Logger() return err } -func RefreshLogLevel(logLevel zerolog.Level) { - if !LoggerFile { +func refreshLogLevel(logLevel zerolog.Level) { + if !logToFile { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Level(logLevel) } else { log.Logger = log.Level(logLevel) @@ -110,14 +114,14 @@ func RefreshLogLevel(logLevel zerolog.Level) { } func initFileLogger() (err error) { - if err = os.RemoveAll(*Openapiartlog.LogDir); err != nil { + if err = os.RemoveAll(*logSt.logDir); err != nil { return err } - if err = os.MkdirAll(*Openapiartlog.LogDir, os.ModePerm); err != nil { + if err = os.MkdirAll(*logSt.logDir, os.ModePerm); err != nil { return err } - if err := os.Chmod(*Openapiartlog.LogDir, 0771); err != nil { + if err := os.Chmod(*logSt.logDir, 0771); err != nil { return err } return initLogger() diff --git a/gosnappi/telemetry.go b/gosnappi/telemetry.go index 6981a600..ba30ee4f 100644 --- a/gosnappi/telemetry.go +++ b/gosnappi/telemetry.go @@ -200,9 +200,9 @@ func (t *telemetry) Start() (Telemetry, error) { func (t *telemetry) Stop() { if t.isOTLPEnabled() { if err := t.traceProvider.Shutdown(context.Background()); err != nil { - GlobalLogger.Error().Msg("Failed shutting down trace provider") + logs.Error().Msg("Failed shutting down trace provider") } - GlobalLogger.Info().Msg("Stopping tracing") + logs.Info().Msg("Stopping tracing") } } diff --git a/requirements.txt b/requirements.txt index 40ab9193..d6911888 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,11 @@ --prefer-binary -protobuf~=3.15.0 ; python_version <= '3.6' -protobuf~=4.23.3 ; python_version > '3.6' -grpcio-tools~=1.54.2 ; python_version > '3.6' -grpcio~=1.35.0 ; python_version <= '3.6' +requests grpcio~=1.54.2 ; python_version > '3.6' +grpcio~=1.35.0 ; python_version <= '3.6' +grpcio-tools~=1.54.2 ; python_version > '3.6' grpcio-tools~=1.35.0 ; python_version <= '3.6' PyYAML -requests -semantic_version +protobuf~=4.23.3 ; python_version > '3.6' +protobuf~=3.15.0 ; python_version <= '3.6' urllib3 +semantic_version