log

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: ISC Imports: 7 Imported by: 0

README

Go Doc Build Status Go Report Card Test Coverage Maintainability

log

This package can be used for production-ready logging in Go applications. It hides the complexity of configuring and using the state-of-the-arts loggers by providing a single interface that is easy-to-use and hard-to-misuse!

Quick Start

You can either log using an instance logger or the singleton logger. After creating an instance logger, you need to set the singleton logger using the SetSingleton method once. The instance logger can be further used to create more contextualized loggers as the children of the root logger.

zap
package main

import "github.com/moorara/log"

func main() {
  // Creating a zap logger
  logger := log.NewZap(log.Options{
    Name:        "my-service",
    Version:     "0.1.0",
    Environment: "production",
    Region:      "us-east-1",
    Tags: map[string]string{
      "domain": "auth",
    },
  })

  // Initializing the singleton logger
  log.SetSingleton(logger)

  // Logging using the singleton logger
  log.Infof("starting server on port %d ...", 8080)

  // Logging using the instance logger
  logger.Info("request received.",
    "tenantId", "aaaaaaaa",
    "requestId", "bbbbbbbb",
  )
}

Output logs from stdout:

{"level":"info","timestamp":"2020-04-24T12:39:04.506116-04:00","caller":"example/main.go:21","message":"starting server on port 8080 ...","domain":"auth","environment":"production","logger":"my-service","region":"us-east-1","version":"0.1.0"}
{"level":"info","timestamp":"2020-04-24T12:39:04.506268-04:00","caller":"example/main.go:24","message":"request received.","domain":"auth","environment":"production","logger":"my-service","region":"us-east-1","version":"0.1.0","tenantId":"aaaaaaaa","requestId":"bbbbbbbb"}
go-kit
package main

import "github.com/moorara/log"

func main() {
  // Creating a kit logger
  logger := log.NewKit(log.Options{
    Name:        "my-service",
    Version:     "0.1.0",
    Environment: "production",
    Region:      "us-east-1",
    Tags: map[string]string{
      "domain": "auth",
    },
  })

  // Initializing the singleton logger
  log.SetSingleton(logger)

  // Logging using the singleton logger
  log.Infof("starting server on port %d ...", 8080)

  // Logging using the instance logger
  logger.Info("request received.",
    "tenantId", "aaaaaaaa",
    "requestId", "bbbbbbbb",
  )
}

Output logs from stdout:

{"caller":"main.go:21","domain":"auth","environment":"production","level":"info","logger":"my-service","message":"starting server on port 8080 ...","region":"us-east-1","timestamp":"2020-04-24T12:39:53.05221-04:00","version":"0.1.0"}
{"caller":"main.go:24","domain":"auth","environment":"production","level":"info","logger":"my-service","message":"request received.","region":"us-east-1","requestId":"bbbbbbbb","tenantId":"aaaaaaaa","timestamp":"2020-04-24T12:39:53.052529-04:00","version":"0.1.0"}

Documentation

Overview

Package log provides leveled structured loggers. It hides the complexity of configuring and using the state-of-the-arts loggers by providing a single interface.

You can either log using an instance logger or the singleton logger. After creating an instance logger, you need to set the singleton logger using the SetSingleton method once. The instance logger can be further used to create more contextualized loggers as the children of the root logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close flushes the singleton logger.

func Debug

func Debug(message string, kv ...interface{})

Debug logs a message and a list of key-value pairs in debug level using the singleton logger.

func Debugf

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

Debugf formats and logs a message in debug level using the singleton logger. It uses fmt.Sprintf() to log a message.

func Error

func Error(message string, kv ...interface{})

Error logs a message and a list of key-value pairs in error level using the singleton logger.

func Errorf

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

Errorf formats and logs a message in error level using the singleton logger. It uses fmt.Sprintf() to log a message.

func Info

func Info(message string, kv ...interface{})

Info logs a message and a list of key-value pairs in info level using the singleton logger.

func Infof

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

Infof formats and logs a message in info level using the singleton logger. It uses fmt.Sprintf() to log a message.

func SetLevel

func SetLevel(level string)

SetLevel changes the logging level of the singleton logger.

func SetSingleton

func SetSingleton(l Logger)

SetSingleton updates the singleton logger.

func Warn

func Warn(message string, kv ...interface{})

Warn logs a message and a list of key-value pairs in warn level using the singleton logger.

func Warnf

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

Warnf formats and logs a message in warn level using the singleton logger. It uses fmt.Sprintf() to log a message.

Types

type Format

type Format int

Format is the logging format.

const (
	FormatJSON Format = iota
	FormatConsole
)

Logging format

type Level

type Level int

Level is the logging level.

const (
	LevelNone Level = iota
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
)

Logging level

func GetLevel

func GetLevel() Level

GetLevel returns the current logging level of the singleton logger.

type Logger

type Logger interface {
	With(kv ...interface{}) Logger
	GetLevel() Level
	SetLevel(level string)
	Debug(message string, kv ...interface{})
	Debugf(format string, args ...interface{})
	Info(message string, kv ...interface{})
	Infof(format string, args ...interface{})
	Warn(message string, kv ...interface{})
	Warnf(format string, args ...interface{})
	Error(message string, kv ...interface{})
	Errorf(format string, args ...interface{})
	Close() error
}

Logger is a leveled structured logger. It is concurrently safe to be used by multiple goroutines.

func NewKit

func NewKit(opts Options) Logger

NewKit creates a new logger based on go-kit logger.

func NewNopLogger

func NewNopLogger() Logger

NewNopLogger creates a logger that never logs anything to anywhere! It can be used for testing purposes.

func NewZap

func NewZap(opts Options) Logger

NewZap creates a new logger based on zap logger.

type Options

type Options struct {
	Name        string
	Version     string
	Environment string
	Region      string
	Tags        map[string]string
	Level       string
	Format      Format
}

Options are optional configurations for creating a logger. Level can be "debug", "info", "warn", "error", or "none" (case-insensitive).

Jump to

Keyboard shortcuts

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