vitess.io/vitess@v0.16.2/go/vt/log/log.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // You can modify this file to hook up a different logging library instead of glog.
    18  // If you adapt to a different logging framework, you may need to use that
    19  // framework's equivalent of *Depth() functions so the file and line number printed
    20  // point to the real caller instead of your adapter function.
    21  
    22  package log
    23  
    24  import (
    25  	"fmt"
    26  	"strconv"
    27  	"sync/atomic"
    28  
    29  	"github.com/golang/glog"
    30  	"github.com/spf13/pflag"
    31  )
    32  
    33  // Level is used with V() to test log verbosity.
    34  type Level = glog.Level
    35  
    36  var (
    37  	// V quickly checks if the logging verbosity meets a threshold.
    38  	V = glog.V
    39  
    40  	// Flush ensures any pending I/O is written.
    41  	Flush = glog.Flush
    42  
    43  	// Info formats arguments like fmt.Print.
    44  	Info = glog.Info
    45  	// Infof formats arguments like fmt.Printf.
    46  	Infof = glog.Infof
    47  	// InfoDepth formats arguments like fmt.Print and uses depth to choose which call frame to log.
    48  	InfoDepth = glog.InfoDepth
    49  
    50  	// Warning formats arguments like fmt.Print.
    51  	Warning = glog.Warning
    52  	// Warningf formats arguments like fmt.Printf.
    53  	Warningf = glog.Warningf
    54  	// WarningDepth formats arguments like fmt.Print and uses depth to choose which call frame to log.
    55  	WarningDepth = glog.WarningDepth
    56  
    57  	// Error formats arguments like fmt.Print.
    58  	Error = glog.Error
    59  	// Errorf formats arguments like fmt.Printf.
    60  	Errorf = glog.Errorf
    61  	// ErrorDepth formats arguments like fmt.Print and uses depth to choose which call frame to log.
    62  	ErrorDepth = glog.ErrorDepth
    63  
    64  	// Exit formats arguments like fmt.Print.
    65  	Exit = glog.Exit
    66  	// Exitf formats arguments like fmt.Printf.
    67  	Exitf = glog.Exitf
    68  	// ExitDepth formats arguments like fmt.Print and uses depth to choose which call frame to log.
    69  	ExitDepth = glog.ExitDepth
    70  
    71  	// Fatal formats arguments like fmt.Print.
    72  	Fatal = glog.Fatal
    73  	// Fatalf formats arguments like fmt.Printf
    74  	Fatalf = glog.Fatalf
    75  	// FatalDepth formats arguments like fmt.Print and uses depth to choose which call frame to log.
    76  	FatalDepth = glog.FatalDepth
    77  )
    78  
    79  // RegisterFlags installs log flags on the given FlagSet.
    80  //
    81  // `go/cmd/*` entrypoints should either use servenv.ParseFlags(WithArgs)? which
    82  // calls this function, or call this function directly before parsing
    83  // command-line arguments.
    84  func RegisterFlags(fs *pflag.FlagSet) {
    85  	flagVal := logRotateMaxSize{
    86  		val: fmt.Sprintf("%d", atomic.LoadUint64(&glog.MaxSize)),
    87  	}
    88  	fs.Var(&flagVal, "log_rotate_max_size", "size in bytes at which logs are rotated (glog.MaxSize)")
    89  }
    90  
    91  // logRotateMaxSize implements pflag.Value and is used to
    92  // try and provide thread-safe access to glog.MaxSize.
    93  type logRotateMaxSize struct {
    94  	val string
    95  }
    96  
    97  func (lrms *logRotateMaxSize) Set(s string) error {
    98  	maxSize, err := strconv.ParseUint(s, 10, 64)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	atomic.StoreUint64(&glog.MaxSize, maxSize)
   103  	lrms.val = s
   104  	return nil
   105  }
   106  
   107  func (lrms *logRotateMaxSize) String() string {
   108  	return lrms.val
   109  }
   110  
   111  func (lrms *logRotateMaxSize) Type() string {
   112  	return "uint64"
   113  }