github.com/Axway/agent-sdk@v1.1.101/pkg/util/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/elastic/beats/v7/libbeat/logp"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  const (
    12  	debugSelector = "apic-agents"
    13  	traceSelector = "apic-agents-trace"
    14  )
    15  
    16  // Get returns the global logger
    17  func Get() *logrus.Logger {
    18  	return log
    19  }
    20  
    21  // GetMetricLogger returns the metric logger
    22  func GetMetricLogger() *logrus.Logger {
    23  	return metric
    24  }
    25  
    26  func GetUsageLogger() *logrus.Logger {
    27  	return usage
    28  }
    29  
    30  var networkTraceIgnoreHeaders = map[string]interface{}{
    31  	"X-Axway-Tenant-Id": true,
    32  	"Authorization":     true,
    33  }
    34  
    35  var isLogP bool
    36  var logHTTPTrace bool
    37  
    38  func init() {
    39  	networkTrace := os.Getenv("LOG_HTTP_TRACE")
    40  	logHTTPTrace = networkTrace == "true"
    41  }
    42  
    43  // SetIsLogP -
    44  func SetIsLogP() {
    45  	isLogP = true
    46  }
    47  
    48  // UnsetIsLogP -
    49  func UnsetIsLogP() {
    50  	isLogP = false
    51  }
    52  
    53  // Trace -
    54  func Trace(args ...interface{}) {
    55  	if isLogP {
    56  		// forward trace logs to logp debug with the trace selector
    57  		if log.Level == logrus.TraceLevel {
    58  			logp.Debug(traceSelector, fmt.Sprint(args...))
    59  		}
    60  	} else {
    61  		log.Trace(args...)
    62  	}
    63  }
    64  
    65  // Tracef -
    66  func Tracef(format string, args ...interface{}) {
    67  	if isLogP {
    68  		// forward trace logs to logp debug with the trace selector
    69  		if log.Level == logrus.TraceLevel {
    70  			logp.Debug(traceSelector, format, args...)
    71  		}
    72  	} else {
    73  		log.Tracef(format, args...)
    74  	}
    75  }
    76  
    77  // Error -
    78  func Error(args ...interface{}) {
    79  	if isLogP {
    80  		logp.Err(fmt.Sprint(args...))
    81  	} else {
    82  		log.Error(args...)
    83  	}
    84  }
    85  
    86  // Errorf -
    87  func Errorf(format string, args ...interface{}) {
    88  	if isLogP {
    89  		logp.Err(format, args...)
    90  	} else {
    91  		log.Errorf(format, args...)
    92  	}
    93  }
    94  
    95  // Debug -
    96  func Debug(args ...interface{}) {
    97  	if isLogP {
    98  		logp.Debug(debugSelector, fmt.Sprint(args...))
    99  	} else {
   100  		log.Debug(args...)
   101  	}
   102  }
   103  
   104  // Debugf -
   105  func Debugf(format string, args ...interface{}) {
   106  	if isLogP {
   107  		logp.Debug(debugSelector, format, args...)
   108  	} else {
   109  		log.Debugf(format, args...)
   110  	}
   111  }
   112  
   113  // Info -
   114  func Info(args ...interface{}) {
   115  	if isLogP {
   116  		logp.Info(fmt.Sprint(args...))
   117  	} else {
   118  		log.Info(args...)
   119  	}
   120  }
   121  
   122  // Infof -
   123  func Infof(format string, args ...interface{}) {
   124  	if isLogP {
   125  		logp.Info(format, args...)
   126  	} else {
   127  		log.Infof(format, args...)
   128  	}
   129  }
   130  
   131  // Warn -
   132  func Warn(args ...interface{}) {
   133  	if isLogP {
   134  		logp.Warn(fmt.Sprint(args...))
   135  	} else {
   136  		log.Warn(args...)
   137  	}
   138  }
   139  
   140  // Warnf -
   141  func Warnf(format string, args ...interface{}) {
   142  	if isLogP {
   143  		logp.Warn(format, args...)
   144  	} else {
   145  		log.Warnf(format, args...)
   146  	}
   147  }
   148  
   149  // TraceRedacted Redacted log for traces
   150  func TraceRedacted(redactedFields []string, args ...interface{}) {
   151  	Trace(ObscureArguments(redactedFields, args...))
   152  }
   153  
   154  // ErrorRedacted Redacted log for errors
   155  func ErrorRedacted(redactedFields []string, args ...interface{}) {
   156  	Error(ObscureArguments(redactedFields, args...))
   157  }
   158  
   159  // InfoRedacted Redacted log for information
   160  func InfoRedacted(redactedFields []string, args ...interface{}) {
   161  	Info(ObscureArguments(redactedFields, args...))
   162  }
   163  
   164  // DebugRedacted Redacted log for debugging
   165  func DebugRedacted(redactedFields []string, args ...interface{}) {
   166  	Debug(ObscureArguments(redactedFields, args...))
   167  }
   168  
   169  // SetLevel -
   170  func SetLevel(level logrus.Level) {
   171  	log.SetLevel(level)
   172  }
   173  
   174  // GetLevel -
   175  func GetLevel() logrus.Level {
   176  	return log.GetLevel()
   177  }
   178  
   179  // DeprecationWarningReplace - log a deprecation warning with the old and replaced usage
   180  func DeprecationWarningReplace(old string, new string) {
   181  	Warnf("%s is deprecated, please start using %s", old, new)
   182  }
   183  
   184  // DeprecationWarningDoc - log a deprecation warning with the old and replaced usage
   185  func DeprecationWarningDoc(old string, docRef string) {
   186  	Warnf("%s is deprecated, please refer to docs.axway.com regarding %s", old, docRef)
   187  }