github.com/rjgonzale/pop/v5@v5.1.3-dev/logger.go (about)

     1  package pop
     2  
     3  import (
     4  	"fmt"
     5  	stdlog "log"
     6  	"os"
     7  
     8  	"github.com/fatih/color"
     9  	"github.com/gobuffalo/pop/v5/logging"
    10  )
    11  
    12  type logger func(lvl logging.Level, s string, args ...interface{})
    13  
    14  // Debug mode, to toggle verbose log traces
    15  var Debug = false
    16  
    17  // Color mode, to toggle colored logs
    18  var Color = true
    19  
    20  var log logger
    21  
    22  var defaultStdLogger = stdlog.New(os.Stdout, "[POP] ", stdlog.LstdFlags)
    23  var defaultLogger = func(lvl logging.Level, s string, args ...interface{}) {
    24  	if !Debug && lvl <= logging.Debug {
    25  		return
    26  	}
    27  	if lvl == logging.SQL {
    28  		if len(args) > 0 {
    29  			xargs := make([]string, len(args))
    30  			for i, a := range args {
    31  				switch a.(type) {
    32  				case string:
    33  					xargs[i] = fmt.Sprintf("%q", a)
    34  				default:
    35  					xargs[i] = fmt.Sprintf("%v", a)
    36  				}
    37  			}
    38  			s = fmt.Sprintf("%s - %s | %s", lvl, s, xargs)
    39  		} else {
    40  			s = fmt.Sprintf("%s - %s", lvl, s)
    41  		}
    42  	} else {
    43  		s = fmt.Sprintf(s, args...)
    44  		s = fmt.Sprintf("%s - %s", lvl, s)
    45  	}
    46  	if Color {
    47  		s = color.YellowString(s)
    48  	}
    49  	defaultStdLogger.Println(s)
    50  }
    51  
    52  // SetLogger overrides the default logger.
    53  //
    54  // The logger must implement the following interface:
    55  // type logger func(lvl logging.Level, s string, args ...interface{})
    56  func SetLogger(l logger) {
    57  	log = l
    58  }