Documentation ¶
Overview ¶
Package log contains necessary logging functions
TODO(a.garipov): Move code using this to log/slog then deprecate and remove this package.
Index ¶
- Constants
- func Debug(format string, args ...any)
- func Error(format string, args ...any)
- func Fatal(args ...any)
- func Fatalf(format string, args ...any)
- func Info(format string, args ...any)
- func OnCloserError(closer io.Closer, l Level)
- func OnPanic(prefix string)
- func OnPanicAndExit(prefix string, exitCode int)
- func Panic(args ...any)
- func Panicf(format string, args ...any)
- func Print(args ...any)
- func Printf(format string, args ...any)
- func Println(args ...any)
- func SetFlags(flags int)
- func SetLevel(l Level)
- func SetOutput(w io.Writer)
- func StdLog(prefix string, l Level) (std *log.Logger)
- func Tracef(format string, args ...any)
- func Writer() io.Writer
- type Level
- type Timer
Examples ¶
Constants ¶
const ( Ldate = 1 << iota Ltime Lmicroseconds Llongfile Lshortfile LUTC Lmsgprefix LstdFlags = Ldate | Ltime )
These constants are the same as in the standard package "log".
See the documentation for log.Ldate, etc.
Variables ¶
This section is empty.
Functions ¶
func OnCloserError ¶
OnCloserError is a convenient helper to log errors returned by io.Closer The point is to not lose information from deferred Close calls. The error is logged with the specified logging level.
Instead of:
defer f.Close()
You can now write:
defer log.OnCloserError(f, log.DEBUG)
Note that if closer is nil, it is simply ignored.
Example ¶
package main import ( "io" "os" "github.com/AdguardTeam/golibs/log" ) type ErrorCloser struct{} func (c *ErrorCloser) Close() error { return io.EOF } func main() { log.SetOutput(os.Stdout) log.SetFlags(0) closer := &ErrorCloser{} f := func() { defer log.OnCloserError(closer, log.ERROR) } f() }
Output: [error] github.com/AdguardTeam/golibs/log_test.ExampleOnCloserError.func1(): error occurred in a Close call: EOF
func OnPanic ¶
func OnPanic(prefix string)
OnPanic is a convenient deferred helper function to log a panic in a goroutine. It should not be used where proper error handling is required.
Example ¶
package main import ( "os" "github.com/AdguardTeam/golibs/log" ) func main() { log.SetOutput(os.Stdout) log.SetFlags(0) f := func() { defer log.OnPanic("") panic("fail") } f() f = func() { defer log.OnPanic("f") panic("fail") } f() }
Output: [error] recovered from panic: fail [error] f: recovered from panic: fail
func OnPanicAndExit ¶
OnPanicAndExit is a convenient deferred helper function to log a panic in a goroutine. Once a panic happens, it logs it and then calls os.Exit with the specified exit code.
func Panic ¶
func Panic(args ...any)
Panic is equivalent to Print() followed by a call to panic().
Example ¶
package main import ( "os" "github.com/AdguardTeam/golibs/log" ) func main() { log.SetOutput(os.Stdout) log.SetFlags(0) defer log.OnPanic("") log.Panic("fail") }
Output: [panic] fail [error] recovered from panic: fail
func Panicf ¶
Panicf is equivalent to Printf() followed by a call to panic().
Example ¶
package main import ( "os" "github.com/AdguardTeam/golibs/log" ) func main() { log.SetOutput(os.Stdout) log.SetFlags(0) defer log.OnPanic("") log.Panicf("fail, some number: %d", 123) }
Output: [panic] fail, some number: 123 [error] recovered from panic: fail, some number: 123
func SetFlags ¶
func SetFlags(flags int)
SetFlags sets the output flags for the default logger. The flag bits are Ldate, Ltime, and so on.
func StdLog ¶
StdLog returns a Go standard library logger that writes everything to logs the way this library's logger would. This is useful for cases that require a stdlib logger, for example http.Server.ErrorLog.
Types ¶
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer is a wrapper for time
func (*Timer) LogElapsed ¶
LogElapsed writes to log message and elapsed time