Skip to content

Commit

Permalink
chore: expose more logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jan 15, 2024
1 parent 3dec7d6 commit bd71eea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ func (c Context) Errorf(err error, format string, args ...interface{}) {
c.logger.Errorf(fmt.Sprintf(format, args...))
}

func (c Context) Infof(format string, args ...interface{}) {
if c.IsDebug() {
// info level logs should only be pushed for debug traces
c.GetSpan().AddEvent(fmt.Sprintf(format, args...), trace.WithAttributes(attribute.String("level", "info")))
}
c.logger.Infof(fmt.Sprintf(format, args...))
}

func (c Context) Warnf(format string, args ...interface{}) {
if c.IsDebug() {
// info level logs should only be pushed for debug traces
c.GetSpan().AddEvent(fmt.Sprintf(format, args...), trace.WithAttributes(attribute.String("level", "warn")))
}
c.logger.Warnf(fmt.Sprintf(format, args...))
}

func (c Context) Logf(level int, format string, args ...interface{}) {
if c.IsTrace() {
// info level logs should only be pushed for debug traces
c.GetSpan().AddEvent(fmt.Sprintf(format, args...), trace.WithAttributes(attribute.String("level", fmt.Sprintf("%d", level))))
}
c.logger.V(level).Infof(format, args...)
}

func (c Context) GetSpan() trace.Span {
return trace.SpanFromContext(c)
}
Expand Down
7 changes: 5 additions & 2 deletions logger/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func UseZap() {
currentLogger = newZap(level)
}

func newZap(level int) ZapLogger {
func NewZapEncoder() zapcore.Encoder {
var encoder zapcore.Encoder

capitalColorLevelEncoder := func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
Expand Down Expand Up @@ -93,9 +93,12 @@ func newZap(level int) ZapLogger {
encoder = zapcore.NewConsoleEncoder(config)

}
return encoder
}

func newZap(level int) ZapLogger {
atomicLevel := zap.NewAtomicLevelAt(zapcore.InfoLevel - zapcore.Level(level))
zapCore := zapcore.NewCore(encoder, zapcore.AddSync(os.Stderr), atomicLevel)
zapCore := zapcore.NewCore(NewZapEncoder(), zapcore.AddSync(os.Stderr), atomicLevel)
var opts []zapapi.Option
if reportCaller {
opts = append(opts, zap.AddCaller(), zap.AddCallerSkip(2))
Expand Down

0 comments on commit bd71eea

Please sign in to comment.