logging

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 10 Imported by: 31

README

logging

semantic-release Release license release Go Report Card

This project is in alpha state. It can AND will continue breaking until version 1.0.0 is released

What Is It

TBD

Supported Go Versions

For security reasons, we only support and recommend the use of one of the latest two Go versions (✅).
Versions that also build are marked with ⚠.

Version Supported
<1.21
1.21
1.22

License

The full functionality of this library is and stays open source and free to use for everyone. Visit our website and get in touch.

See the exact licensing terms here

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

View Source
const (
	FormatterText = "text"
	FormatterJSON = "json"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func Debugf

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

func Debugln

func Debugln(args ...interface{})

func EnableHTTPClient added in v0.4.0

func EnableHTTPClient(c *http.Client, opts ...ClientLoggerOption)

EnableHTTPClient adds slog functionality to the HTTP client. It attempts to obtain a logger with FromContext. If no logger is in the context, it tries to use a fallback logger, which might be set by WithFallbackLogger. If no logger was found finally, the Transport is executed without logging.

func Error

func Error(args ...interface{})

func Errorf

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

func Errorln

func Errorln(args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Fatalln

func Fatalln(args ...interface{})

func FromContext added in v0.4.0

func FromContext(ctx context.Context) (logger *slog.Logger, ok bool)

FromContext takes a Logger from the context, if it was previously set by ToContext

func Info

func Info(args ...interface{})

func Infof

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

func Infoln

func Infoln(args ...interface{})

func Logf

func Logf(level logrus.Level, format string, args ...interface{})

func Logln

func Logln(level logrus.Level, args ...interface{})

func Middleware added in v0.4.0

func Middleware(options ...MiddlewareOption) func(http.Handler) http.Handler

Middleware enables request logging and sets a logger to the request context. Use FromContext to obtain the logger anywhere in the request liftime.

The default logger is slog.Default, with the request's URL and Method as preset attributes. When the request terminates, a INFO line with the Status Code and amount written to the client is printed. This behaviors can be modified with options.

func Panic

func Panic(args ...interface{})

func Panicf

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

func Panicln

func Panicln(args ...interface{})

func SetFormatter

func SetFormatter(formatter logrus.Formatter)

func SetGlobal

func SetGlobal()

func SetIDKey

func SetIDKey(key string)

SetIDKey key of id in logentry

func SetLevel

func SetLevel(level logrus.Level)

func SetOutput

func SetOutput(out io.Writer)

func StringerValuer added in v0.4.0

func StringerValuer(s fmt.Stringer) slog.LogValuer

StringValuer returns a Valuer that forces the logger to use the type's String method, even in json ouput mode. By wrapping the type we defer String being called to the point we actually log.

func ToContext added in v0.4.0

func ToContext(ctx context.Context, logger *slog.Logger) context.Context

ToContext sets a Logger to the context.

func Trace

func Trace(args ...interface{})

func Tracef

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

func Traceln

func Traceln(args ...interface{})

func Warn

func Warn(args ...interface{})

func Warnf

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

func Warning

func Warning(args ...interface{})

func Warningf

func Warningf(format string, args ...interface{})

func Warningln

func Warningln(args ...interface{})

func Warnln

func Warnln(args ...interface{})

Types

type ClientLoggerOption added in v0.4.0

type ClientLoggerOption func(*logRountTripper)

func WithClientDurationFunc added in v0.4.0

func WithClientDurationFunc(df func(time.Time) time.Duration) ClientLoggerOption

WithClientDurationFunc allows overiding the request duration for testing.

func WithClientGroup added in v0.4.0

func WithClientGroup(name string) ClientLoggerOption

WithClientGroup groups the log attributes produced by the client.

func WithClientRequestAttr added in v0.4.0

func WithClientRequestAttr(requestToAttr func(*http.Request) slog.Attr) ClientLoggerOption

WithClientRequestAttr allows customizing the information used from a request as request attributes.

func WithClientResponseAttr added in v0.4.0

func WithClientResponseAttr(responseToAttr func(*http.Response) slog.Attr) ClientLoggerOption

WithClientResponseAttr allows customizing the information used from a response as response attributes.

func WithFallbackLogger added in v0.4.0

func WithFallbackLogger(logger *slog.Logger) ClientLoggerOption

WithFallbackLogger uses the passed logger if none was found in the context.

type Config

type Config struct {
	Level       string    `json:"level"`
	Formatter   formatter `json:"formatter"`
	LocalLogger bool      `json:"localLogger"`
	AddSource   bool      `json:"addSource"`
}

func (*Config) SetLogger

func (c *Config) SetLogger() (err error)

func (*Config) Slog added in v0.5.0

func (c *Config) Slog() *slog.Logger

Slog constructs a slog.Logger with the Formatter and Level from config.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error

type Entry

type Entry struct {
	*logrus.Entry
	// contains filtered or unexported fields
}

func Log deprecated

func Log(id string) *Entry

Deprecated: Log creates a new entry with an id

func LogWithFields deprecated

func LogWithFields(id string, fields ...interface{}) *Entry

Deprecated: LogWithFields creates a new entry with an id and the given fields

func New

func New() *Entry

New instantiates a new entry

func OnError

func OnError(err error) *Entry

func WithError added in v0.3.4

func WithError(err error) *Entry

func WithFields

func WithFields(fields ...interface{}) *Entry

WithFields creates a new entry without an id and the given fields

func (*Entry) Debug

func (e *Entry) Debug(args ...interface{})

func (*Entry) Debugf

func (e *Entry) Debugf(format string, args ...interface{})

func (*Entry) Debugln

func (e *Entry) Debugln(args ...interface{})

func (*Entry) Error

func (e *Entry) Error(args ...interface{})

func (*Entry) Errorf

func (e *Entry) Errorf(format string, args ...interface{})

func (*Entry) Errorln

func (e *Entry) Errorln(args ...interface{})

func (*Entry) Fatal

func (e *Entry) Fatal(args ...interface{})

func (*Entry) Fatalf

func (e *Entry) Fatalf(format string, args ...interface{})

func (*Entry) Fatalln

func (e *Entry) Fatalln(args ...interface{})

func (*Entry) Info

func (e *Entry) Info(args ...interface{})

func (*Entry) Infof

func (e *Entry) Infof(format string, args ...interface{})

func (*Entry) Infoln

func (e *Entry) Infoln(args ...interface{})

func (*Entry) Log

func (e *Entry) Log(level logrus.Level, args ...interface{})

func (*Entry) Logf

func (e *Entry) Logf(level logrus.Level, format string, args ...interface{})

func (*Entry) Logln

func (e *Entry) Logln(level logrus.Level, args ...interface{})

func (*Entry) OnError

func (e *Entry) OnError(err error) *Entry

OnError sets the error. The log will only be printed if err is not nil

func (*Entry) Panic

func (e *Entry) Panic(args ...interface{})

func (*Entry) Panicf

func (e *Entry) Panicf(format string, args ...interface{})

func (*Entry) Panicln

func (e *Entry) Panicln(args ...interface{})

func (*Entry) SetFields

func (e *Entry) SetFields(fields ...interface{}) *Entry

SetFields sets the given fields on the entry. It panics if length of fields is odd

func (*Entry) Trace

func (e *Entry) Trace(args ...interface{})

func (*Entry) Tracef

func (e *Entry) Tracef(format string, args ...interface{})

func (*Entry) Traceln

func (e *Entry) Traceln(args ...interface{})

func (*Entry) Warn

func (e *Entry) Warn(args ...interface{})

func (*Entry) Warnf

func (e *Entry) Warnf(format string, args ...interface{})

func (*Entry) Warning

func (e *Entry) Warning(args ...interface{})

func (*Entry) Warningf

func (e *Entry) Warningf(format string, args ...interface{})

func (*Entry) Warningln

func (e *Entry) Warningln(args ...interface{})

func (*Entry) Warnln

func (e *Entry) Warnln(args ...interface{})

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) *Entry

func (*Entry) WithFields

func (e *Entry) WithFields(fields logrus.Fields) *Entry

func (*Entry) WithTime

func (e *Entry) WithTime(t time.Time) *Entry

type LoggedWriter added in v0.4.0

type LoggedWriter interface {
	http.ResponseWriter

	// Attr is called after the next handler
	// in the Middleware returns and
	// the complete reponse should have been written.
	//
	// The returned Attribute should be a [slog.Group]
	// containing response Attributes.
	Attr() slog.Attr

	// Err() is called by the middleware to check
	// if the underlying writer returned an error.
	// If so, the middleware will print an ERROR line.
	Err() error
}

LoggedWriter stores information regarding the response. This might be status code, amount of data written or header.

type MiddlewareOption added in v0.4.0

type MiddlewareOption func(*middleware)

func WithDurationFunc added in v0.4.0

func WithDurationFunc(df func(time.Time) time.Duration) MiddlewareOption

WithDurationFunc allows overriding the request duration for testing.

func WithGroup added in v0.4.0

func WithGroup(name string) MiddlewareOption

WithGroup groups the log attributes produced by the middleware.

func WithIDFunc added in v0.4.0

func WithIDFunc(nextID func() slog.Attr) MiddlewareOption

WithIDFunc enables the creating of request IDs in the middleware, which are then attached to the logger.

func WithLoggedWriter added in v0.4.0

func WithLoggedWriter(wrap func(w http.ResponseWriter) LoggedWriter) MiddlewareOption

WithLoggedWriter allows customizing the writer from which post-request attributes are taken.

func WithLogger added in v0.4.0

func WithLogger(logger *slog.Logger) MiddlewareOption

WitLogger sets the passed logger with request attributes into the Request's context.

func WithRequestAttr added in v0.4.0

func WithRequestAttr(requestToAttr func(*http.Request) slog.Attr) MiddlewareOption

WithRequestAttr allows customizing the information used from a request as request attributes.

Jump to

Keyboard shortcuts

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