Documentation ¶
Index ¶
- Constants
- Variables
- func Debug(msg interface{}, keyvals ...interface{})
- func Error(msg interface{}, keyvals ...interface{})
- func Fatal(msg interface{}, keyvals ...interface{})
- func GetPrefix() string
- func Helper()
- func Info(msg interface{}, keyvals ...interface{})
- func NowUTC() time.Time
- func Print(msg interface{}, keyvals ...interface{})
- 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 StandardLog(opts ...StandardLogOption) *log.Logger
- func Warn(msg interface{}, keyvals ...interface{})
- func WithContext(ctx context.Context, logger Logger, keyvals ...interface{}) context.Context
- type Formatter
- type Level
- type Logger
- type LoggerOption
- func WithCaller() LoggerOption
- func WithFields(keyvals ...interface{}) LoggerOption
- func WithFormatter(f Formatter) LoggerOption
- func WithLevel(level Level) LoggerOption
- func WithOutput(w io.Writer) LoggerOption
- func WithPrefix(prefix string) LoggerOption
- func WithTimeFormat(format string) LoggerOption
- func WithTimeFunction(f TimeFunction) LoggerOption
- func WithTimestamp() LoggerOption
- type StandardLogOption
- type TimeFunction
Constants ¶
const DefaultTimeFormat = "2006/01/02 15:04:05"
DefaultTimeFormat is the default time format.
Variables ¶
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("DEBUG"). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "63", Dark: "63", }) // InfoLevel is the style for info level. InfoLevelStyle = lipgloss.NewStyle(). SetString("INFO"). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "39", Dark: "86", }) // WarnLevel is the style for warn level. WarnLevelStyle = lipgloss.NewStyle(). SetString("WARN"). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "208", Dark: "192", }) // ErrorLevel is the style for error level. ErrorLevelStyle = lipgloss.NewStyle(). SetString("ERROR"). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "203", Dark: "204", }) // FatalLevel is the style for error level. FatalLevelStyle = lipgloss.NewStyle(). SetString("FATAL"). Bold(true). MaxWidth(4). Foreground(lipgloss.AdaptiveColor{ Light: "133", Dark: "134", }) // KeyStyles overrides styles for specific keys. KeyStyles = map[string]lipgloss.Style{} )
var ( // ErrMissingValue is returned when a key is missing a value. ErrMissingValue = fmt.Errorf("missing value") )
Functions ¶
func Fatal ¶
func Fatal(msg interface{}, keyvals ...interface{})
Fatal logs a fatal message 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 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 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 StandardLog ¶
func StandardLog(opts ...StandardLogOption) *log.Logger
StandardLog returns a standard logger from the default logger.
Types ¶
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 interface { // SetLevel sets the allowed level. SetLevel(level Level) // GetLevel returns the allowed level. GetLevel() Level // SetPrefix sets the logger prefix. The default is no prefix. SetPrefix(prefix string) // GetPrefix returns the logger prefix. GetPrefix() string // SetReportTimestamp sets whether the logger should report the timestamp. SetReportTimestamp(bool) // SetReportCaller sets whether the logger should report the caller location. SetReportCaller(bool) // SetTimeFunction sets the time function used to get the time. // The default is time.Now. // // To use UTC time instead of local time set the time // function to `NowUTC`. SetTimeFunction(f TimeFunction) // SetTimeFormat sets the time format. The default is "2006/01/02 15:04:05". SetTimeFormat(format string) // SetOutput sets the output destination. The default is os.Stderr. SetOutput(w io.Writer) // SetFormatter sets the formatter. The default is TextFormatter. SetFormatter(f Formatter) // Helper marks the calling function as a helper // and skips it for source location information. // It's the equivalent of testing.TB.Helper(). Helper() // With returns a new sub logger with the given key value pairs. With(keyval ...interface{}) Logger // Debug logs a debug message. Debug(msg interface{}, keyval ...interface{}) // Info logs an info message. Info(msg interface{}, keyval ...interface{}) // Warn logs a warning message. Warn(msg interface{}, keyval ...interface{}) // Error logs an error message. Error(msg interface{}, keyval ...interface{}) // Fatal logs a fatal message. Fatal(msg interface{}, keyval ...interface{}) // Print logs a message with no level. Print(msg interface{}, keyval ...interface{}) // StandardLog returns a standard logger from this logger. StandardLog(...StandardLogOption) *log.Logger }
Logger is an interface for logging.
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 New ¶
func New(opts ...LoggerOption) Logger
New returns a new logger. It uses os.Stderr as the default output.
type LoggerOption ¶
type LoggerOption = func(*logger)
LoggerOption is an option for a logger.
func WithCaller ¶
func WithCaller() LoggerOption
WithCaller returns a LoggerOption that enables caller for the logger.
func WithFields ¶
func WithFields(keyvals ...interface{}) LoggerOption
WithFields returns a LoggerOption that sets the fields for the logger.
func WithFormatter ¶
func WithFormatter(f Formatter) LoggerOption
WithFormatter returns a LoggerOption that sets the formatter for the logger.
func WithLevel ¶
func WithLevel(level Level) LoggerOption
WithLevel returns a LoggerOption that sets the level for the logger. The default is InfoLevel.
func WithOutput ¶
func WithOutput(w io.Writer) LoggerOption
WithOutput returns a LoggerOption that sets the output for the logger. The default is os.Stderr.
func WithPrefix ¶
func WithPrefix(prefix string) LoggerOption
WithPrefix returns a LoggerOption that sets the prefix for the logger.
func WithTimeFormat ¶
func WithTimeFormat(format string) LoggerOption
WithTimeFormat returns a LoggerOption that sets the time format for the logger. The default is "2006/01/02 15:04:05".
func WithTimeFunction ¶
func WithTimeFunction(f TimeFunction) LoggerOption
WithTimeFunction returns a LoggerOption that sets the time function for the logger. The default is time.Now.
func WithTimestamp ¶
func WithTimestamp() LoggerOption
WithTimestamp returns a LoggerOption that enables timestamps for the logger.
type StandardLogOption ¶
type StandardLogOption struct {
ForceLevel Level
}
StandardLogOption can be used to configure the standard log adapter.
type TimeFunction ¶
TimeFunction is a function that returns a time.Time.