...

Source file src/github.com/jackc/pgx/v4/logger.go

Documentation: github.com/jackc/pgx/v4

     1  package pgx
     2  
     3  import (
     4  	"context"
     5  	"encoding/hex"
     6  	"errors"
     7  	"fmt"
     8  )
     9  
    10  // The values for log levels are chosen such that the zero value means that no
    11  // log level was specified.
    12  const (
    13  	LogLevelTrace = 6
    14  	LogLevelDebug = 5
    15  	LogLevelInfo  = 4
    16  	LogLevelWarn  = 3
    17  	LogLevelError = 2
    18  	LogLevelNone  = 1
    19  )
    20  
    21  // LogLevel represents the pgx logging level. See LogLevel* constants for
    22  // possible values.
    23  type LogLevel int
    24  
    25  func (ll LogLevel) String() string {
    26  	switch ll {
    27  	case LogLevelTrace:
    28  		return "trace"
    29  	case LogLevelDebug:
    30  		return "debug"
    31  	case LogLevelInfo:
    32  		return "info"
    33  	case LogLevelWarn:
    34  		return "warn"
    35  	case LogLevelError:
    36  		return "error"
    37  	case LogLevelNone:
    38  		return "none"
    39  	default:
    40  		return fmt.Sprintf("invalid level %d", ll)
    41  	}
    42  }
    43  
    44  // Logger is the interface used to get logging from pgx internals.
    45  type Logger interface {
    46  	// Log a message at the given level with data key/value pairs. data may be nil.
    47  	Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
    48  }
    49  
    50  // LoggerFunc is a wrapper around a function to satisfy the pgx.Logger interface
    51  type LoggerFunc func(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
    52  
    53  // Log delegates the logging request to the wrapped function
    54  func (f LoggerFunc) Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{}) {
    55  	f(ctx, level, msg, data)
    56  }
    57  
    58  // LogLevelFromString converts log level string to constant
    59  //
    60  // Valid levels:
    61  //
    62  //	trace
    63  //	debug
    64  //	info
    65  //	warn
    66  //	error
    67  //	none
    68  func LogLevelFromString(s string) (LogLevel, error) {
    69  	switch s {
    70  	case "trace":
    71  		return LogLevelTrace, nil
    72  	case "debug":
    73  		return LogLevelDebug, nil
    74  	case "info":
    75  		return LogLevelInfo, nil
    76  	case "warn":
    77  		return LogLevelWarn, nil
    78  	case "error":
    79  		return LogLevelError, nil
    80  	case "none":
    81  		return LogLevelNone, nil
    82  	default:
    83  		return 0, errors.New("invalid log level")
    84  	}
    85  }
    86  
    87  func logQueryArgs(args []interface{}) []interface{} {
    88  	logArgs := make([]interface{}, 0, len(args))
    89  
    90  	for _, a := range args {
    91  		switch v := a.(type) {
    92  		case []byte:
    93  			if len(v) < 64 {
    94  				a = hex.EncodeToString(v)
    95  			} else {
    96  				a = fmt.Sprintf("%x (truncated %d bytes)", v[:64], len(v)-64)
    97  			}
    98  		case string:
    99  			if len(v) > 64 {
   100  				a = fmt.Sprintf("%s (truncated %d bytes)", v[:64], len(v)-64)
   101  			}
   102  		}
   103  		logArgs = append(logArgs, a)
   104  	}
   105  
   106  	return logArgs
   107  }
   108  

View as plain text