sdk

package
Version: v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package sdk/bridge provides methods to either hook into the SDK logger itself or create compatible instances.

Hooks

The simples way is to simply anonymously import the hook package to configure the whole application to use the slf4g framework on any usage of the SDK based loggers.

import (
   _ "github.com/echocat/slf4g/sdk/bridge/hook"
)

For manual hooks please see the examples.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOnFatal = func(log.Event) {
	os.Exit(1)
}

DefaultOnFatal defines what happens by default when someone calls one of the Logger.Fatal(), Logger.Fatalf() or Logger.Fatalln() methods. The initial behavior will be that it exit with error code 1 after logging the event.

View Source
var DefaultOnPanic = func(e log.Event) {
	panic(e)
}

DefaultOnPanic defines what happens by default when someone calls one of the Logger.Panic(), Logger.Panicf() or Logger.Panicln() methods. The initial behavior will be that it panics after logging the event.

Functions

func Configure

func Configure(customizer ...func(*log.LoggingWriter))

Configure configures the standard SDK logging framework to use slf4g with its root Logger and logs everything printed using Print(), Printf() and Println() on level.Info.

Limitations

1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level as everything else.

2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or panics afterwards. This cannot be prevented when you use the logger directly. If possible use NewLogger() to use an SDK compatible interface.

Example
// Configures the whole application with to use the ROOT Logger and logs
// everything to level.Info.
Configure()
Output:

func ConfigureWith

func ConfigureWith(target log.CoreLogger, logAs level.Level, customizer ...func(*log.LoggingWriter))

ConfigureWith configures the standard SDK logging framework to use slf4g with it's the given Logger and logs everything printed using Print(), Printf() and Println() on given level.Level.

Limitations

1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level as everything else.

2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or panics afterwards. This cannot be prevented when you use the logger directly. If possible use NewLogger() to use an SDK compatible interface.

Example
// Configures the whole application with to use the logger named "sdk" and
// logs everything to level.Debug.
ConfigureWith(log.GetLogger("sdk"), level.Debug)
Output:

func NewWrapper

func NewWrapper(target log.CoreLogger, logAs level.Level) *sdklog.Logger

NewWrapper creates an SDK Logger which is use slf4g the provided Logger and logs everything printed using Print(), Printf() and Println() on given level.Level.

Limitations

1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level as everything else.

2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or panics afterwards. This cannot be prevented when you use the logger directly. If possible use NewLogger() to use an SDK compatible interface.

Example
// Creates a new SDK logger that uses the slf4g logger "sdk" and logs
// everything to level.Info.
wrapped := NewWrapper(log.GetLogger("sdk"), level.Info)

wrapped.Print("foo", "bar")
Output:

Types

type Logger

type Logger interface {
	// Print calls l.Output to print to the logger.
	// Arguments are handled in the manner of fmt.Print.
	Print(...interface{})
	// Printf calls l.Output to print to the logger.
	// Arguments are handled in the manner of fmt.Printf.
	Printf(string, ...interface{})
	// Println calls l.Output to print to the logger.
	// Arguments are handled in the manner of fmt.Println.
	Println(...interface{})

	// Fatal is equivalent to l.Print() and can be followed by a call to
	// os.Exit(1).
	Fatal(...interface{})
	// Fatalf is equivalent to l.Printf() and can be followed by a call to
	// os.Exit(1).
	Fatalf(string, ...interface{})
	// Fatalln is equivalent to l.Println() and can be followed by a call to
	// os.Exit(1).
	Fatalln(...interface{})

	// Panic is equivalent to l.Print() and can followed by a call to panic().
	Panic(...interface{})
	// Panicf is equivalent to l.Printf() and can followed by a call to panic().
	Panicf(string, ...interface{})
	// Panicln is equivalent to l.Println() and can followed by a call to
	// panic().
	Panicln(...interface{})
}

Logger is an interface which describes instances which are compatible with the SDK Logger instance.

func NewLogger

func NewLogger(target log.CoreLogger, printLevel level.Level) Logger

NewLogger creates a new instance of an SDK compatible Logger which forwards all it's events to the provided log.CoreLogger.

printLevel defines the level which is used to log to the given log.CoreLogger on every Logger.Print(), Logger.Printf() and Logger.Println() event.

Example
// Creates a new SDK compatible logger that uses the slf4g logger "sdk" and
// logs everything to level.Info.
wrapped := NewLogger(log.GetLogger("sdk"), level.Info)

wrapped.Print("foo", "bar")
Output:

type LoggerImpl

type LoggerImpl struct {
	// Delegate is the Logger of the slf4g framework where to forward all logged
	// events of this implementation to.
	Delegate log.CoreLogger

	// PrintLevel defines the regular level.Level to log everything one if
	// methods Print(), Printf() or Println() are used. If this is not set
	// level.Info is used.
	PrintLevel level.Level

	// OnPanic defines what to do if someone calls one of the Logger.Panic(),
	// Logger.Panicf() or Logger.Panicln() methods. Be default DefaultOnPanic
	// is used.
	OnPanic func(log.Event)

	// OnFatal defines what to do if someone calls one of the Logger.Fatal(),
	// Logger.Fatalf() or Logger.Fatalln() methods. Be default DefaultOnFatal
	// is used.
	OnFatal func(log.Event)
}

LoggerImpl is a default implementation of the Logger interface. It forwards all logged events to the configured Delegate.

func (*LoggerImpl) Fatal

func (instance *LoggerImpl) Fatal(args ...interface{})

Fatal implements Logger.Fatal

func (*LoggerImpl) Fatalf

func (instance *LoggerImpl) Fatalf(s string, args ...interface{})

Fatalf implements Logger.Fatalf

func (*LoggerImpl) Fatalln

func (instance *LoggerImpl) Fatalln(args ...interface{})

Fatalln implements Logger.Fatalln

func (*LoggerImpl) Panic

func (instance *LoggerImpl) Panic(args ...interface{})

Panic implements Logger.Panic

func (*LoggerImpl) Panicf

func (instance *LoggerImpl) Panicf(s string, args ...interface{})

Panicf implements Logger.Panicf

func (*LoggerImpl) Panicln

func (instance *LoggerImpl) Panicln(args ...interface{})

Panicln implements Logger.Panicln

func (*LoggerImpl) Print

func (instance *LoggerImpl) Print(args ...interface{})

Print implements Logger.Print

func (*LoggerImpl) Printf

func (instance *LoggerImpl) Printf(s string, args ...interface{})

Printf implements Logger.Printf

func (*LoggerImpl) Println

func (instance *LoggerImpl) Println(args ...interface{})

Println implements Logger.Println

Directories

Path Synopsis
Importing this package anonymously will configure the whole application to use the slf4g framework on any usage of the SDK based loggers.
Importing this package anonymously will configure the whole application to use the slf4g framework on any usage of the SDK based loggers.

Jump to

Keyboard shortcuts

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