Documentation ¶
Overview ¶
Package log provides a log interface
Index ¶
- Constants
- func Debug(args ...interface{})
- func Debugf(template string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(template string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(template string, args ...interface{})
- func Info(args ...interface{})
- func Infof(template string, args ...interface{})
- func Init(opts ...Option) error
- func Log(level Level, v ...interface{})
- func Logf(level Level, format string, v ...interface{})
- func NewContext(ctx context.Context, l Logger) context.Context
- func NewContextWithKey(ctx context.Context, key any, l Logger) context.Context
- func String() string
- func Trace(args ...interface{})
- func Tracef(template string, args ...interface{})
- func V(lvl Level, log Logger) bool
- func Warn(args ...interface{})
- func Warnf(template string, args ...interface{})
- type Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Free()
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WritePosInt(i int)
- func (b *Buffer) WritePosIntWidth(i, width int)
- func (b *Buffer) WriteString(s string)
- type Format
- type Handler
- type HandlerOptions
- type Helper
- func (h *Helper) Debug(args ...interface{})
- func (h *Helper) Debugf(template string, args ...interface{})
- func (h *Helper) Error(args ...interface{})
- func (h *Helper) Errorf(template string, args ...interface{})
- func (h *Helper) Fatal(args ...interface{})
- func (h *Helper) Fatalf(template string, args ...interface{})
- func (h *Helper) Info(args ...interface{})
- func (h *Helper) Infof(template string, args ...interface{})
- func (h *Helper) Trace(args ...interface{})
- func (h *Helper) Tracef(template string, args ...interface{})
- func (h *Helper) Warn(args ...interface{})
- func (h *Helper) Warnf(template string, args ...interface{})
- func (h *Helper) WithError(err error) *Helper
- func (h *Helper) WithFields(fields map[string]interface{}) *Helper
- type Level
- type Logger
- type Option
- type Options
Constants ¶
const ( // TimeKey is the key used by the built-in handlers for the time // when the log method is called. The associated Value is a [time.Time]. TimeKey = "ts" // PidKey is the key used by the built-in handlers for the pid // when the log level is lower than InfoLevel. PidKey = "pid" // MessageKey is the key used by the built-in handlers for the // message of the log call. The associated value is a string. MessageKey = "msg" // NanoTimeFieldFormat indicates the format of timestamp decoded // from a float value (time in seconds and nanoseconds). NanoTimeFieldFormat = "2006-01-02 15:04:05.999999999" )
Keys for "built-in" attributes.
Variables ¶
This section is empty.
Functions ¶
func NewContextWithKey ¶ added in v3.10.6
Types ¶
type Buffer ¶ added in v3.10.6
type Buffer []byte
buffer adapted from go/src/fmt/print.go
func (*Buffer) WritePosInt ¶ added in v3.10.6
func (*Buffer) WritePosIntWidth ¶ added in v3.10.6
WritePosIntWidth writes non-negative integer i to the buffer, padded on the left by zeroes to the given width. Use a width of 0 to omit padding.
func (*Buffer) WriteString ¶ added in v3.10.6
type Handler ¶ added in v3.10.6
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶ added in v3.10.6
NewJSONHandler creates a Handler that writes to w, using the default options.
func (*Handler) Enabled ¶ added in v3.10.6
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*Handler) Handle ¶ added in v3.10.6
Handle formats its argument Record as a JSON object on a single line.
type HandlerOptions ¶ added in v3.10.6
type HandlerOptions struct { // Ignore records with levels below Level.Level(). // The default is InfoLevel. Level slog.Leveler // ReplaceAttr can be used to change the default keys of the built-in // attributes, convert types (for example, to replace a `time.Time` with the // integer seconds since the Unix epoch), sanitize personal information, or // remove attributes from the output. ReplaceAttr func(a slog.Attr) slog.Attr }
HandlerOptions are options for a TextHandler or Handler. A zero HandlerOptions consists entirely of default values.
func (HandlerOptions) NewHandler ¶ added in v3.10.6
func (opts HandlerOptions) NewHandler(w io.Writer, json bool) *Handler
NewJSONHandler creates a Handler with the given options that writes to w.
type Helper ¶
type Helper struct { Logger // contains filtered or unexported fields }
Helper for logger
func SetDefault ¶ added in v3.10.6
SetDefault sets the default Logger and returns default.
func (*Helper) WithFields ¶
WithFields write logger fields
type Level ¶
type Level int8
const ( // TraceLevel level. Designates finer-grained informational events than the Debug. TraceLevel Level = iota - 2 // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel // InfoLevel is the default logging priority. // General operational entries about what's going on inside the application. InfoLevel // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel // ErrorLevel level. Logs. Used for errors that should definitely be noted. ErrorLevel // FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity. FatalLevel )
func GetLevel ¶
GetLevel converts a level string into a logger Level value. returns an error if the input string does not match known values.
type Logger ¶
type Logger interface { // Init initializes options Init(options ...Option) error // The Logger options Options() Options // Fields set fields to always be logged Fields(fields map[string]interface{}) Logger // Log writes a log entry Log(level Level, v ...interface{}) // Logf writes a formatted log entry Logf(level Level, format string, v ...interface{}) // String returns the name of logger String() string }
Logger is a generic logging interface
var DefaultLogger Logger
func FromContextWithKey ¶ added in v3.10.6
type Option ¶
type Option func(*Options)
func WithCallerSkipCount ¶
WithCallerSkipCount set frame count to skip
func WithContext ¶ added in v3.10.6
func WithContext(k, v interface{}) Option
WithContext set default context for the logger
func WithFields ¶
WithFields set default fields for the logger
func WithFormat ¶ added in v3.10.6
WithFormat set default output format for the logger
func WithHandler ¶ added in v3.10.6
WithHandler set default handler for the logger
func WithOutput ¶
WithOutput set default output writer for the logger
type Options ¶
type Options struct { // The logging level the logger should log at. default is `InfoLevel` Level Level // fields to always be logged Fields map[string]interface{} // It's common to set this to a file, or leave it default which is `os.Stderr` Out io.Writer // Caller skip frame count for file:line info CallerSkipCount int // Alternative options Context context.Context // Format log print format options Format Format // A Handler handles log records produced by a Logger.. // // A typical handler may print log records to standard error, // or write them to a file or database, or perhaps augment them // with additional attributes and pass them on to another handler. // // Any of the Handler's methods may be called concurrently with itself // or with other methods. It is the responsibility of the Handler to // manage this concurrency. Handler slog.Handler }