logger

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: MIT Imports: 2 Imported by: 2

README

logger

lint

Go structured logger compatible with stackdriver build with zap

Installation

go get -u bitbucket.org/labpatoscedro/logger

Quick Start

log := logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel)
defer log.Sync()

log.Debug("debug log", "key", "value")
log.Info("info log", "key", "value")
log.Warn("warn log", "key", "value")
log.Error("error log", "key", "value", "err", errors.New("meu erro!!"))
log.DPanic("d-panic log", "key", "value")
log.Panic("panic log", "key", "value")
log.Fatal("fatal log", "key", "value")

Dependencies

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoding

type Encoding uint8

Encoding specifies the logging output formatter: console or json

const (
	ConsoleEncoding Encoding = iota
	JSONEncoding
)

type Level

type Level uint8

Level is the logging priority. Higher levels are more important.

const (
	// DebugLevel are typically used on development environemt or for debug purpose.
	// Is more voluminous and are usually disabled in production.
	DebugLevel Level = iota

	// InfoLevel is the default logging priority.
	InfoLevel

	// WarnLevel logs are more important than Info, but not critical
	WarnLevel

	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel

	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel

	// PanicLevel logs a message, then panics.
	PanicLevel

	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

type Logger

type Logger interface {
	// Log logs a message with the given Level.
	// The variadic key-value pairs are treated as they are in With.
	Log(level Level, msg string, keyAndValues ...interface{})

	// Debug logs a message with some additional context.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes log severity as 'DEBUG'
	Debug(msg string, keyAndValues ...interface{})

	// Info logs a message with some additional context.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes log severity as 'INFO'
	Info(msg string, keysAndValues ...interface{})

	// Warn logs a message with some additional context.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes log severity as 'WARNING'
	Warn(msg string, keyAndValues ...interface{})

	// Error logs a message with some additional context.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes log severity as 'ERROR' and includes the stacktrace.
	Error(msg string, keyAndValues ...interface{})

	// DPanic logs a message with some additional context.
	// In development, the logger then panics. (See DPanicLevel for details.)
	// The variadic key-value pairs are treated as they are in With.
	// Assumes log severity as 'CRITICAL' and includes the stacktrace.
	DPanic(msg string, keysAndValues ...interface{})

	// Panic logs a message with some additional context, then panics.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes severity as 'ALERT' and includes the stacktrace.
	Panic(msg string, keysAndValues ...interface{})

	// Fatal logs a message with some additional context, then calls os.Exit.
	// The variadic key-value pairs are treated as they are in With.
	// Assumes severity as 'EMERGENCY' and includes the stacktrace.
	Fatal(msg string, keysAndValues ...interface{})

	// Sync flushes any buffered log entries.
	Sync()
}
Example
package main

import (
	"bitbucket.org/labpatoscedro/logger"
)

func main() {
	log := logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel)
	defer log.Sync()

	log.Debug("my log message")
	log.Debug("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.Info("my log message")
	log.Info("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.Warn("my log message")
	log.Warn("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.Error("my log message")
	log.Error("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.DPanic("my log message")
	log.DPanic("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.Panic("my log message")
	log.Panic("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})

	log.Fatal("my log message")
	log.Fatal("my log message",
		"key1", "value 1",
		"key2", 1,
		"key3", 1.2,
		"key4", true,
		"key5", map[string]interface{}{
			"key":         "value",
			"another_key": 1,
		})
}
Output:

func New

func New() Logger

New returns a new logger with json encoding and default info level.

Example
package main

import (
	"bitbucket.org/labpatoscedro/logger"
)

func main() {
	log := logger.New()
	defer log.Sync()

	log.Info("info message")
}
Output:

func NewLogger

func NewLogger(encoding Encoding) Logger

NewLogger returns a new logger with the given encoding(see Encoding for details) and default info level.

Example
package main

import (
	"bitbucket.org/labpatoscedro/logger"
)

func main() {
	jsonLogger := logger.NewLogger(logger.JSONEncoding)
	defer jsonLogger.Sync()

	jsonLogger.Info("info message")

	consoleLogger := logger.NewLogger(logger.ConsoleEncoding)
	defer consoleLogger.Sync()

	consoleLogger.Info("info message")
}
Output:

func NewLoggerWithLevel

func NewLoggerWithLevel(encoding Encoding, level Level) Logger

NewLoggerWithLevel returns a new logger with the given encoding(see Encoding for details) and level (see Level for details).

Example
package main

import (
	"bitbucket.org/labpatoscedro/logger"
)

func main() {
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.InfoLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.WarnLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.ErrorLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DPanicLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.PanicLevel)
	_ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.FatalLevel)

	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.DebugLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.InfoLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.WarnLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.ErrorLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.DPanicLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.PanicLevel)
	_ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.FatalLevel)
}
Output:

func Nil added in v1.1.0

func Nil() Logger

Nil returns a dumb logger instance which does nothing. Useful for a safe logger initialization.

Example
package main

import (
	"bitbucket.org/labpatoscedro/logger"
)

func main() {
	log := logger.Nil()

	log.Info("silent info message")
	log.Debug("silent debug message")
}
Output:

Jump to

Keyboard shortcuts

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