log

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2023 License: GPL-3.0 Imports: 5 Imported by: 7

README

godocs.io builds.sr.ht status Go Report Card

⚠️ Deprecated: use log/slog instead ⚠️

package log

Package log implements a simple level logging package that maintains API compatibility with the standard library log package. It extends the standard library log.Logger type with a Level type that can be used to define output verbosity. It adds additional methods that will be printed only if the logger is configured at or below a given level. For example:

package main

import "git.sr.ht/~spc/go-log"

func main() {
    log.SetLevel(log.LevelWarn)
    log.Traceln("Messages at LevelTrace will not print if the Level is Warn")
    log.Warnln("Messages at LevelTrace will print with the Level set to Warn")
}

Documentation

Overview

Package log implements a simple level logging package. It defines a type, Logger, with methods for formatting output at specific verbosity levels. It maintains API compatibility with the standard library "log" package by embedding a 'log.Logger' struct.

Index

Examples

Constants

View Source
const (
	Ldate         = log.Ldate
	Ltime         = log.Ltime
	Lmicroseconds = log.Lmicroseconds
	Llongfile     = log.Llongfile
	Lshortfile    = log.Lshortfile
	LUTC          = log.LUTC
	LstdFlags     = log.LstdFlags
)

These flags define which text to prefix to each log entry generated by the Logger. Bits are or'ed together to control what's printed. There is no control over the order they appear (the order listed here) or the format they present (as described in the comments). The prefix is followed by a colon only when Llongfile or Lshortfile is specified. For example, flags Ldate | Ltime (or LstdFlags) produce,

2009/01/23 01:23:23 message

while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,

2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

These variables are exported in order to maintain package API compatibility.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug prints to the standard logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Print.

func Debugf

func Debugf(format string, v ...interface{})

Debugf prints to the standard logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Printf.

func Debugln

func Debugln(v ...interface{})

Debugln prints to the standard logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Println.

func Error

func Error(v ...interface{})

Error prints to the standard logger if level is at least LevelError. Arguments are handled in the manner of fmt.Print.

func Errorf

func Errorf(format string, v ...interface{})

Errorf prints to the standard logger if level is at least LevelError. Arguments are handled in the manner of fmt.Printf.

func Errorln

func Errorln(v ...interface{})

Errorln prints to the standard logger if level is at least LevelError. Arguments are handled in the manner of fmt.Println.

func Fatal

func Fatal(v ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Fatalln is equivalent to Println() followed by a call to os.Exit(1).

func Flags

func Flags() int

Flags returns the output flags for the standard logger.

func FormatLevel

func FormatLevel(lvl Level) string

FormatLevel converts the Level to a string.

func Info

func Info(v ...interface{})

Info prints to the standard logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Print.

func Infof

func Infof(format string, v ...interface{})

Infof prints to the standard logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Printf.

func Infoln

func Infoln(v ...interface{})

Infoln prints to the standard logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Println.

func Output

func Output(calldepth int, s string) error

Output wraps a call to the standard logger's Output function for API compatibility.

Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Calldepth is the count of the number of frames to skip when computing the file name and line number if Llongfile or Lshortfile is set; a value of 1 will print the details for the caller of Output.

func Panic

func Panic(v ...interface{})

Panic is equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Panicf is equivalent to Printf() followed by a call to panic().

func Panicln

func Panicln(v ...interface{})

Panicln is equivalent to Println() followed by a call to panic().

func Prefix

func Prefix() string

Prefix returns the output prefix for the standard logger.

func Print

func Print(v ...interface{})

Print calls Print to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{})

Printf calls Printf to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{})

Println calls Println to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func SetFlags

func SetFlags(flag int)

SetFlags sets the output flags for the standard logger.

func SetLevel

func SetLevel(l Level)

SetLevel sets the output level for the standard logger.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the standard logger.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the output prefix for the standard logger.

func Trace

func Trace(v ...interface{})

Trace prints to the standard logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Print.

func Tracef

func Tracef(format string, v ...interface{})

Tracef prints to the standard logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Printf.

func Traceln

func Traceln(v ...interface{})

Traceln prints to the standard logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Println.

func Warn

func Warn(v ...interface{})

Warn prints to the standard logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Print.

func Warnf

func Warnf(format string, v ...interface{})

Warnf prints to the standard logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Printf.

func Warnln

func Warnln(v ...interface{})

Warnln prints to the standard logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Println.

func Writer

func Writer() io.Writer

Writer returns the output destination for the standard logger.

Types

type Level

type Level int

Level defines various levels of logging output

const (
	LevelError Level = iota
	LevelWarn
	LevelInfo
	LevelDebug
	LevelTrace

	LevelUnknown Level = -1
)

LevelError is the default level.

func CurrentLevel

func CurrentLevel() Level

CurrentLevel returns the output level for the standard logger.

func ParseLevel

func ParseLevel(str string) (Level, error)

ParseLevel returns the Level value represented by the string.

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	*log.Logger
	Level Level
}

A Logger represents a standard library Logger, configured for output at a given verbosity level. See standard library log.Logger for detailed usage.

Example
// Set the output to Stdout so it is captured by the example test harness.
// Remember the standard library logger output is Stderr by default.
log.SetOutput(os.Stdout)

// Clear the prefix flags so we aren't dancing around timestamps.
log.SetFlags(0)

// Log!
log.Error("Error messages are always logged")
log.Debug("Debug messages are sometimes logged... but not this one...")

// Raise the log level
log.SetLevel(log.LevelDebug)

// Log again!
log.Debug("Now that the log level is set to Debug, this message is logged!")
log.Trace("But we still have one more level that is not logged at the Debug level.")
Output:

Error messages are always logged
Now that the log level is set to Debug, this message is logged!

func New

func New(out io.Writer, prefix string, flag int, level Level) *Logger

New creates a new Logger. The out, prefix and flag variables are passed directly to the embedded standard library log.New function. The level argument defines a minimum verbosity level.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug prints to the logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf prints to the logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln prints to the logger if level is at least LevelDebug. Arguments are handled in the manner of fmt.Println.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error prints to the logger if level is at least LevelError. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf prints to the logger if level is at least LevelError. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln prints to the logger if level is at least LevelError. Arguments are handled in the manner of fmt.Println.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info prints to the logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof prints to the logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln prints to the logger if level is at least LevelInfo. Arguments are handled in the manner of fmt.Println.

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace prints to the logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Print.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

Tracef prints to the logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Traceln

func (l *Logger) Traceln(v ...interface{})

Traceln prints to the logger if level is at least LevelTrace. Arguments are handled in the manner of fmt.Println.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn prints to the logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf prints to the logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warnln prints to the logger if level is at least LevelWarn. Arguments are handled in the manner of fmt.Println.

type ParseError

type ParseError string

A ParseError occurs when a string value cannot be parsed to a valid level.

func (ParseError) Error

func (e ParseError) Error() string

Directories

Path Synopsis
cmd
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL