ovirtclientlog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Unified logging interface for Go client libraries

This repository contains a simple unified logging interface for all oVirt Go client libraries. This is not a logger itself, just an interface definition coupled with default loggers.

This library is used as a dependency, so you will most likely not need to rely on it. However, if you need to fetch it you can do so using go get:

go get github.com/ovirt/go-client-log

You can then reference this library using the ovirtclientlog package name.

Default loggers

This library providers 3 default loggers:

  • Go logging
  • Go test logging
  • "NOOP" logging
Go logging

A Go logger can be created using the NewGoLogger() function. Optionally, a *log.Logger instance can be passed. If it is not passed, the log is written to the globally configured log destination.

logger := ovirtclientlog.NewGoLogger(nil)
Test logging

This library also contains the ability to log via Logf in *testing.T. You can create a logger like this:

func TestYourFeature(t *testing.T) {
	logger := ovirtclientlog.NewTestLogger(t)
}

Using the test logger will have the benefit that the log messages will be properly attributed to the test that wrote them even if multiple tests are executed in parallel.

NOOP logging

If you need a logger that doesn't do anything simply use ovirtclientlog.NewNOOPLogger().

Adding your own logger

You can easily integrate your own logger too. Loggers must satisfy the following interface:

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

For example, you can adapt logging to klog like this:

type klogLogger struct {
}

func (k klogLogger) Debugf(format string, args ...interface{}) {
	// klog doesn't have a debug level
	klog.Infof(format, args...)
}

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

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

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

You can then create a new logger copy like this:

logger := &klogLogger{}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger provides pluggable logging for oVirt client libraries.

func NewGoLogger

func NewGoLogger(logger *log.Logger) Logger

NewGoLogger creates a logger that writes to the Go log facility. The optional logger parameter can be used to create a scoped logger, otherwise the global logger is used.

func NewNOOPLogger

func NewNOOPLogger() Logger

NewNOOPLogger returns a logger that does nothing.

func NewTestLogger

func NewTestLogger(t *testing.T) Logger

NewTestLogger returns a logger that logs via the Go test facility

Jump to

Keyboard shortcuts

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