github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/pulsar/logger.go (about)

     1  // Copyright 2023 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package pulsar
    15  
    16  import (
    17  	"github.com/apache/pulsar-client-go/pulsar/log"
    18  	"go.uber.org/zap"
    19  )
    20  
    21  // Logger wrapper cdc logger to adapt pulsar logger
    22  type Logger struct {
    23  	zapLogger *zap.Logger
    24  }
    25  
    26  // SubLogger sub
    27  func (p *Logger) SubLogger(pulsarFields log.Fields) log.Logger {
    28  	zapFields := make([]zap.Field, 0, len(pulsarFields))
    29  	for k, v := range pulsarFields {
    30  		zapFields = append(zapFields, zap.Any(k, v))
    31  	}
    32  	return &Logger{p.zapLogger.With(zapFields...)}
    33  }
    34  
    35  // WithFields with fields
    36  func (p *Logger) WithFields(fields log.Fields) log.Entry {
    37  	return p.SubLogger(fields)
    38  }
    39  
    40  // WithField with field
    41  func (p *Logger) WithField(name string, value interface{}) log.Entry {
    42  	return &Logger{p.zapLogger.With(zap.Any(name, value))}
    43  }
    44  
    45  // WithError error
    46  func (p *Logger) WithError(err error) log.Entry {
    47  	return &Logger{p.zapLogger.With(zap.Error(err))}
    48  }
    49  
    50  // Debug debug
    51  func (p *Logger) Debug(args ...interface{}) {
    52  	p.zapLogger.Sugar().Debug(args...)
    53  }
    54  
    55  // Info info
    56  func (p *Logger) Info(args ...interface{}) {
    57  	p.zapLogger.Sugar().Info(args...)
    58  }
    59  
    60  // Warn warn
    61  func (p *Logger) Warn(args ...interface{}) {
    62  	p.zapLogger.Sugar().Warn(args...)
    63  }
    64  
    65  // Error error
    66  func (p *Logger) Error(args ...interface{}) {
    67  	p.zapLogger.Sugar().Error(args...)
    68  }
    69  
    70  // Debugf debugf
    71  func (p *Logger) Debugf(format string, args ...interface{}) {
    72  	p.zapLogger.Sugar().Debugf(format, args...)
    73  }
    74  
    75  // Infof infof
    76  func (p *Logger) Infof(format string, args ...interface{}) {
    77  	p.zapLogger.Sugar().Infof(format, args...)
    78  }
    79  
    80  // Warnf warnf
    81  func (p *Logger) Warnf(format string, args ...interface{}) {
    82  	p.zapLogger.Sugar().Warnf(format, args...)
    83  }
    84  
    85  // Errorf errorf
    86  func (p *Logger) Errorf(format string, args ...interface{}) {
    87  	p.zapLogger.Sugar().Errorf(format, args...)
    88  }
    89  
    90  // NewPulsarLogger new pulsar logger
    91  func NewPulsarLogger(base *zap.Logger) *Logger {
    92  	return &Logger{
    93  		zapLogger: base.WithOptions(zap.AddCallerSkip(1)),
    94  	}
    95  }