github.com/tickstep/library-go@v0.1.1/logger/verbose.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  )
     8  
     9  var (
    10  	// IsVerbose 是否调试
    11  	IsVerbose = false
    12  
    13  	// Outputs 输出
    14  	Outputs = []io.Writer{os.Stderr}
    15  )
    16  
    17  // CmdVerbose 调试
    18  type CmdVerbose struct {
    19  	Module string
    20  }
    21  
    22  func New(module, envVerbose string) *CmdVerbose {
    23  	IsVerbose = os.Getenv(envVerbose) == "1"
    24  	return &CmdVerbose{
    25  		Module: module,
    26  	}
    27  }
    28  
    29  // Info 提示
    30  func (pv *CmdVerbose) Info(l string) {
    31  	Verbosef("DEBUG: %s INFO: %s\n", pv.Module, l)
    32  }
    33  
    34  // Infof 提示, 格式输出
    35  func (pv *CmdVerbose) Infof(format string, a ...interface{}) {
    36  	Verbosef("DEBUG: %s INFO: %s", pv.Module, fmt.Sprintf(format, a...))
    37  }
    38  
    39  // Warn 警告
    40  func (pv *CmdVerbose) Warn(l string) {
    41  	Verbosef("DEBUG: %s WARN: %s\n", pv.Module, l)
    42  }
    43  
    44  // Warnf 警告, 格式输出
    45  func (pv *CmdVerbose) Warnf(format string, a ...interface{}) {
    46  	Verbosef("DEBUG: %s WARN: %s", pv.Module, fmt.Sprintf(format, a...))
    47  }
    48  
    49  // Verbosef 调试格式输出
    50  func Verbosef(format string, a ...interface{}) (n int, err error) {
    51  	if IsVerbose {
    52  		for _, Output := range Outputs {
    53  			n1, err := fmt.Fprintf(Output, TimePrefix()+" "+format, a...)
    54  			n += n1
    55  			if err != nil {
    56  				return n, err
    57  			}
    58  		}
    59  	}
    60  	return
    61  }
    62  
    63  // Verboseln 调试输出一行
    64  func Verboseln(a ...interface{}) (n int, err error) {
    65  	if IsVerbose {
    66  		for _, Output := range Outputs {
    67  			n1, err := fmt.Fprint(Output, TimePrefix()+" ")
    68  			n += n1
    69  			if err != nil {
    70  				return n, err
    71  			}
    72  			n2, err := fmt.Fprintln(Output, a...)
    73  			n += n2
    74  			if err != nil {
    75  				return n, err
    76  			}
    77  		}
    78  	}
    79  	return
    80  }