log

package module
v0.0.0-...-dca3c8e Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: MIT Imports: 5 Imported by: 1

README

log

GoDoc Go Report Card cover.run Build Status

Simple drop-in replacement for golang log package which adds log levels and better log message formatting.

Install
$ go get -u -v -x github.com/akamensky/go-log
Usage example
// TODO: Rewrite example
License

Read license here

Documentation

Overview

Package log provides drop-in replacement for standard log package. It adds log levels, io.Writer interface for output and named loggers with possibility of separate output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg ...interface{})

Debug is a logging method that will write to default logger with DEBUG level

func Debugf

func Debugf(format string, a ...interface{})

Debugf is just like Debug, but with formatting

func Error

func Error(msg ...interface{})

Error is a logging method that will write to default logger with Error level

func Errorf

func Errorf(format string, a ...interface{})

Errorf is just like Error, but with formatting

func Fatal

func Fatal(msg ...interface{})

Fatal is a logging method that will write to default logger with Fatal level, and then terminate program with exist code 1

func Fatalf

func Fatalf(format string, a ...interface{})

Fatalf is just like Fatal, but with formatting

func Info

func Info(msg ...interface{})

Info is a logging method that will write to default logger with INFO level

func Infof

func Infof(format string, a ...interface{})

Infof is just like Info, but with formatting

func Log

func Log(lvl Level, msg ...interface{})

Log is a generic method that will write to default logger and can accept custom logging levels

func Logf

func Logf(lvl Level, format string, a ...interface{})

Logf is just like Log, but with formatting

func SetFormat

func SetFormat(format string)

SetFormat sets logging format for default logger. Any other loggers that do not overwrite format will inherit it as well. It takes a string that is processed by text/template with required values in template:

{{.Timestamp}} is a Timestamp format for which is set via SetTimeFormat {{.Namespace}} is a namespace of logger, default logger namespace is "main" {{.Level}} is a logging Level for current record {{.Message}} is actual Message contents

if passed format cannot be parsed the function panics.

func SetHandler

func SetHandler(w io.Writer)

SetHandler sets output for default logger

func SetLevel

func SetLevel(level Level)

SetLevel sets logging level where logging output will include all levels lower than currently set default level is INFO

func SetTimeFormat

func SetTimeFormat(format string)

SetTimeFormat follows standard Golang time formatting for default logger

func Warn

func Warn(msg ...interface{})

Warn is a logging method that will write to default logger with WARNING level

func Warnf

func Warnf(format string, a ...interface{})

Warnf is just like Warn, but with formatting

Types

type Level

type Level int

Level is a builtin type which refers to logging level actually an int under the hood for easy level comparison

const (
	DISABLED Level = -1 // Logging disabled. Like completely
	FATAL    Level = 0  // Show errors and fatal errors
	ERROR    Level = 10 // Show errors and fatal errors
	WARNING  Level = 20 // Show warnings and errors
	INFO     Level = 30 // Show information messages and everything higher than that
	DEBUG    Level = 40 // Show all including debug messages
)

It is int so that you can add your own levels and use them When to include message or not defined by simple comparison Current log level should be more or equal to the message level for it to be included

type NamedLogger

type NamedLogger interface {
	// Log is a generic method that will write
	// to named logger and can accept custom
	// logging levels
	Log(lvl Level, msg ...interface{})
	// Logf is just like Log, but with formatting
	Logf(lvl Level, format string, a ...interface{})

	// Debug is a logging method that will write
	// to named logger with DEBUG level
	Debug(msg ...interface{})
	// Debugf is just like Debug, but with formatting
	Debugf(format string, a ...interface{})

	// Info is a logging method that will write
	// to named logger with INFO level
	Info(msg ...interface{})
	// Infof is just like Info, but with formatting
	Infof(format string, a ...interface{})

	// Warn is a logging method that will write
	// to named logger with WARNING level
	Warn(msg ...interface{})
	// Warnf is just like Warn, but with formatting
	Warnf(format string, a ...interface{})

	// Error is a logging method that will write
	// to named logger with ERROR level
	Error(msg ...interface{})
	// Errorf is just like Error, but with formatting
	Errorf(format string, a ...interface{})

	// Fatal is a logging method that will write
	// to named logger with FATAL level and
	// terminate program with exit code 1
	Fatal(msg ...interface{})
	// Fatalf is just like Fatal, but with formatting
	Fatalf(format string, a ...interface{})

	// SetFormat sets logging format for named logger.
	// It takes a string that is processed by text/template
	// with required values in template:
	//
	// {{.Timestamp}} is a Timestamp format for which is set via SetTimeFormat
	// {{.Namespace}} is a namespace of logger, default logger namespace is "main"
	// {{.Level}} is a logging Level for current record
	// {{.Message}} is actual Message contents
	//
	// If passed format cannot be parsed the function panics.
	SetFormat(format string) NamedLogger

	// SetTimeFormat is a method to set timestamp formatting for
	// named logger separately from other loggers.
	// Returns itself for easy chaining.
	SetTimeFormat(format string) NamedLogger

	// SetTimeFormat is a method to set output for
	// named logger separately from other loggers.
	// Returns itself for easy chaining.
	SetOutput(w io.Writer) NamedLogger
}

NamedLogger is a named logger where each part of application can write to their own named logger, with all having different level, output, format etc set

func GetNamedLogger

func GetNamedLogger(namespace string) NamedLogger

GetNamedLogger will return existing named logger or create new one if it does not exist yet namespace will be used in output format. See SetFormat()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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