github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/log/redact.go (about)

     1  // Copyright 2020 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 log
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/errors"
    20  	"go.uber.org/zap"
    21  	"go.uber.org/zap/zapcore"
    22  )
    23  
    24  // InitRedact inits the enableRedactLog
    25  func InitRedact(redactLog bool) {
    26  	errors.RedactLogEnabled.Store(redactLog)
    27  }
    28  
    29  // NeedRedact returns whether to redact log
    30  func NeedRedact() bool {
    31  	return errors.RedactLogEnabled.Load()
    32  }
    33  
    34  // ZapRedactBinary receives zap.Binary and return omitted information if redact log enabled
    35  func ZapRedactBinary(key string, val []byte) zapcore.Field {
    36  	if NeedRedact() {
    37  		return zap.String(key, "?")
    38  	}
    39  	return zap.Binary(key, val)
    40  }
    41  
    42  // ZapRedactArray receives zap.Array and return omitted information if redact log enabled
    43  func ZapRedactArray(key string, val zapcore.ArrayMarshaler) zapcore.Field {
    44  	if NeedRedact() {
    45  		return zap.String(key, "?")
    46  	}
    47  	return zap.Array(key, val)
    48  }
    49  
    50  // ZapRedactReflect receives zap.Reflect and return omitted information if redact log enabled
    51  func ZapRedactReflect(key string, val interface{}) zapcore.Field {
    52  	if NeedRedact() {
    53  		return zap.String(key, "?")
    54  	}
    55  	return zap.Reflect(key, val)
    56  }
    57  
    58  // ZapRedactStringer receives stringer argument and return omitted information in zap.Field  if redact log enabled
    59  func ZapRedactStringer(key string, arg fmt.Stringer) zap.Field {
    60  	return zap.Stringer(key, RedactStringer(arg))
    61  }
    62  
    63  // ZapRedactString receives stringer argument and return omitted information in zap.Field  if redact log enabled
    64  func ZapRedactString(key string, arg string) zap.Field {
    65  	return zap.String(key, RedactString(arg))
    66  }
    67  
    68  // RedactString receives string argument and return omitted information if redact log enabled
    69  func RedactString(arg string) string {
    70  	if NeedRedact() {
    71  		return "?"
    72  	}
    73  	return arg
    74  }
    75  
    76  // RedactStringer receives stringer argument and return omitted information if redact log enabled
    77  func RedactStringer(arg fmt.Stringer) fmt.Stringer {
    78  	if NeedRedact() {
    79  		return stringer{}
    80  	}
    81  	return arg
    82  }
    83  
    84  type stringer struct{}
    85  
    86  // String implement fmt.Stringer
    87  func (s stringer) String() string {
    88  	return "?"
    89  }