github.com/sentienttechnologies/studio-go-runner@v0.0.0-20201118202441-6d21f2ced8ee/pkg/studio/log.go (about)

     1  // Copyright 2018-2020 (c) Cognizant Digital Business, Evolutionary AI. Issued under the Apache 2.0 License.
     2  
     3  package studio
     4  
     5  // This file contains the implementation of a logger that adorns the logxi package with
     6  // some common information not by default supplied by the generic code
     7  
     8  import (
     9  	"os"
    10  	"sync"
    11  
    12  	logxi "github.com/karlmutch/logxi/v1"
    13  )
    14  
    15  var (
    16  	hostName string
    17  )
    18  
    19  func init() {
    20  	hostName, _ = os.Hostname()
    21  }
    22  
    23  // Logger encapsulates the logging device that is used to emit logs and
    24  // as a receiver that has the logging methods
    25  //
    26  type Logger struct {
    27  	log logxi.Logger
    28  	sync.Mutex
    29  }
    30  
    31  // NewLogger can be used to instantiate a wrapper logger with a module label
    32  //
    33  func NewLogger(component string) (log *Logger) {
    34  	logxi.DisableCallstack()
    35  
    36  	return &Logger{
    37  		log: logxi.New(component),
    38  	}
    39  }
    40  
    41  // Trace is a method for output of trace level messages
    42  // with a varargs style list of parameters that is formatted
    43  // as label and then the value in a single list
    44  //
    45  func (l *Logger) Trace(msg string, args ...interface{}) {
    46  	allArgs := append([]interface{}{}, args...)
    47  	allArgs = append(allArgs, "host")
    48  	allArgs = append(allArgs, hostName)
    49  
    50  	l.Lock()
    51  	defer l.Unlock()
    52  	l.log.Trace(msg, allArgs)
    53  }
    54  
    55  // Debug is a method for output of debugging level messages
    56  // with a varargs style list of parameters that is formatted
    57  // as label and then the value in a single list
    58  //
    59  func (l *Logger) Debug(msg string, args ...interface{}) {
    60  	allArgs := append([]interface{}{}, args...)
    61  	allArgs = append(allArgs, "host")
    62  	allArgs = append(allArgs, hostName)
    63  
    64  	l.Lock()
    65  	defer l.Unlock()
    66  	l.log.Debug(msg, allArgs)
    67  }
    68  
    69  // Info is a method for output of informational level messages
    70  // with a varargs style list of parameters that is formatted
    71  // as label and then the value in a single list
    72  //
    73  func (l *Logger) Info(msg string, args ...interface{}) {
    74  	allArgs := append([]interface{}{}, args...)
    75  	allArgs = append(allArgs, "host")
    76  	allArgs = append(allArgs, hostName)
    77  
    78  	l.Lock()
    79  	defer l.Unlock()
    80  	l.log.Info(msg, allArgs)
    81  }
    82  
    83  // Warn is a method for output of warning level messages
    84  // with a varargs style list of parameters that is formatted
    85  // as label and then the value in a single list
    86  //
    87  func (l *Logger) Warn(msg string, args ...interface{}) error {
    88  	allArgs := append([]interface{}{}, args...)
    89  	allArgs = append(allArgs, "host")
    90  	allArgs = append(allArgs, hostName)
    91  
    92  	l.Lock()
    93  	defer l.Unlock()
    94  	return l.log.Warn(msg, allArgs)
    95  }
    96  
    97  // Error is a method for output of error level messages
    98  // with a varargs style list of parameters that is formatted
    99  // as label and then the value in a single list
   100  //
   101  func (l *Logger) Error(msg string, args ...interface{}) error {
   102  	allArgs := append([]interface{}{}, args...)
   103  	allArgs = append(allArgs, "host")
   104  	allArgs = append(allArgs, hostName)
   105  
   106  	l.Lock()
   107  	defer l.Unlock()
   108  	return l.log.Error(msg, allArgs)
   109  }
   110  
   111  // Fatal is a method for output of fatal level messages
   112  // with a varargs style list of parameters that is formatted
   113  // as label and then the value in a single list
   114  //
   115  func (l *Logger) Fatal(msg string, args ...interface{}) {
   116  	allArgs := append([]interface{}{}, args...)
   117  	allArgs = append(allArgs, "host")
   118  	allArgs = append(allArgs, hostName)
   119  
   120  	l.Lock()
   121  	defer l.Unlock()
   122  	l.log.Fatal(msg, allArgs)
   123  }
   124  
   125  // Log is a method for output of parameterized level messages
   126  // with a varargs style list of parameters that is formatted
   127  // as label and then the value in a single list
   128  //
   129  func (l *Logger) Log(level int, msg string, args []interface{}) {
   130  	allArgs := append([]interface{}{}, args...)
   131  	allArgs = append(allArgs, "host")
   132  	allArgs = append(allArgs, hostName)
   133  
   134  	l.Lock()
   135  	defer l.Unlock()
   136  	l.log.Log(level, msg, allArgs)
   137  }
   138  
   139  // SetLevel can be used to set the threshold for the level of messages
   140  // that will be output by the logger
   141  //
   142  func (l *Logger) SetLevel(lvl int) {
   143  	l.Lock()
   144  	defer l.Unlock()
   145  	l.log.SetLevel(lvl)
   146  }
   147  
   148  // IsTrace returns true in the event that the theshold logging level
   149  // allows for trace messages to appear in the output
   150  //
   151  func (l *Logger) IsTrace() bool {
   152  	l.Lock()
   153  	defer l.Unlock()
   154  	return l.log.IsTrace()
   155  }
   156  
   157  // IsDebug returns true in the event that the theshold logging level
   158  // allows for debugging messages to appear in the output
   159  //
   160  func (l *Logger) IsDebug() bool {
   161  	l.Lock()
   162  	defer l.Unlock()
   163  	return l.log.IsDebug()
   164  }
   165  
   166  // IsInfo returns true in the event that the theshold logging level
   167  // allows for informational messages to appear in the output
   168  //
   169  func (l *Logger) IsInfo() bool {
   170  	l.Lock()
   171  	defer l.Unlock()
   172  	return l.log.IsInfo()
   173  }
   174  
   175  // IsWarn returns true in the event that the theshold logging level
   176  // allows for warning messages to appear in the output
   177  //
   178  func (l *Logger) IsWarn() bool {
   179  	l.Lock()
   180  	defer l.Unlock()
   181  	return l.log.IsWarn()
   182  }