log

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Unlicense Imports: 11 Imported by: 115

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

Examples

Constants

View Source
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 Debug

func Debug(format string, args ...any)

Debug writes to debug log

func Error

func Error(format string, args ...any)

Error writes to error log

func Fatal

func Fatal(args ...any)

Fatal writes to error log and exits application

func Fatalf

func Fatalf(format string, args ...any)

Fatalf writes to error log and exits application

func Info

func Info(format string, args ...any)

Info writes to info log

func OnCloserError added in v0.8.2

func OnCloserError(closer io.Closer, l Level)

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.SetFlags(0)
	log.SetLevel(log.INFO)
	log.SetOutput(os.Stdout)

	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 added in v0.7.0

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.SetFlags(0)
	log.SetLevel(log.INFO)
	log.SetOutput(os.Stdout)

	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 added in v0.8.1

func OnPanicAndExit(prefix string, exitCode int)

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 added in v0.10.9

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.SetFlags(0)
	log.SetLevel(log.INFO)
	log.SetOutput(os.Stdout)

	defer log.OnPanic("")

	log.Panic("fail")

}
Output:


[panic] fail
[error] recovered from panic: fail

func Panicf added in v0.10.9

func Panicf(format string, args ...any)

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

Example
package main

import (
	"os"

	"github.com/AdguardTeam/golibs/log"
)

func main() {
	log.SetFlags(0)
	log.SetLevel(log.INFO)
	log.SetOutput(os.Stdout)

	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 Print

func Print(args ...any)

Print writes to info log

func Printf

func Printf(format string, args ...any)

Printf writes to info log

func Println

func Println(args ...any)

Println writes to info log

func SetFlags added in v0.5.0

func SetFlags(flags int)

SetFlags sets the output flags for the default logger. The flag bits are Ldate, Ltime, and so on.

func SetLevel

func SetLevel(l Level)

SetLevel sets logging level.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets output printing method

func StdLog added in v0.4.4

func StdLog(prefix string, l Level) (std *log.Logger)

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.

func Tracef

func Tracef(format string, args ...any)

Tracef writes to debug log and adds the calling function's name

func Writer added in v0.4.3

func Writer() io.Writer

Writer returns the output destination for the default logger.

Types

type Level added in v0.8.0

type Level uint32

Level is the log level type.

Example
package main

import (
	"os"

	"github.com/AdguardTeam/golibs/log"
)

func main() {
	log.SetOutput(os.Stdout)
	log.SetFlags(0)

	log.Info("printed")

	log.SetLevel(log.OFF)
	log.Info("not printed")

}
Output:


[info] printed
const (
	OFF Level = iota
	ERROR
	INFO
	DEBUG
)

Level constants.

func GetLevel added in v0.1.3

func GetLevel() (l Level)

GetLevel returns level

func (Level) String added in v0.8.2

func (l Level) String() string

String implements fmt.Stringer for Level

type Timer added in v0.1.2

type Timer struct {
	// contains filtered or unexported fields
}

Timer is a wrapper for time

func StartTimer added in v0.1.2

func StartTimer() Timer

StartTimer returns a Timer with a start time

func (*Timer) LogElapsed added in v0.1.2

func (t *Timer) LogElapsed(message string, args ...any)

LogElapsed writes to log message and elapsed time

Jump to

Keyboard shortcuts

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