sypl

package module
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 15 Imported by: 3

README

sypl

sypl provides a Simple Yet Powerful Logger built on top of the Golang sypl. A sypl logger can have many Outputs, and each Output is responsible for writing to a specified destination. Each Output can have multiple Processors, which run in isolation manipulating the log message. The order of execution is important, and is according to the registering (add) order. These features allow sypl to fit into many different logging flows.

Install

$ go get github.com/saucelabs/sypl

Specific version

Example: $ go get github.com/saucelabs/sypl@v1.2.0

Usage

See example_test.go, and sypl_test.go file.

Documentation

Run $ make doc or check out online.

How it works

A picture worth thousand words.

high-level-arch

Development

Check out CONTRIBUTION.

Release
  1. Update CHANGELOG accordingly.
  2. Once changes from MR are merged.
  3. Tag and release.

Roadmap

Check out CHANGELOG.

Documentation

Overview

Package sypl provides a Simple Yet Powerful Logger built on top of the Golang logger. A sypl logger can have many `Output`s, and each `Output` is responsible for writing to a specified destination. Each Output can have multiple `Processor`s, which run in isolation manipulating the log message. The order of execution is according to the registering order. The above features allow sypl to fit into many different logging flows and needs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IBasePrinter added in v1.3.2

type IBasePrinter interface {
	// PrintMessage prints messages. It's a powerful option because it gives
	// full-control over the message. Use `NewMessage` to create the message.
	PrintMessage(messages ...message.IMessage) ISypl

	// PrintWithOptions is a more flexible way of printing, allowing to specify
	// a few message's options. For full-control over the message is possible
	// via `PrintMessage`.
	PrintWithOptions(o *options.Options, l level.Level, args ...interface{}) ISypl

	// PrintfWithOptions prints according with the specified format. It's a more
	// flexible way of printing, allowing to specify a few message's options.
	// For full-control over the message is possible via `PrintMessage`.
	PrintfWithOptions(o *options.Options, l level.Level, format string, args ...interface{}) ISypl

	// PrintfWithOptions prints according with the specified format, also adding
	// a new line to the end. It's a more flexible way of printing, allowing to
	// specify a few message's options. For full-control over the message is
	// possible via `PrintMessage`.
	PrintlnfWithOptions(o *options.Options, l level.Level, format string, args ...interface{}) ISypl

	// PrintfWithOptions prints, also adding a new line to the end. It's a more
	// flexible way of printing, allowing to specify a few message's options.
	// For full-control over the message is possible via `PrintMessage`.
	PrintlnWithOptions(o *options.Options, l level.Level, args ...interface{}) ISypl
}

IBasePrinter specifies the foundation for other printers.

type IBasicPrinter added in v1.3.2

type IBasicPrinter interface {
	// Print just prints.
	Print(l level.Level, args ...interface{}) ISypl

	// Printf prints according with the specified format.
	Printf(l level.Level, format string, args ...interface{}) ISypl

	// Printlnf prints according with the specified format, also adding a new
	// line to the end.
	Printlnf(l level.Level, format string, args ...interface{}) ISypl

	// Println prints, also adding a new line to the end.
	Println(l level.Level, args ...interface{}) ISypl
}

IBasicPrinter specifies the basic printers.

type IConvenientPrinter added in v1.3.2

type IConvenientPrinter interface {
	// PrintPretty prints data structures as JSON text.
	//
	// Notes:
	// - Only exported fields of the data structure will be printed.
	// - Message isn't processed.
	PrintPretty(l level.Level, data interface{}) ISypl

	// PrintlnPretty prints data structures as JSON text, also adding a new line
	// to the end.
	//
	// Notes:
	// - Only exported fields of the data structure will be printed.
	// - Message isn't processed.
	PrintlnPretty(l level.Level, data interface{}) ISypl

	// PrintMessagerPerOutput allows you to concurrently print messages, each
	// one, at the specified level and to the specified output.
	//
	// Note: If the named output doesn't exits, the message will not be printed.
	PrintMessagesToOutputs(messagesToOutputs ...MessageToOutput) ISypl
}

IConvenientPrinter specifies convenient printers.

type ILeveledPrinter added in v1.3.2

type ILeveledPrinter interface {
	// Fatal prints, and exit with os.Exit(1).
	Fatal(args ...interface{}) ISypl

	// Fatalf prints according with the format, and exit with os.Exit(1).
	Fatalf(format string, args ...interface{}) ISypl

	// Fatallnf prints according with the format, also adding a new line to the
	// end, and exit with os.Exit(1).
	Fatallnf(format string, args ...interface{}) ISypl

	// Fatalln prints, also adding a new line and the end, and exit with
	// os.Exit(1).
	Fatalln(args ...interface{}) ISypl

	// Error prints @ the Error level.
	Error(args ...interface{}) ISypl

	// Errorf prints according with the format @ the Error level.
	Errorf(format string, args ...interface{}) ISypl

	// Errorlnf prints according with the format @ the Error level, also adding
	// a new line to the end.
	Errorlnf(format string, args ...interface{}) ISypl

	// Errorln prints, also adding a new line to the end @ the Error level.
	Errorln(args ...interface{}) ISypl

	// Serror prints like Error, and returns an error with the non-processed
	// content.
	Serror(args ...interface{}) error

	// Serrorf prints like Errorf, and returns an error with the non-processed
	// content.
	Serrorf(format string, args ...interface{}) error

	// Serrorlnf prints like Errorlnf, and returns an error with the
	// non-processed content.
	Serrorlnf(format string, args ...interface{}) error

	// Serrorln prints like Errorln, and returns an error with the non-processed
	// content.
	Serrorln(args ...interface{}) error

	// Info prints @ the Info level.
	Info(args ...interface{}) ISypl

	// Infof prints according with the specified format @ the Info level.
	Infof(format string, args ...interface{}) ISypl

	// Infolnf prints according with the specified format @ the Info level, also
	// adding a new line to the end.
	Infolnf(format string, args ...interface{}) ISypl

	// Infoln prints, also adding a new line to the end @ the Info level.
	Infoln(args ...interface{}) ISypl

	// Warn prints @ the Warn level.
	Warn(args ...interface{}) ISypl

	// Warnf prints according with the specified format @ the Warn level.
	Warnf(format string, args ...interface{}) ISypl

	// Warnlnf prints according with the specified format @ the Warn level, also
	// adding a new line to the end.
	Warnlnf(format string, args ...interface{}) ISypl

	// Warnln prints, also adding a new line to the end @ the Warn level.
	Warnln(args ...interface{}) ISypl

	// Debug prints @ the Debug level.
	Debug(args ...interface{}) ISypl

	// Debugf prints according with the specified format @ the Debug level.
	Debugf(format string, args ...interface{}) ISypl

	// Debuglnf prints according with the specified format @ the Debug level,
	// also adding a new line to the end.
	Debuglnf(format string, args ...interface{}) ISypl

	// Debugln prints, also adding a new line to the end @ the Debug level.
	Debugln(args ...interface{}) ISypl

	// Trace prints @ the Trace level.
	Trace(args ...interface{}) ISypl

	// Tracef prints according with the specified format @ the Trace level.
	Tracef(format string, args ...interface{}) ISypl

	// Tracelnf prints according with the specified format @ the Trace level,
	// also adding a new line to the end.
	Tracelnf(format string, args ...interface{}) ISypl

	// Traceln prints, also adding a new line to the end @ the Trace level.
	Traceln(args ...interface{}) ISypl
}

ILeveledPrinter specifies the leveled printers.

type IPrinters added in v1.3.2

IPrinters is all available printers.

type ISypl added in v1.3.2

type ISypl interface {
	meta.IMeta
	IPrinters

	// String interface.
	String() string

	// GetMaxLevel returns the `maxLevel` of all outputs.
	GetMaxLevel() map[string]level.Level

	// SetMaxLevel sets the `maxLevel` of all outputs.
	SetMaxLevel(l level.Level)

	// AddOutputs adds one or more outputs.
	AddOutputs(outputs ...output.IOutput) ISypl

	// GetOutput returns the registered output by its name. If not found, will be nil.
	GetOutput(name string) output.IOutput

	// SetOutputs sets one or more outputs. Use to update output(s).
	SetOutputs(outputs ...output.IOutput)

	// GetOutputs returns registered outputs.
	GetOutputs() []output.IOutput

	// GetOutputsNames returns the names of the registered outputs.
	GetOutputsNames() []string

	// New creates a child logger.
	New(name string) ISypl
	// contains filtered or unexported methods
}

ISypl specified what a Sypl logger does.

type MessageToOutput added in v1.3.2

type MessageToOutput struct {
	// Content to be printed.
	Content string

	// Level of the message.
	Level level.Level

	// OutputName name of the output.
	OutputName string
}

MessageToOutput defines a `Message` to printed at the specified `Level`, and to the specified `Output`.

type Sypl

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

Sypl logger definition.

func New

func New(name string, outputs ...output.IOutput) *Sypl

New is the Sypl factory.

Example (Chained)

Chained is the chained example of creating, and setting up a `sypl` logger. It writes to both `stdout` and `stderr`.

package main

import (
	"os"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/message"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.New("Testing Logger").
		// Creates two `Output`s. "Console" and "Error". "Console" will print to
		// `Fatal`, `Error`, and `Info`. "Error" will only print `Fatal`, and
		// `Error` levels.
		AddOutputs(output.NewOutput("Console", level.Info, os.Stdout)).
		// Creates a `Processor`. It will prefix all messages. It will only
		// prefix messages for this specific `Output`, and @ `Error` level.
		AddOutputs(output.NewOutput("Error", level.Error, os.Stderr).
			AddProcessors(func(prefix string) processor.IProcessor {
				return processor.NewProcessor("Prefixer", func(message message.IMessage) error {
					if message.GetLevel() == level.Error {
						message.GetContent().SetProcessed(prefix + message.GetContent().GetProcessed())
					}

					return nil
				})
			}(shared.DefaultPrefixValue))).
		// Prints:
		// Test info message
		Println(level.Info, "Test info message").
		// Prints:
		// Test error message
		// My Prefix - Test error message
		Println(level.Error, "Test error message")

	
Output:

Example (ChainedUsingBuiltin)

ChainedUsingBuiltin is the chained example of creating, and setting up a `sypl` logger using built-in `Output`, and `Processor`. It writes to `stdout`, and `stderr`.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.New("Testing Logger").
		// Adds an `Output`. In this case, called "Console" that will print to
		// `stdout` and max print level @ `Info`.
		//
		// Adds a `Processor`. It will prefix all messages.
		AddOutputs(output.Console(level.Info).AddProcessors(processor.Prefixer(shared.DefaultPrefixValue))).
		// Prints: My Prefix - Test info message
		Infoln("Test info message")

}
Output:

My Prefix - Test info message
Example (ChildLoggers)

Child loggers example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/formatter"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/status"
)

func main() {
	// Creates logger, and name it.
	k8Logger := sypl.New("k8").
		AddOutputs(output.Console(level.Info).SetFormatter(formatter.Text()))

	k8Logger.Infoln("k8 connected")

	podLogger := k8Logger.New("pod")
	podLogger.Infoln("pod created")

	k8Logger.GetOutput("Console").SetStatus(status.Disabled)

	k8Logger.Infoln("k8 connected")
	podLogger.Infoln("pod created")

	// Prints:
	//
	// k8 connected component=k8 level=info timestamp=2021-08-11T09:24:13-07:00
	// pod created component=pod level=info timestamp=2021-08-11T09:24:13-07:00
}
Output:

Example (ErrorSimulator)

Simulates a problematic processor.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.New(shared.DefaultComponentNameOutput).
		AddOutputs(output.Console(level.Info, processor.ErrorSimulator("Test"))).
		Infoln(shared.DefaultContentOutput)

	// Prints:
	//
	// 2021/08/10 20:00:56 [sypl] [Error] Output: "Console" Processor: "ErrorSimulator" Error: "Test" Original Message: "contentTest"
}
Output:

Example (Flags)

Flags example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/flag"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.New("Testing Logger", output.Console(level.Info, processor.Prefixer(shared.DefaultPrefixValue))).
		// Message will be processed, and printed independent of `Level`
		// restrictions.
		PrintlnWithOptions(&options.Options{
			Flag: flag.Force,
		}, level.Debug, shared.DefaultContentOutput).

		// Message will be processed, but not printed.
		PrintlnWithOptions(&options.Options{
			Flag: flag.Mute,
		}, level.Info, shared.DefaultContentOutput).

		// Message will not be processed, but printed.
		PrintlnWithOptions(&options.Options{
			Flag: flag.Skip,
		}, level.Info, shared.DefaultContentOutput).

		// Should not print - restricted by level.
		Debugln(shared.DefaultContentOutput).

		// SkipAndForce message will not be processed, but will be printed
		// independent of `Level` restrictions.
		PrintlnWithOptions(&options.Options{
			Flag: flag.SkipAndForce,
		}, level.Debug, shared.DefaultContentOutput).

		// Message will not be processed, neither printed.
		PrintlnWithOptions(&options.Options{
			Flag: flag.SkipAndMute,
		}, level.Debug, shared.DefaultContentOutput)

}
Output:

My Prefix - contentTest
contentTest
contentTest
Example (InlineUsingBuiltin)

inlineUsingBuiltin same as `ChainedUsingBuiltin` but using inline form.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	sypl.New("Testing Logger", output.Console(level.Info).
		AddProcessors(processor.Prefixer(shared.DefaultPrefixValue))).
		Infoln("Test info message")

}
Output:

My Prefix - Test info message
Example (JsonFormatter)

JSON formatter example.

package main

import (
	"fmt"
	"strings"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/formatter"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	buf, o := output.SafeBuffer(level.Info)
	o.SetFormatter(formatter.JSON())

	// Creates logger, and name it.
	sypl.New(shared.DefaultComponentNameOutput).
		AddOutputs(o).
		PrintWithOptions(&options.Options{
			Fields: options.Fields{
				"field1": "value1",
				"field2": 1,
				"field3": true,
				"field4": []string{"1", "2"},
			},
		}, level.Info, shared.DefaultContentOutput)

	s := buf.String()

	fmt.Print(
		strings.Contains(s, `"component"`),
		strings.Contains(s, `"message"`),
		strings.Contains(s, `"field1"`),
		strings.Contains(s, `"field2"`),
		strings.Contains(s, `"field3"`),
		strings.Contains(s, `"field4"`),
		strings.Contains(s, `"level"`),
		strings.Contains(s, `"timestamp"`),
	)

	// Prints:
	//
	// {
	// 	"component": "componentNameTest",
	// 	"content": "contentTest",
	// 	"field1": "value1",
	// 	"field2": 1,
	// 	"field3": true,
	// 	"field4": [
	// 		"1",
	// 		"2"
	// 	],
	// 	"level": "info",
	// 	"timestamp": "2021-08-10T23:27:25-07:00"
	// }

}
Output:

true true true true true true true true
Example (NewDefault)

NewDefault output example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.NewDefault(shared.DefaultComponentNameOutput, level.Trace).
		Infoln(shared.DefaultContentOutput).
		Errorln("error message")

	// Prints:
	//
	// component=componentNameTest output=console level=info timestamp=2021-08-17T19:10:00-07:00 message=contentTest
	// component=componentNameTest output=console level=error timestamp=2021-08-17T19:10:00-07:00 message=error message
}
Output:

Example (NotChained)

NonChained is a non-chained example of creating, and setting up a `sypl` logger. It writes to a custom buffer.

package main

import (
	"fmt"
	"strings"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/message"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	buf := new(strings.Builder)

	// Creates logger, and name it.
	testingLogger := sypl.New("Testing Logger")

	// Creates an `Output`. In this case, called "Console" that will print to
	// `stdout` and max print level @ `Info`.
	ConsoleToStdOut := output.NewOutput("Console", level.Info, buf)

	// Creates a `Processor`. It will prefix all messages.
	Prefixer := func(prefix string) processor.IProcessor {
		return processor.NewProcessor("Prefixer", func(message message.IMessage) error {
			message.GetContent().SetProcessed(prefix + message.GetContent().GetProcessed())

			return nil
		})
	}

	// Adds `Processor` to `Output`.
	ConsoleToStdOut.AddProcessors(Prefixer(shared.DefaultPrefixValue))

	// Adds `Output` to logger.
	testingLogger.AddOutputs(ConsoleToStdOut)

	// Writes: "My Prefix - Test message"
	testingLogger.Print(level.Info, "Test info message")

	fmt.Println(buf.String())

}
Output:

My Prefix - Test info message
Example (PrintMessagesToOutputs)

PrintMessagesToOutputs example.

package main

import (
	"os"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/formatter"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
)

func main() {
	// Creates logger, and name it.
	sypl.New("pod").AddOutputs(
		output.NewOutput("Console 1", level.Trace, os.Stdout).SetFormatter(formatter.Text()),
		output.NewOutput("Console 2", level.Trace, os.Stdout).SetFormatter(formatter.Text()),
		output.NewOutput("Console 3", level.Trace, os.Stdout).SetFormatter(formatter.Text()),
	).PrintMessagesToOutputs(
		sypl.MessageToOutput{OutputName: "Console 1", Level: level.Info, Content: "Test 1\n"},
		sypl.MessageToOutput{OutputName: "Console 1", Level: level.Debug, Content: "Test 2\n"},
		sypl.MessageToOutput{OutputName: "Console 2", Level: level.Info, Content: "Test 3\n"},
		sypl.MessageToOutput{OutputName: "Console 4", Level: level.Info, Content: "Test 4\n"},
	)

	// Prints:
	//
	// output=Console 2 level=Info message=Test 3
	// output=Console 1 level=Debug message=Test 2
	// output=Console 1 level=Info message=Test 1
}
Output:

Example (PrintOnlyIfTagged)

PrintOnlyIfTagged output example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.NewDefault(shared.DefaultComponentNameOutput, level.Trace, processor.PrintOnlyIfTagged("testTag")).
		Infoln(shared.DefaultContentOutput).
		PrintlnWithOptions(&options.Options{
			Tags: []string{"testTag"},
		}, level.Info, shared.DefaultContentOutput)

	// Prints:
	//
	// component=componentNameTest output=console level=info timestamp=2021-08-17T19:19:57-07:00 message=contentTest
}
Output:

Example (PrintPretty)

PrintPretty example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
)

func main() {
	type TestType struct {
		Key1 string
		Key2 int
	}

	sypl.New("Testing Logger", output.Console(level.Info)).PrintPretty(level.Info, &TestType{
		Key1: "text",
		Key2: 12,
	})

}
Output:

{
	"Key1": "text",
	"Key2": 12
}
Example (PrintWithOptions)

printWithOptions demonstrates `sypl` flexibility. `Options` enhances the usual `PrintX` methods allowing to specify flags, and tags.

package main

import (
	"fmt"
	"strings"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/message"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/processor"
	"github.com/saucelabs/sypl/safebuffer"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	testingLogger := sypl.New("Testing Logger")

	// Creates 3 `Output`s, all called "Console" that will print to `stdout`, and
	// max print level @ `Info`.
	var c1buf safebuffer.Buffer
	Console1ToStdOut := output.NewOutput("Buffer 1", level.Info, &c1buf)

	var c2buf safebuffer.Buffer
	Console2ToStdOut := output.NewOutput("Buffer 2", level.Info, &c2buf)

	var c3buf safebuffer.Buffer
	Console3ToStdOut := output.NewOutput("Buffer 3", level.Info, &c3buf)

	// Creates a `Processor`. It will `prefix` all messages with the Output, and
	// Processor names.
	Prefixer := func() processor.IProcessor {
		return processor.NewProcessor("Prefixer", func(message message.IMessage) error {
			prefix := fmt.Sprintf("Output: %s Processor: %s Content: ",
				message.GetOutputName(),
				message.GetProcessorName(),
			)

			message.GetContent().SetProcessed(prefix + message.GetContent().GetProcessed())

			return nil
		})
	}

	// Creates a `Processor`. It will `suffix` all messages with the specified
	// `tag`.
	SuffixBasedOnTag := func(tag string) processor.IProcessor {
		return processor.NewProcessor("SuffixBasedOnTag", func(message message.IMessage) error {
			if message.ContainTag(tag) {
				message.GetContent().SetProcessed(message.GetContent().GetProcessed() + " - My Suffix")
			}

			return nil
		})
	}

	// Adds `Processor`s to `Output`s.
	Console1ToStdOut.AddProcessors(Prefixer(), SuffixBasedOnTag("SuffixIt"))
	Console2ToStdOut.AddProcessors(Prefixer(), SuffixBasedOnTag("SuffixIt"))
	Console3ToStdOut.AddProcessors(Prefixer(), SuffixBasedOnTag("SuffixIt"))

	// Adds all `Output`s to logger.
	testingLogger.AddOutputs(Console1ToStdOut, Console2ToStdOut, Console3ToStdOut)

	// Prints with prefix, without suffix.
	testingLogger.Print(level.Info, shared.DefaultContentOutput)

	fmt.Println(strings.EqualFold(c1buf.String(), "Output: Buffer 1 Processor: Prefixer Content: contentTest"))
	fmt.Println(strings.EqualFold(c2buf.String(), "Output: Buffer 2 Processor: Prefixer Content: contentTest"))
	fmt.Println(strings.EqualFold(c3buf.String(), "Output: Buffer 3 Processor: Prefixer Content: contentTest"))

	c1buf.Reset()
	c2buf.Reset()
	c3buf.Reset()

	// Prints with prefix, and suffix.
	testingLogger.PrintWithOptions(&options.Options{
		OutputsNames:    []string{"Buffer 1"},
		ProcessorsNames: []string{"Prefixer", "SuffixBasedOnTag"},
		Tags:            []string{"SuffixIt"},
	}, level.Info, shared.DefaultContentOutput)

	fmt.Println(strings.EqualFold(c1buf.String(), "Output: Buffer 1 Processor: Prefixer Content: contentTest - My Suffix"))

}
Output:

true
true
true
true
Example (SerrorX)

Serror{f|lnf|ln} example.

package main

import (
	"errors"
	"fmt"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	testingLogger := sypl.New("Testing Logger", output.Console(level.Info))

	sErrorResult := testingLogger.Serror(shared.DefaultContentOutput)

	errExample := errors.New("Failed to reach something")
	sErrorfResult := testingLogger.Serrorf("Failed to do something, %s", errExample)
	sErrorlnfResult := testingLogger.Serrorlnf("Failed to do something, %s", errExample)
	sErrorlnResult := testingLogger.Serrorln(shared.DefaultContentOutput)

	fmt.Print(
		sErrorResult.Error() == shared.DefaultContentOutput,
		sErrorfResult.Error() == "Failed to do something, Failed to reach something",
		sErrorlnfResult.Error() == "Failed to do something, Failed to reach something"+"\n",
		sErrorlnResult.Error() == shared.DefaultContentOutput+"\n",
	)

}
Output:

contentTestFailed to do something, Failed to reach somethingFailed to do something, Failed to reach something
contentTest
true true true true
Example (StdErrOutputExample)

StdErr output example.

package main

import (
	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	sypl.New(shared.DefaultComponentNameOutput, output.StdErr()).
		Infoln(shared.DefaultContentOutput).
		Errorln(shared.DefaultContentOutput)

	// Prints:
	//
	// contentTest
}
Output:

Example (SyplDebug)

SYPL_DEBUG (filter) example.

package main

import (
	"os"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/output"
)

func main() {
	// Creates logger, and name it.
	os.Setenv("SYPL_DEBUG", "pod,svc")

	sypl.New("pod").AddOutputs(output.Console(level.Info)).Infoln("pod created")
	sypl.New("svc").AddOutputs(output.Console(level.Info)).Infoln("svc created")
	sypl.New("vs").AddOutputs(output.Console(level.Info)).Infoln("vs created")
	sypl.New("np").AddOutputs(output.Console(level.Info)).Infoln("np created")
	sypl.New("cm").AddOutputs(output.Console(level.Info)).Infoln("cm created")

	os.Unsetenv("SYPL_DEBUG")

}
Output:

pod created
svc created
Example (TextFormatter)

Text formatter example.

package main

import (
	"fmt"
	"strings"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/formatter"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	buf, o := output.SafeBuffer(level.Info)
	o.SetFormatter(formatter.Text())

	// Creates logger, and name it.
	sypl.New(shared.DefaultComponentNameOutput).
		AddOutputs(o).
		PrintlnWithOptions(&options.Options{
			Fields: options.Fields{
				"field1": "value1",
				"field2": "value2",
				"field3": "value3",
			},
		}, level.Info, shared.DefaultContentOutput)

	s := buf.String()

	fmt.Print(
		strings.Contains(s, shared.DefaultContentOutput),
		strings.Contains(s, "field1=value1"),
		strings.Contains(s, "field2=value2"),
		strings.Contains(s, "field3=value3"),
		strings.Contains(s, "component="),
		strings.Contains(s, "level="),
		strings.Contains(s, "timestamp="),
	)

	// Prints:
	//
	// component=componentNameTest level=info field1=value1 field2=value2 field3=value3 timestamp=2021-08-10T22:50:36-07:00

}
Output:

true true true true true true true
Example (UpdateOutputsMaxLevel)

Updating outputs' max levels example.

package main

import (
	"os"

	"github.com/saucelabs/sypl"
	"github.com/saucelabs/sypl/level"
	"github.com/saucelabs/sypl/options"
	"github.com/saucelabs/sypl/output"
	"github.com/saucelabs/sypl/shared"
)

func main() {
	// Creates logger, and name it.
	l := sypl.New(shared.DefaultComponentNameOutput).AddOutputs(
		output.NewOutput("Console 1", level.Info, os.Stdout),
		output.NewOutput("Console 2", level.Debug, os.Stdout),
		output.NewOutput("Console 3", level.Trace, os.Stdout),
	)

	l.PrintlnWithOptions(&options.Options{
		OutputsNames: []string{"Console 1"},
	}, level.Info, l.GetMaxLevel())

	l.SetMaxLevel(level.Info)

	l.PrintlnWithOptions(&options.Options{
		OutputsNames: []string{"Console 1"},
	}, level.Info, l.GetMaxLevel())

}
Output:

map[Console 1:Info Console 2:Debug Console 3:Trace]
map[Console 1:Info Console 2:Info Console 3:Info]

func NewDefault added in v1.3.6

func NewDefault(name string, maxLevel level.Level, processors ...processor.IProcessor) *Sypl

NewDefault creates a logger that covers most of all needs: - Writes message to `stdout` @ the specified `maxLevel` - Writes error messages only to `stderr`

Note: `processors` are applied to both outputs.

func (*Sypl) AddOutputs added in v1.3.2

func (sypl *Sypl) AddOutputs(outputs ...output.IOutput) ISypl

AddOutputs adds one or more outputs.

func (*Sypl) Debug

func (sypl *Sypl) Debug(args ...interface{}) ISypl

Debug prints @ the Debug level.

func (*Sypl) Debugf

func (sypl *Sypl) Debugf(format string, args ...interface{}) ISypl

Debugf prints according with the specified format @ the Debug level.

func (*Sypl) Debugln

func (sypl *Sypl) Debugln(args ...interface{}) ISypl

Debugln prints, also adding a new line to the end @ the Debug level.

func (*Sypl) Debuglnf added in v1.2.0

func (sypl *Sypl) Debuglnf(format string, args ...interface{}) ISypl

Debuglnf prints according with the specified format @ the Debug level, also adding a new line to the end.

func (*Sypl) Error

func (sypl *Sypl) Error(args ...interface{}) ISypl

Error prints @ the Error level.

func (*Sypl) Errorf

func (sypl *Sypl) Errorf(format string, args ...interface{}) ISypl

Errorf prints according with the format @ the Error level.

func (*Sypl) Errorln

func (sypl *Sypl) Errorln(args ...interface{}) ISypl

Errorln prints, also adding a new line to the end @ the Error level.

func (*Sypl) Errorlnf added in v1.2.0

func (sypl *Sypl) Errorlnf(format string, args ...interface{}) ISypl

Errorlnf prints according with the format @ the Error level, also adding a new line to the end.

func (*Sypl) Fatal

func (sypl *Sypl) Fatal(args ...interface{}) ISypl

Fatal prints, and exit with os.Exit(1).

func (*Sypl) Fatalf

func (sypl *Sypl) Fatalf(format string, args ...interface{}) ISypl

Fatalf prints according with the format, and exit with os.Exit(1).

func (*Sypl) Fatalln

func (sypl *Sypl) Fatalln(args ...interface{}) ISypl

Fatalln prints, also adding a new line and the end, and exit with os.Exit(1).

func (*Sypl) Fatallnf added in v1.2.0

func (sypl *Sypl) Fatallnf(format string, args ...interface{}) ISypl

Fatallnf prints according with the format, also adding a new line to the end, and exit with os.Exit(1).

func (*Sypl) GetMaxLevel added in v1.3.8

func (sypl *Sypl) GetMaxLevel() map[string]level.Level

GetMaxLevel returns the `maxLevel` of all outputs.

func (*Sypl) GetName added in v1.1.0

func (sypl *Sypl) GetName() string

GetName returns the sypl name.

func (*Sypl) GetOutput added in v1.1.0

func (sypl *Sypl) GetOutput(name string) output.IOutput

GetOutput returns the registered output by its name. If not found, will be nil.

func (*Sypl) GetOutputs added in v1.1.0

func (sypl *Sypl) GetOutputs() []output.IOutput

GetOutputs returns registered outputs.

func (*Sypl) GetOutputsNames added in v1.3.2

func (sypl *Sypl) GetOutputsNames() []string

GetOutputsNames returns the names of the registered outputs.

func (*Sypl) GetStatus added in v1.3.2

func (sypl *Sypl) GetStatus() status.Status

GetStatus returns the sypl status.

func (*Sypl) Info

func (sypl *Sypl) Info(args ...interface{}) ISypl

Info prints @ the Info level.

func (*Sypl) Infof

func (sypl *Sypl) Infof(format string, args ...interface{}) ISypl

Infof prints according with the specified format @ the Info level.

func (*Sypl) Infoln

func (sypl *Sypl) Infoln(args ...interface{}) ISypl

Infoln prints, also adding a new line to the end @ the Info level.

func (*Sypl) Infolnf added in v1.2.0

func (sypl *Sypl) Infolnf(format string, args ...interface{}) ISypl

Infolnf prints according with the specified format @ the Info level, also adding a new line to the end.

func (*Sypl) New added in v1.3.2

func (sypl *Sypl) New(name string) ISypl

New creates a child logger. The child logger is an accurate, efficient and shallow copy of the parent logger. Changes to internals, such as the state of outputs, and processors, are reflected cross all other loggers.

func (*Sypl) Print

func (sypl *Sypl) Print(l level.Level, args ...interface{}) ISypl

Print just prints.

func (*Sypl) PrintMessage added in v1.3.2

func (sypl *Sypl) PrintMessage(messages ...message.IMessage) ISypl

PrintMessage prints messages. It's a powerful option because it gives full-control over the message. Use `NewMessage` to create the message. it gives full-control over the message. Use `NewMessage` to create the message.

func (*Sypl) PrintMessagesToOutputs added in v1.3.2

func (sypl *Sypl) PrintMessagesToOutputs(messagesToOutputs ...MessageToOutput) ISypl

PrintMessagerPerOutput allows you to concurrently print messages, each one, at the specified level and to the specified output.

Note: If the named output doesn't exits, the message will not be printed.

func (*Sypl) PrintPretty added in v1.1.1

func (sypl *Sypl) PrintPretty(l level.Level, data interface{}) ISypl

PrintPretty prints data structures as JSON text.

Notes: - Only exported fields of the data structure will be printed. - Message isn't processed.

func (*Sypl) PrintWithOptions added in v1.1.0

func (sypl *Sypl) PrintWithOptions(o *options.Options, l level.Level, args ...interface{}) ISypl

PrintWithOptions is a more flexible way of printing, allowing to specify a few message's options. For full-control over the message is possible via `PrintMessage`.

func (*Sypl) Printf

func (sypl *Sypl) Printf(l level.Level, format string, args ...interface{}) ISypl

Printf prints according with the specified format.

func (*Sypl) PrintfWithOptions added in v1.1.0

func (sypl *Sypl) PrintfWithOptions(o *options.Options, l level.Level, format string, args ...interface{}) ISypl

PrintfWithOptions prints according with the specified format. It's a more flexible way of printing, allowing to specify a few message's options. For full-control over the message is possible via `PrintMessage`.

func (*Sypl) Println

func (sypl *Sypl) Println(l level.Level, args ...interface{}) ISypl

Println prints, also adding a new line to the end.

func (*Sypl) PrintlnPretty added in v1.1.1

func (sypl *Sypl) PrintlnPretty(l level.Level, data interface{}) ISypl

PrintlnPretty prints data structures as JSON text, also adding a new line to the end.

Notes: - Only exported fields of the data structure will be printed. - Message isn't processed.

func (*Sypl) PrintlnWithOptions added in v1.1.0

func (sypl *Sypl) PrintlnWithOptions(o *options.Options, l level.Level, args ...interface{}) ISypl

PrintfWithOptions prints, also adding a new line to the end. It's a more flexible way of printing, allowing to specify a few message's options. For full-control over the message is possible via `PrintMessage`.

func (*Sypl) Printlnf added in v1.2.0

func (sypl *Sypl) Printlnf(l level.Level, format string, args ...interface{}) ISypl

Printlnf prints according with the specified format, also adding a new line to the end.

func (*Sypl) PrintlnfWithOptions added in v1.3.2

func (sypl *Sypl) PrintlnfWithOptions(o *options.Options, l level.Level, format string, args ...interface{}) ISypl

PrintfWithOptions prints according with the specified format, also adding a new line to the end. It's a more flexible way of printing, allowing to specify a few message's options. For full-control over the message is possible via `PrintMessage`.

func (*Sypl) Serror added in v1.2.3

func (sypl *Sypl) Serror(args ...interface{}) error

Serror prints like Error, and returns an error with the non-processed content.

func (*Sypl) Serrorf added in v1.2.3

func (sypl *Sypl) Serrorf(format string, args ...interface{}) error

Serrorf prints like Errorf, and returns an error with the non-processed content.

func (*Sypl) Serrorln added in v1.2.3

func (sypl *Sypl) Serrorln(args ...interface{}) error

Serrorln prints like Errorln, and returns an error with the non-processed content.

func (*Sypl) Serrorlnf added in v1.2.3

func (sypl *Sypl) Serrorlnf(format string, args ...interface{}) error

Serrorlnf prints like Errorlnf, and returns an error with the non-processed content.

func (*Sypl) SetMaxLevel added in v1.3.8

func (sypl *Sypl) SetMaxLevel(l level.Level)

SetMaxLevel sets the `maxLevel` of all outputs.

func (*Sypl) SetName added in v1.3.2

func (sypl *Sypl) SetName(name string)

SetName sets the sypl name.

func (*Sypl) SetOutputs added in v1.3.2

func (sypl *Sypl) SetOutputs(outputs ...output.IOutput)

SetOutputs sets one or more outputs. Use to update output(s).

func (*Sypl) SetStatus added in v1.3.2

func (sypl *Sypl) SetStatus(s status.Status)

SetStatus sets the sypl status.

func (Sypl) String added in v1.3.2

func (sypl Sypl) String() string

String interface implementation.

func (*Sypl) Trace

func (sypl *Sypl) Trace(args ...interface{}) ISypl

Trace prints @ the Trace level.

func (*Sypl) Tracef

func (sypl *Sypl) Tracef(format string, args ...interface{}) ISypl

Tracef prints according with the specified format @ the Trace level.

func (*Sypl) Traceln

func (sypl *Sypl) Traceln(args ...interface{}) ISypl

Traceln prints, also adding a new line to the end @ the Trace level.

func (*Sypl) Tracelnf added in v1.2.0

func (sypl *Sypl) Tracelnf(format string, args ...interface{}) ISypl

Tracelnf prints according with the specified format @ the Trace level, also adding a new line to the end.

func (*Sypl) Warn

func (sypl *Sypl) Warn(args ...interface{}) ISypl

Warn prints @ the Warn level.

func (*Sypl) Warnf

func (sypl *Sypl) Warnf(format string, args ...interface{}) ISypl

Warnf prints according with the specified format @ the Warn level.

func (*Sypl) Warnln

func (sypl *Sypl) Warnln(args ...interface{}) ISypl

Warnln prints, also adding a new line to the end @ the Warn level.

func (*Sypl) Warnlnf added in v1.2.0

func (sypl *Sypl) Warnlnf(format string, args ...interface{}) ISypl

Warnlnf prints according with the specified format @ the Warn level, also adding a new line to the end.

Directories

Path Synopsis
internal
builtin
Package log implements a simple logging package.
Package log implements a simple logging package.

Jump to

Keyboard shortcuts

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