github.com/coveo/gotemplate@v2.7.7+incompatible/template/extra_logging.go (about)

     1  package template
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/coveo/gotemplate/utils"
    10  	"github.com/fatih/color"
    11  	logging "github.com/op/go-logging"
    12  )
    13  
    14  const (
    15  	logger         = "gotemplate"
    16  	loggerInternal = "gotemplate-int"
    17  	loggingBase    = "Logging"
    18  )
    19  
    20  var loggingFuncs = dictionary{
    21  	"critical": func(args ...interface{}) string { return logBase(Log.Critical, args...) },
    22  	"debug":    func(args ...interface{}) string { return logBase(Log.Debug, args...) },
    23  	"error":    func(args ...interface{}) string { return logBase(Log.Error, args...) },
    24  	"fatal":    func(args ...interface{}) string { return logBase(Log.Fatal, args...) },
    25  	"info":     func(args ...interface{}) string { return logBase(Log.Info, args...) },
    26  	"notice":   func(args ...interface{}) string { return logBase(Log.Notice, args...) },
    27  	"panic":    func(args ...interface{}) string { return logBase(Log.Panic, args...) },
    28  	"warning":  func(args ...interface{}) string { return logBase(Log.Warning, args...) },
    29  }
    30  
    31  var loggingFuncsAliases = aliases{
    32  	"critical": {"criticalf"},
    33  	"debug":    {"debugf"},
    34  	"error":    {"errorf"},
    35  	"fatal":    {"fatalf"},
    36  	"info":     {"infof"},
    37  	"notice":   {"noticef"},
    38  	"panic":    {"panicf"},
    39  	"warning":  {"warn", "warnf", "warningf"},
    40  }
    41  
    42  var loggingFuncsHelp = descriptions{
    43  	"critical": "Logs a message using CRITICAL as log level (0).",
    44  	"debug":    "Logs a message using DEBUG as log level (5).",
    45  	"error":    "Logs a message using ERROR as log level (1).",
    46  	"fatal":    "Equivalents to critical followed by a call to os.Exit(1).",
    47  	"info":     "Logs a message using INFO as log level (4).",
    48  	"notice":   "Logs a message using NOTICE as log level (3).",
    49  	"panic":    "Equivalents to critical followed by a call to panic.",
    50  	"warning":  "Logs a message using WARNING as log level (2).",
    51  }
    52  
    53  func (t *Template) addLoggingFuncs() {
    54  	t.AddFunctions(loggingFuncs, loggingBase, FuncOptions{
    55  		FuncHelp:    loggingFuncsHelp,
    56  		FuncAliases: loggingFuncsAliases,
    57  	})
    58  }
    59  
    60  func logBase(f func(...interface{}), args ...interface{}) string {
    61  	f(utils.FormatMessage(args...))
    62  	return ""
    63  }
    64  
    65  // Log is the logger used to log message during template processing
    66  var Log = logging.MustGetLogger(logger)
    67  
    68  // log is application logger used to follow the behaviour of the application
    69  var log = logging.MustGetLogger(loggerInternal)
    70  
    71  var loggingMutex sync.Mutex
    72  
    73  func getLogLevelInternal() logging.Level {
    74  	loggingMutex.Lock()
    75  	defer loggingMutex.Unlock()
    76  	return logging.GetLevel(loggerInternal)
    77  }
    78  
    79  // GetLogLevel returns the current logging level for gotemplate
    80  func GetLogLevel() logging.Level {
    81  	loggingMutex.Lock()
    82  	defer loggingMutex.Unlock()
    83  	return logging.GetLevel(logger)
    84  }
    85  
    86  // SetLogLevel set the logging level for gotemplate
    87  func SetLogLevel(level logging.Level) {
    88  	loggingMutex.Lock()
    89  	defer loggingMutex.Unlock()
    90  	logging.SetLevel(level, logger)
    91  }
    92  
    93  // ConfigureLogging allows configuration of the default logging level
    94  func ConfigureLogging(level, internalLevel logging.Level, simple bool) {
    95  	format := `[%{module}] %{time:2006/01/02 15:04:05.000} %{color}%{level:-8s} %{message}%{color:reset}`
    96  	if simple {
    97  		format = `[%{level}] %{message}`
    98  	}
    99  	logging.SetBackend(logging.NewBackendFormatter(logging.NewLogBackend(color.Error, "", 0), logging.MustStringFormatter(format)))
   100  	SetLogLevel(level)
   101  	logging.SetLevel(internalLevel, loggerInternal)
   102  }
   103  
   104  // InitLogging allows configuration of the default logging level
   105  func InitLogging() int {
   106  	if level, err := strconv.Atoi(utils.GetEnv(EnvDebug, "2")); err != nil {
   107  		log.Warningf("Unable to convert %s into integer: %s", EnvDebug, os.Getenv(EnvDebug))
   108  	} else {
   109  		logging.SetLevel(logging.Level(level), loggerInternal)
   110  	}
   111  	return 0
   112  }
   113  
   114  // Default package init
   115  var _ = InitLogging()
   116  
   117  // TryGetLoggingLevelFromString converts a string into a logging level
   118  func TryGetLoggingLevelFromString(level string, defaultLevel logging.Level) (logging.Level, error) {
   119  	level = strings.TrimSpace(level)
   120  	if level == "" {
   121  		return defaultLevel, nil
   122  	}
   123  
   124  	levelNum, err := strconv.Atoi(level)
   125  	if err == nil {
   126  		return logging.Level(levelNum), nil
   127  	}
   128  
   129  	return logging.LogLevel(level)
   130  }
   131  
   132  // GetLoggingLevelFromString converts a string into a logging level
   133  func GetLoggingLevelFromString(level string) logging.Level {
   134  	return must(TryGetLoggingLevelFromString(level, logging.INFO)).(logging.Level)
   135  }