debug

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 8 Imported by: 2 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

debug-log

GoDoc GitHub license

The debug-log Go package (imported as debug) provides a thin logger inspired by the NPM's debug package. It outputs the log messages depending on the DEBUG environment variable.

Example of usage:

package main

import (
	"os"

	"github.com/davidmz/debug-log"
)

func main() {
	_ = os.Setenv("DEBUG", "myproject:*")
	testLogger := debug.NewLogger("myproject:test")

	testLogger.Println("log line")
	// Output: TIMESTAMP [myproject:test] log line
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvSourceFunc

type EnvSourceFunc func() string

EnvSourceFunc is a type of function that returns a DEBUG value. Use it with UseEnvSource option if you don't want to use standard UseEnvVar option.

type Logger

type Logger interface {
	// Print writes a message via the fmt.Sprint
	Print(v ...interface{})
	// Print writes a message via the fmt.Sprintf
	Printf(format string, v ...interface{})
	// Print writes a message via the fmt.Sprintln
	Println(v ...interface{})
	// Returns logger name
	Name() string
	// Output returns the underlying writer of the logger
	Output() io.Writer
	// Fork creates a new Logger with the same options but with a new name
	Fork(name string, options ...Option) Logger
}

Logger is a minimal logger interface.

func NewLogger

func NewLogger(name string, options ...Option) Logger

NewLogger creates a new named logger with the given options. If no options are provided, the following defaults are used: WithOutput(os.Stdout) and UseEnvVar("DEBUG").

Example
package main

import (
	"os"

	"github.com/davidmz/debug-log"
)

func main() {
	_ = os.Setenv("DEBUG", "test")
	logger := debug.NewLogger("test", debug.WithoutTime())

	logger.Println("log line")
}
Output:
[test] log line
Example (UseEnvString)
package main

import (
	"github.com/davidmz/debug-log"
)

func main() {
	logger := debug.NewLogger("test", debug.WithoutTime(), debug.UseEnvString("test"))

	logger.Println("log line")
}
Output:
[test] log line

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option allows to configure logger. Use them in NewLogger as arguments.

func UseEnvSource

func UseEnvSource(src EnvSourceFunc) Option

UseEnvSource allows to use custom source of DEBUG value. Use UseEnvVar to obtain DEBUG from the environment variable.

func UseEnvString added in v1.2.0

func UseEnvString(envString string) Option

UseEnvString sets the constant string as the DEBUG value.

func UseEnvVar

func UseEnvVar(name string) Option

UseEnvVar allow logger to use the given environment variable as DEBUG value.

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput sets the output writer for the logger.

func WithPrefix added in v1.3.0

func WithPrefix(prefix string) Option

WithPrefix sets the prefix for the logger.

func WithoutTime

func WithoutTime() Option

WithoutTime excludes the timestamps from log output.

type Options

type Options []Option

Options is just a list of Option. It implements the Option interface by itself.

Jump to

Keyboard shortcuts

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