github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/context.go (about)

     1  package countlog
     2  
     3  import (
     4  	"context"
     5  	"github.com/v2pro/plz/countlog/spi"
     6  	"unsafe"
     7  )
     8  
     9  func Ctx(ctx context.Context) *Context {
    10  	wrapped, isWrapped := ctx.(*Context)
    11  	if isWrapped {
    12  		return wrapped
    13  	}
    14  	return &Context{Context: ctx, logContext: &spi.LogContext{}}
    15  }
    16  
    17  type Context struct {
    18  	context.Context
    19  	logContext *spi.LogContext
    20  	suppressedMinLevel int
    21  }
    22  
    23  func (ctx *Context) SuppressLevelsBelow(level int) {
    24  	ctx.suppressedMinLevel = level
    25  }
    26  
    27  func (ctx *Context) Value(key interface{}) interface{} {
    28  	if ctx == nil {
    29  		return nil
    30  	}
    31  	if key == spi.LogContextKey {
    32  		return ctx.logContext
    33  	}
    34  	return ctx.Context.Value(key)
    35  }
    36  
    37  func (ctx *Context) Trace(event string, properties ...interface{}) {
    38  	if LevelTrace < spi.MinLevel {
    39  		return
    40  	}
    41  	ptr := unsafe.Pointer(&properties)
    42  	if LevelTrace < ctx.suppressedMinLevel {
    43  		addMemo(LevelTrace, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
    44  		return
    45  	}
    46  	log(LevelTrace, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
    47  }
    48  
    49  func (ctx *Context) TraceCall(event string, err error, properties ...interface{}) error {
    50  	if err != nil {
    51  		ptr := unsafe.Pointer(&properties)
    52  		return log(LevelWarn, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
    53  	}
    54  	if LevelTrace < spi.MinLevel {
    55  		return nil
    56  	}
    57  	ptr := unsafe.Pointer(&properties)
    58  	log(LevelTrace, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
    59  	return nil
    60  }
    61  
    62  func (ctx *Context) Debug(event string, properties ...interface{}) {
    63  	if LevelDebug < spi.MinLevel {
    64  		return
    65  	}
    66  	ptr := unsafe.Pointer(&properties)
    67  	log(LevelDebug, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
    68  }
    69  
    70  func (ctx *Context) DebugCall(event string, err error, properties ...interface{}) error {
    71  	if err != nil {
    72  		ptr := unsafe.Pointer(&properties)
    73  		return log(LevelWarn, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
    74  	}
    75  	if LevelDebug < spi.MinLevel {
    76  		return nil
    77  	}
    78  	ptr := unsafe.Pointer(&properties)
    79  	log(LevelDebug, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
    80  	return nil
    81  }
    82  
    83  func (ctx *Context) Info(event string, properties ...interface{}) {
    84  	if LevelInfo < spi.MinLevel {
    85  		return
    86  	}
    87  	ptr := unsafe.Pointer(&properties)
    88  	log(LevelInfo, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
    89  }
    90  
    91  func (ctx *Context) InfoCall(event string, err error, properties ...interface{}) error {
    92  	if err != nil {
    93  		ptr := unsafe.Pointer(&properties)
    94  		return log(LevelWarn, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
    95  	}
    96  	if LevelInfo < spi.MinLevel {
    97  		return nil
    98  	}
    99  	ptr := unsafe.Pointer(&properties)
   100  	log(LevelInfo, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
   101  	return nil
   102  }
   103  
   104  func (ctx *Context) LogAccess(event string, err error, properties ...interface{}) error {
   105  	if err != nil {
   106  		ptr := unsafe.Pointer(&properties)
   107  		return log(LevelError, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
   108  	}
   109  	if LevelInfo < spi.MinLevel {
   110  		return nil
   111  	}
   112  	ptr := unsafe.Pointer(&properties)
   113  	log(LevelInfo, event, "call", ctx, err, castEmptyInterfaces(uintptr(ptr)))
   114  	return nil
   115  }
   116  
   117  func (ctx *Context) Warn(event string, properties ...interface{}) {
   118  	ptr := unsafe.Pointer(&properties)
   119  	log(LevelWarn, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
   120  }
   121  
   122  func (ctx *Context) Error(event string, properties ...interface{}) {
   123  	ptr := unsafe.Pointer(&properties)
   124  	log(LevelError, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
   125  }
   126  
   127  func (ctx *Context) Fatal(event string, properties ...interface{}) {
   128  	ptr := unsafe.Pointer(&properties)
   129  	log(LevelFatal, event, "", ctx, nil, castEmptyInterfaces(uintptr(ptr)))
   130  }
   131  
   132  func (ctx *Context) Add(key string, value interface{}) {
   133  	ctx.logContext.Properties = append(ctx.logContext.Properties, key)
   134  	ctx.logContext.Properties = append(ctx.logContext.Properties, value)
   135  }