Documentation ¶
Index ¶
- Constants
- Variables
- func Debug(msg interface{}, keyvals ...interface{})
- func Debugf(format string, args ...interface{})
- func Error(msg interface{}, keyvals ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(msg interface{}, keyvals ...interface{})
- func Fatalf(format string, args ...interface{})
- func GetPrefix() string
- func Helper()
- func Info(msg interface{}, keyvals ...interface{})
- func Infof(format string, args ...interface{})
- func LongCallerFormatter(file string, line int, funcName string) string
- func NowUTC() time.Time
- func Print(msg interface{}, keyvals ...interface{})
- func Printf(format string, args ...interface{})
- func SetCallerFormatter(f CallerFormatter)
- func SetCallerOffset(offset int)
- func SetDefault(logger *Logger)
- func SetFormatter(f Formatter)
- func SetLevel(level Level)
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- func SetReportCaller(report bool)
- func SetReportTimestamp(report bool)
- func SetTimeFormat(format string)
- func SetTimeFunction(f TimeFunction)
- func ShortCallerFormatter(file string, line int, funcName string) string
- func StandardLog(opts ...StandardLogOptions) *log.Logger
- func Warn(msg interface{}, keyvals ...interface{})
- func Warnf(format string, args ...interface{})
- func WithContext(ctx context.Context, logger *Logger) context.Context
- type CallerFormatter
- type Formatter
- type Level
- type Logger
- func (l *Logger) Debug(msg interface{}, keyvals ...interface{})
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Error(msg interface{}, keyvals ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatal(msg interface{}, keyvals ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) GetLevel() Level
- func (l *Logger) GetPrefix() string
- func (l *Logger) Helper()
- func (l *Logger) Info(msg interface{}, keyvals ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Print(msg interface{}, keyvals ...interface{})
- func (l *Logger) Printf(format string, args ...interface{})
- func (l *Logger) SetCallerFormatter(f CallerFormatter)
- func (l *Logger) SetCallerOffset(offset int)
- func (l *Logger) SetFormatter(f Formatter)
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) SetPrefix(prefix string)
- func (l *Logger) SetReportCaller(report bool)
- func (l *Logger) SetReportTimestamp(report bool)
- func (l *Logger) SetTimeFormat(format string)
- func (l *Logger) SetTimeFunction(f TimeFunction)
- func (l *Logger) StandardLog(opts ...StandardLogOptions) *log.Logger
- func (l *Logger) Warn(msg interface{}, keyvals ...interface{})
- func (l *Logger) Warnf(format string, args ...interface{})
- func (l *Logger) With(keyvals ...interface{}) *Logger
- func (l *Logger) WithPrefix(prefix string) *Logger
- type LoggerOption
- type Options
- type StandardLogOptions
- type TimeFunction
Constants ¶
const DefaultTimeFormat = "2006/01/02 15:04:05"
DefaultTimeFormat is the default time format.
Variables ¶
var ( // TimestampKey is the key for the timestamp. TimestampKey = "ts" // MessageKey is the key for the message. MessageKey = "msg" // LevelKey is the key for the level. LevelKey = "lvl" // CallerKey is the key for the caller. CallerKey = "caller" // PrefixKey is the key for the prefix. PrefixKey = "prefix" )
var ( // TimestampStyle is the style for timestamps. TimestampStyle = lipgloss.NewStyle() // CallerStyle is the style for caller. CallerStyle = lipgloss.NewStyle().Faint(true) // PrefixStyle is the style for prefix. PrefixStyle = lipgloss.NewStyle().Bold(true).Faint(true) // MessageStyle is the style for messages. MessageStyle = lipgloss.NewStyle() // KeyStyle is the style for keys. KeyStyle = lipgloss.NewStyle().Faint(true) // ValueStyle is the style for values. ValueStyle = lipgloss.NewStyle() // SeparatorStyle is the style for separators. SeparatorStyle = lipgloss.NewStyle().Faint(true) // DebugLevel is the style for debug level. DebugLevelStyle = lipgloss.NewStyle(). SetString(strings.ToUpper(DebugLevel.String())). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "63", Dark: "63", }) // InfoLevel is the style for info level. InfoLevelStyle = lipgloss.NewStyle(). SetString(strings.ToUpper(InfoLevel.String())). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "39", Dark: "86", }) // WarnLevel is the style for warn level. WarnLevelStyle = lipgloss.NewStyle(). SetString(strings.ToUpper(WarnLevel.String())). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "208", Dark: "192", }) // ErrorLevel is the style for error level. ErrorLevelStyle = lipgloss.NewStyle(). SetString(strings.ToUpper(ErrorLevel.String())). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "203", Dark: "204", }) // FatalLevel is the style for error level. FatalLevelStyle = lipgloss.NewStyle(). SetString(strings.ToUpper(FatalLevel.String())). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "133", Dark: "134", }) // KeyStyles overrides styles for specific keys. KeyStyles = map[string]lipgloss.Style{} // ValueStyles overrides value styles for specific keys. ValueStyles = map[string]lipgloss.Style{} )
var ContextKey = contextKey{"log"}
ContextKey is the key used to store the logger in context.
var ( // ErrMissingValue is returned when a key is missing a value. ErrMissingValue = fmt.Errorf("missing value") )
Functions ¶
func Debugf ¶ added in v0.2.0
func Debugf(format string, args ...interface{})
Debugf logs a debug message with formatting.
func Errorf ¶ added in v0.2.0
func Errorf(format string, args ...interface{})
Errorf logs an error message with formatting.
func Fatal ¶
func Fatal(msg interface{}, keyvals ...interface{})
Fatal logs a fatal message and exit.
func Fatalf ¶ added in v0.2.0
func Fatalf(format string, args ...interface{})
Fatalf logs a fatal message with formatting and exit.
func Helper ¶
func Helper()
Helper marks the calling function as a helper and skips it for source location information. It's the equivalent of testing.TB.Helper().
func Infof ¶ added in v0.2.0
func Infof(format string, args ...interface{})
Infof logs an info message with formatting.
func LongCallerFormatter ¶ added in v0.2.0
LongCallerFormatter is a caller formatter that returns the full path and line number.
func NowUTC ¶
NowUTC is a convenient function that returns the current time in UTC timezone.
This is to be used as a time function. For example:
log.SetTimeFunction(log.NowUTC)
func Print ¶
func Print(msg interface{}, keyvals ...interface{})
Print logs a message with no level.
func Printf ¶ added in v0.2.0
func Printf(format string, args ...interface{})
Printf logs a message with formatting and no level.
func SetCallerFormatter ¶ added in v0.2.0
func SetCallerFormatter(f CallerFormatter)
SetCallerFormatter sets the caller formatter for the default logger.
func SetCallerOffset ¶ added in v0.2.2
func SetCallerOffset(offset int)
SetCallerOffset sets the caller offset for the default logger.
func SetDefault ¶ added in v0.2.0
func SetDefault(logger *Logger)
SetDefault sets the default global logger.
func SetFormatter ¶
func SetFormatter(f Formatter)
SetFormatter sets the formatter for the default logger.
func SetReportCaller ¶
func SetReportCaller(report bool)
SetReportCaller sets whether to report caller location for the default logger.
func SetReportTimestamp ¶
func SetReportTimestamp(report bool)
SetReportTimestamp sets whether to report timestamp for the default logger.
func SetTimeFormat ¶
func SetTimeFormat(format string)
SetTimeFormat sets the time format for the default logger.
func SetTimeFunction ¶
func SetTimeFunction(f TimeFunction)
SetTimeFunction sets the time function for the default logger.
func ShortCallerFormatter ¶ added in v0.2.0
ShortCallerFormatter is a caller formatter that returns the last 2 levels of the path and line number.
func StandardLog ¶
func StandardLog(opts ...StandardLogOptions) *log.Logger
StandardLog returns a standard logger from the default logger.
Types ¶
type CallerFormatter ¶ added in v0.2.0
CallerFormatter is the caller formatter.
type Formatter ¶
type Formatter uint8
Formatter is a formatter for log messages.
const ( // TextFormatter is a formatter that formats log messages as text. Suitable for // console output and log files. TextFormatter Formatter = iota // JSONFormatter is a formatter that formats log messages as JSON. JSONFormatter // LogfmtFormatter is a formatter that formats log messages as logfmt. LogfmtFormatter )
type Level ¶
type Level int32
Level is a logging level.
func ParseLevel ¶ added in v0.1.2
ParseLevel converts level in string to Level type. Default level is InfoLevel.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a Logger that implements Logger.
func Default ¶
func Default() *Logger
Default returns the default logger. The default logger comes with timestamp enabled.
func FromContext ¶
FromContext returns the logger from the given context. This will return the default package logger if no logger found in context.
func NewWithOptions ¶ added in v0.2.0
NewWithOptions returns a new logger using the provided options.
func With ¶
func With(keyvals ...interface{}) *Logger
With returns a new logger with the given keyvals.
func WithPrefix ¶
WithPrefix returns a new logger with the given prefix.
func (*Logger) Debug ¶
func (l *Logger) Debug(msg interface{}, keyvals ...interface{})
Debug prints a debug message.
func (*Logger) Error ¶
func (l *Logger) Error(msg interface{}, keyvals ...interface{})
Error prints an error message.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(msg interface{}, keyvals ...interface{})
Fatal prints a fatal message and exits.
func (*Logger) Helper ¶
func (l *Logger) Helper()
Helper marks the calling function as a helper and skips it for source location information. It's the equivalent of testing.TB.Helper().
func (*Logger) Info ¶
func (l *Logger) Info(msg interface{}, keyvals ...interface{})
Info prints an info message.
func (*Logger) Print ¶
func (l *Logger) Print(msg interface{}, keyvals ...interface{})
Print prints a message with no level.
func (*Logger) SetCallerFormatter ¶ added in v0.2.0
func (l *Logger) SetCallerFormatter(f CallerFormatter)
SetCallerFormatter sets the caller formatter.
func (*Logger) SetCallerOffset ¶ added in v0.2.2
SetCallerOffset sets the caller offset.
func (*Logger) SetFormatter ¶
SetFormatter sets the formatter.
func (*Logger) SetReportCaller ¶
SetReportCaller sets whether the caller location should be reported.
func (*Logger) SetReportTimestamp ¶
SetReportTimestamp sets whether the timestamp should be reported.
func (*Logger) SetTimeFormat ¶
SetTimeFormat sets the time format.
func (*Logger) SetTimeFunction ¶
func (l *Logger) SetTimeFunction(f TimeFunction)
SetTimeFunction sets the time function.
func (*Logger) StandardLog ¶
func (l *Logger) StandardLog(opts ...StandardLogOptions) *log.Logger
StandardLog returns a standard logger from Logger. The returned logger can infer log levels from message prefix. Expected prefixes are DEBUG, INFO, WARN, ERROR, and ERR.
func (*Logger) Warn ¶
func (l *Logger) Warn(msg interface{}, keyvals ...interface{})
Warn prints a warning message.
func (*Logger) WithPrefix ¶ added in v0.2.0
WithPrefix returns a new logger with the given prefix.
type Options ¶ added in v0.2.0
type Options struct { // TimeFunction is the time function for the logger. The default is time.Now. TimeFunction TimeFunction // TimeFormat is the time format for the logger. The default is "2006/01/02 15:04:05". TimeFormat string // Level is the level for the logger. The default is InfoLevel. Level Level // Prefix is the prefix for the logger. The default is no prefix. Prefix string // ReportTimestamp is whether the logger should report the timestamp. The default is false. ReportTimestamp bool // ReportCaller is whether the logger should report the caller location. The default is false. ReportCaller bool // CallerFormatter is the caller format for the logger. The default is CallerShort. CallerFormatter CallerFormatter // CallerOffset is the caller format for the logger. The default is 0. CallerOffset int // Fields is the fields for the logger. The default is no fields. Fields []interface{} // Formatter is the formatter for the logger. The default is TextFormatter. Formatter Formatter }
Options is the options for the logger.
type StandardLogOptions ¶ added in v0.2.0
type StandardLogOptions struct {
ForceLevel Level
}
StandardLogOptions can be used to configure the standard log adapter.
type TimeFunction ¶
TimeFunction is a function that returns a time.Time.