log

package
v0.0.0-...-9d39026 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Unlicense Imports: 11 Imported by: 6

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

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/Potterli20/golibs-fork/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/Potterli20/golibs-fork/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

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

func Panic(args ...any)

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

Example
package main

import (
	"os"

	"github.com/Potterli20/golibs-fork/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

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

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

Example
package main

import (
	"os"

	"github.com/Potterli20/golibs-fork/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 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

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

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

func Writer() io.Writer

Writer returns the output destination for the default logger.

Types

type Level

type Level uint32

Level is the log level type.

const (
	ERROR Level = iota
	INFO
	DEBUG
)

Level constants.

func GetLevel

func GetLevel() (l Level)

GetLevel returns level

func (Level) String

func (l Level) String() string

String implements fmt.Stringer for Level

type Timer

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

Timer is a wrapper for time

func StartTimer

func StartTimer() Timer

StartTimer returns a Timer with a start time

func (*Timer) LogElapsed

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