Skip to content
Snippets Groups Projects
Select Git revision
  • 7d54e7f76c62ebcf8687274b73439adaac141a63
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

config.go

Blame
  • config.go 2.28 KiB
    package logs
    
    import (
    	"context"
    	"time"
    
    	"gitlab.com/uafrica/go-utils/config"
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/redis"
    )
    
    type Config struct {
    	ActionsKeep                 bool  `json:"actions_keep" doc:"Set true to keep list of actions in logs"`
    	ActionsMaxSQLLength         int64 `json:"actions_max_sql_length" doc:"Set length of SQL query to keep in action list (default 0 = delete)"`
    	ActionsMaxSQSReqBodyLength  int64 `json:"actions_max_sqs_req_body_length" doc:"Set length of SQS Request body to keep in action list (default 0 = delete)"`
    	ActionsMaxAPIReqBodyLength  int64 `json:"actions_max_api_req_body_length" doc:"Set length of API Request body to keep in action list (default 0 = delete)"`
    	ActionsMaxAPIResBodyLength  int64 `json:"actions_max_api_res_body_length" doc:"Set length of API Response body to keep in action list (default 0 = delete)"`
    	ActionsMaxSearchQueryLength int64 `json:"actions_max_search_query_length" doc:"Set length of search query to keep in action list (default 0 = delete)"`
    }
    
    const configPrefix = "LOGS"
    
    var (
    	logConfig         Config
    	dynamicLogConfig  Config
    	dynamicExpireTime time.Time
    	redisCli          redis.IRedis
    )
    
    func init() {
    	if err := config.LoadEnv(configPrefix, &logConfig); err != nil {
    		logger.Errorf("failed to load LOGS config: %+v", err)
    	}
    	dynamicLogConfig = logConfig
    	dynamicExpireTime = time.Now()
    
    	//see if can load overrides from redis
    	var err error
    	redisCli, err = redis.New(context.Background())
    	if err != nil {
    		logger.Errorf("Not able to connect to REDIS for runtime %s config: %+v", configPrefix, err)
    	}
    }
    
    //todo: call only on each use and check expiry time before reading from REDIS again, e.g. reload no faster that 10s
    func currentLogConfig() Config {
    	if redisCli == nil || dynamicExpireTime.After(time.Now()) {
    		return dynamicLogConfig
    	}
    
    	//time to attempt reload
    	//copy static config then overload values which are defined from REDIS
    	dynamicLogConfig = logConfig
    	dynamicExpireTime = time.Now().Add(time.Second * 10)
    
    	if err := config.Load(configPrefix, &dynamicLogConfig, redisCli); err != nil {
    		logger.Errorf("failed to load %s config from REDIS", configPrefix)
    	} else {
    		logger.Debugf("Loaded %s config: %+v", configPrefix, dynamicLogConfig)
    	}
    	return dynamicLogConfig
    } //runtimeConfigLoad