golog

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2020 License: MIT Imports: 6 Imported by: 27

README

Go Log Go Doc

A simple Go logging package.

Table Of Contents

Overview

Go Log is a simple logging library inspired by the standard log package.

It was created because there was a lack of simple easy to use logging libraries in the Go ecosystem.

Features:

  • 1 line setup
  • Log levels: Debug, Info, Warn, Error, Fatal
  • Log output formatting via Go templates
  • Function names similar to log package
  • Advanced customizability if needed

See the GoDoc page for detailed usage instructions

Documentation

Overview

Go Log is a simple to use logging library inspired by the standard `log` package.

It provides the: Debug, Info, Warn, Error, and Fatal log levels.

Go Log is easy to setup:

// logger will print normal messages to stdout and errors to stderr
logger := golog.NewLogger("example")

The logging API should be familiar to those who have used the standard `fmt` and `log` packages.

logger.Debug("hello debug")
logger.Debugf("hello %s", "debug")

logger.Info("hello info")
logger.Infof("hello %s", "info")

logger.Warn("hello warn")
logger.Warnf("hello %s", "warn")

logger.Error("hello error")
logger.Errorf("hello %s", "error")

logger.Fatal("hello fatal")
logger.Fatalf("hello %s", "fatal")

You can configure Go Log to only show messages of certain importance

logger.SetLevel(golog.DebugLevel)

logger.SetLevel(golog.InfoLevel)

logger.SetLevel(golog.WarnLevel)

logger.SetLevel(golog.ErrorLevel)

logger.SetLevel(golog.FatalLevel)

Log output format can be configured with Go templates

logger.SetFormatTmpl("name={{ .Name }} level={{ .Level }} msg={{ .Msg }}\n")

Child loggers can be created

child := logger.GetChild("child")
Example (Basic)

Shows how to use Go Log with no customization.

package main

import (
	"github.com/Noah-Huppert/golog"
)

func main() {
	// logger will print normal messages to stdout and errors to stderr
	logger := golog.NewLogger("basic-example")

	// Log messages for each log level
	logger.Debug("hello debug")
	logger.Debugf("hello %s", "debug")

	logger.Info("hello info")
	logger.Infof("hello %s", "info")

	logger.Warn("hello warn")
	logger.Warnf("hello %s", "warn")

	logger.Error("hello error")
	logger.Errorf("hello %s", "error")

	logger.Fatal("hello fatal")
	logger.Fatalf("hello %s", "fatal")

}
Output:

basic-example [DEBUG] hello debug
basic-example [DEBUG] hello debug
basic-example [INFO] hello info
basic-example [INFO] hello info
basic-example [WARN] hello warn
basic-example [WARN] hello warn
basic-example [ERROR] hello error
basic-example [ERROR] hello error
basic-example [FATAL] hello fatal
basic-example [FATAL] hello fatal
Example (Child)

Shows how to use the GetChild method

package main

import (
	"github.com/Noah-Huppert/golog"
)

func main() {
	// logger will print normal messages to stdout and errors to stderr
	logger := golog.NewLogger("get-child-example")

	// Log messages with the parent logger
	logger.Debug("hello debug")

	// Create a child logger
	child := logger.GetChild("child")

	// Log messages with the child logger
	child.Debug("hello debug")

}
Output:

get-child-example [DEBUG] hello debug
get-child-example.child [DEBUG] hello debug
Example (Format)

Shows how to customize the log output format

package main

import (
	"github.com/Noah-Huppert/golog"
)

func main() {
	// Configure logger with special format
	logger := golog.NewLogger("format-example")
	logger.SetFormatTmpl("name={{ .Name }} level={{ .Level }} msg={{ .Msg }}\n")

	// Log messages for each log level
	logger.Debug("hello debug")
	logger.Debugf("hello %s", "debug")

	logger.Info("hello info")
	logger.Infof("hello %s", "info")

	logger.Warn("hello warn")
	logger.Warnf("hello %s", "warn")

	logger.Error("hello error")
	logger.Errorf("hello %s", "error")

	logger.Fatal("hello fatal")
	logger.Fatalf("hello %s", "fatal")

}
Output:

name=format-example level=DEBUG msg=hello debug
name=format-example level=DEBUG msg=hello debug
name=format-example level=INFO msg=hello info
name=format-example level=INFO msg=hello info
name=format-example level=WARN msg=hello warn
name=format-example level=WARN msg=hello warn
name=format-example level=ERROR msg=hello error
name=format-example level=ERROR msg=hello error
name=format-example level=FATAL msg=hello fatal
name=format-example level=FATAL msg=hello fatal
Example (Levels)

Shows how to only display messages from certain log levels

package main

import (
	"github.com/Noah-Huppert/golog"
)

func main() {
	// Configure logger to only display error messages or greater
	logger := golog.NewLogger("levels-example")
	logger.SetLevel(golog.ErrorLevel)

	// Log messages which will not be shown because they are below the
	// specified log level
	logger.Debug("I will not be shown b/c I am a debug message")
	logger.Info("I am just an info message show I will not be shown")
	logger.Warn("Due to the fact that I am a warn message I will not be displayed")

	// Log message that will show
	logger.Error("Error log messages will show")
	logger.Fatal("I am a fatal message so I will be displayed")

}
Output:

levels-example [ERROR] Error log messages will show
levels-example [FATAL] I am a fatal message so I will be displayed

Index

Examples

Constants

View Source
const DebugLevel int = 60

DebugLevel is used to identify the DEBUG log level and compare it to other levels

View Source
const DebugLevelName string = "DEBUG"

DebugLevelName is the name of the DEBUG log level

View Source
const DefaultLogFmt string = "{{ .Name }} [{{ .Level }}] {{ .Msg }}\n"

DefaultLogFmt is the default log format template

View Source
const ErrorLevel int = 90

ErrorLevel is used to identify the ERROR log level and compare it to other levels

View Source
const ErrorLevelName string = "ERROR"

ErrorLevelName is the name of the ERROR log level

View Source
const FatalLevel int = 100

FatalLevel is used to identify the FATAL log level and compare it to other levels

View Source
const FatalLevelName string = "FATAL"

FatalLevelName is the name of the FATAL log level

View Source
const InfoLevel int = 70

InfoLevel is used to identify the INFO log level and compare it to other levels

View Source
const InfoLevelName string = "INFO"

InfoLevelName is the name of the INFO log level

View Source
const WarnLevel int = 80

WarnLevel is used to identify the WARN log level and compare it to other levels

View Source
const WarnLevelName string = "WARN"

WarnLevelName is the name of the WARN log level

Variables

Levels maps level priorities to level names

Functions

func MustExecTmpl

func MustExecTmpl(t *template.Template, data interface{}) string

MustExecTmpl executes a Go template and returns the result. Panics if an error occurs.

func MustMkTmpl

func MustMkTmpl(tmpl string) *template.Template

MustMkTmpl creates a Go template. Panics if an error occurs.

Types

type BaseLogger

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

BaseLogger formats and outputs log messages. It implements some of the basic functionality that all loggers must provide.

func NewBaseLogger

func NewBaseLogger(name string) *BaseLogger

NewBaseLogger creates a new BaseLogger instance

func (*BaseLogger) SetFormatTmpl

func (l *BaseLogger) SetFormatTmpl(tmpl string)

func (*BaseLogger) SetLevel

func (l *BaseLogger) SetLevel(level int)

func (*BaseLogger) SetName

func (l *BaseLogger) SetName(name string)

type LogMsgCtx

type LogMsgCtx struct {
	// Name is the identifier of the logger
	Name string

	// Level is the name of the message log level
	Level string

	// Msg is the log message contents
	Msg string
}

LogMsgCtx holds information about a log message

func NewLogMsgCtx

func NewLogMsgCtx(name, lvl, msg string) LogMsgCtx

NewLogMsgCtx creates a new LogMsgCtx

type Logger

type Logger interface {
	// SetName sets the logger's identifying name
	SetName(name string)

	// SetLevel sets the minimum log level which will be outputted
	SetLevel(level int)

	// SetFormatTmpl defines a Go template used to format log output.
	// Fields from the LogMsgCtx struct are available to use in the
	// template.
	SetFormatTmpl(tmpl string)

	// GetChild creates a new logger who's name is the original logger's
	// name and the provided name combined
	GetChild(name string) Logger

	// Fatal writes at the FATAL level and panics the process
	Fatal(data ...interface{})

	// Fatalf formats a string, writes at the FATAL level, and panics the
	// process
	Fatalf(format string, data ...interface{})

	// Error writes at the ERROR level
	Error(data ...interface{})

	// Errorf formats a string and writes at the ERROR level
	Errorf(format string, data ...interface{})

	// Warn writes at the WARN level
	Warn(data ...interface{})

	// Warnf formats a string and writes at the WARN level
	Warnf(format string, data ...interface{})

	// Info writes at the INFO level
	Info(data ...interface{})

	// Infof formats a string and writes at the INFO level
	Infof(format string, data ...interface{})

	// Debug writes at the DEBUG level
	Debug(data ...interface{})

	// Debugf formats a string and writes at the DEBUG level
	Debugf(format string, data ...interface{})
}

Logger defines methods for outputting information

type WriterLogger

type WriterLogger struct {
	// BaseLogger is used to provide the basic functionality of a Logger
	*BaseLogger

	// FatalWriter is used to output FATAL level log messages
	FatalWriter io.Writer

	// ErrorWriter is used to output ERROR level log messages
	ErrorWriter io.Writer

	// WarnWriter is used to output WARN level log messages
	WarnWriter io.Writer

	// InfoWriter is used to output INFO level log messages
	InfoWriter io.Writer

	// DebugWriter is used to output DEBUG level log messages
	DebugWriter io.Writer
}

WriterLogger implements the Logger interface using different io.Writers for the different log levels.

func NewLogger

func NewLogger(name string) *WriterLogger

NewLogger creates a WriterLogger which outputs normal messages to stdout and error messages to stderr

func NewStdLogger

func NewStdLogger(name string) *WriterLogger

NewStdLogger calls NewLogger, kept for compatibility

func NewWriterLogger

func NewWriterLogger(name string, fatalWriter io.Writer, errorWriter io.Writer,
	warnWriter io.Writer, infoWriter io.Writer,
	debugWriter io.Writer) *WriterLogger

NewWriterLogger creates a new WriterLogger

func (WriterLogger) Debug

func (l WriterLogger) Debug(data ...interface{})

func (WriterLogger) Debugf

func (l WriterLogger) Debugf(format string, data ...interface{})

func (WriterLogger) Error

func (l WriterLogger) Error(data ...interface{})

func (WriterLogger) Errorf

func (l WriterLogger) Errorf(format string, data ...interface{})

func (WriterLogger) Fatal

func (l WriterLogger) Fatal(data ...interface{})

func (WriterLogger) Fatalf

func (l WriterLogger) Fatalf(format string, data ...interface{})

func (WriterLogger) GetChild

func (l WriterLogger) GetChild(child string) Logger

GetChild implements Logger.GetChild

func (WriterLogger) Info

func (l WriterLogger) Info(data ...interface{})

func (WriterLogger) Infof

func (l WriterLogger) Infof(format string, data ...interface{})

func (WriterLogger) Warn

func (l WriterLogger) Warn(data ...interface{})

func (WriterLogger) Warnf

func (l WriterLogger) Warnf(format string, data ...interface{})

Jump to

Keyboard shortcuts

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