github.com/graphql-editor/azure-functions-golang-worker@v0.1.0/worker/logger.go (about)

     1  package worker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/graphql-editor/azure-functions-golang-worker/rpc"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // Logger that sends logs through rpc
    11  type Logger struct {
    12  	InvocationID string
    13  	EventID      string
    14  	Cat          rpc.RpcLog_RpcLogCategory
    15  	Stream       Sender
    16  }
    17  
    18  // Trace logs current stack trace with message
    19  func (l Logger) Trace(msg string) {
    20  	st := errors.WithStack(errors.New("")).(stackTracer).StackTrace()
    21  	msg = fmt.Sprintf("%s\n%+v", msg, st[1:])
    22  	l.log(msg, rpc.RpcLog_Trace)
    23  }
    24  
    25  // Tracef logs current stack trace with message with formatted string
    26  func (l Logger) Tracef(sfmt string, args ...interface{}) {
    27  	l.Trace(fmt.Sprintf(sfmt, args...))
    28  }
    29  
    30  // Debug logs a debug level message
    31  func (l Logger) Debug(msg string) {
    32  	l.log(msg, rpc.RpcLog_Debug)
    33  }
    34  
    35  // Debugf logs current stack trace with message with formatted string
    36  func (l Logger) Debugf(sfmt string, args ...interface{}) {
    37  	l.Debug(fmt.Sprintf(sfmt, args...))
    38  }
    39  
    40  // Info logs an info level message
    41  func (l Logger) Info(msg string) {
    42  	l.log(msg, rpc.RpcLog_Information)
    43  }
    44  
    45  // Infof logs current stack trace with message with formatted string
    46  func (l Logger) Infof(sfmt string, args ...interface{}) {
    47  	l.Info(fmt.Sprintf(sfmt, args...))
    48  }
    49  
    50  // Warn logs an warning level message
    51  func (l Logger) Warn(msg string) {
    52  	l.log(msg, rpc.RpcLog_Warning)
    53  }
    54  
    55  // Warnf logs current stack trace with message with formatted string
    56  func (l Logger) Warnf(sfmt string, args ...interface{}) {
    57  	l.Warn(fmt.Sprintf(sfmt, args...))
    58  }
    59  
    60  // Error logs an error level message
    61  func (l Logger) Error(msg string) {
    62  	l.log(msg, rpc.RpcLog_Error)
    63  }
    64  
    65  // Errorf logs current stack trace with message with formatted string
    66  func (l Logger) Errorf(sfmt string, args ...interface{}) {
    67  	l.Error(fmt.Sprintf(sfmt, args...))
    68  }
    69  
    70  // Fatal logs an fatal level message
    71  func (l Logger) Fatal(msg string) {
    72  	l.log(msg, rpc.RpcLog_Critical)
    73  }
    74  
    75  // Fatalf logs current stack trace with message with formatted string
    76  func (l Logger) Fatalf(sfmt string, args ...interface{}) {
    77  	l.Fatal(fmt.Sprintf(sfmt, args...))
    78  }
    79  
    80  func (l *Logger) log(msg string, level rpc.RpcLog_Level) {
    81  	l.Stream.Send(&rpc.StreamingMessage{
    82  		Content: &rpc.StreamingMessage_RpcLog{
    83  			RpcLog: &rpc.RpcLog{
    84  				InvocationId: l.InvocationID,
    85  				EventId:      l.EventID,
    86  				Message:      msg,
    87  				Level:        level,
    88  				Category:     rpc.RpcLog_RpcLogCategory_name[int32(l.Cat)],
    89  			},
    90  		},
    91  	})
    92  }