simplog

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: MIT Imports: 4 Imported by: 2

README

simplog logo

GitHub tag (latest SemVer) codecov Go Report Card Codacy Badge Maintainability go.dev reference

A simple and opinionated library that lets you set up and use zap quickly.

About

Simplog is a small library that sets zap up in a way that is easy to use and provides some additional features. Simplog is opinionated and tries to provide a good default configuration for most use cases. If you need more features, you may use the resulting zap logger directly.

Features

  • Client and server modes
  • Visualized log levels
  • Optional context-binding support
  • Easy to use

Install

go get -u github.com/nikoksr/simplog

 

Example Code

For more examples, see the examples directory.

package main

import "github.com/nikoksr/simplog"

func main() {
  // Using the manual configuration; alternatively you can use NewClientLogger() or NewServerLogger().
  logger := simplog.NewWithOptions(&simplog.Options{
    Debug:             false,
    IsServer:          true,
  })

  // At this point, you're using a zap.SugaredLogger and can use it as you would normally do.
  logger.Info("You're awesome!")
  logger.Warn("Coffee is almost empty!")
  logger.Error("Unable to operate, caffein levels too low.")
}

 

Example Outputs

Client & server mode in debug

In debug mode, independent of the mode, the logger will print all messages greater-equal than the debug-level in a human readable format.

2022-10-23T14:25:15.537+0200	INFO	simplog	test-simplog/main.go:12	You're awesome!
2022-10-23T14:25:15.537+0200	WARN	simplog	test-simplog/main.go:13	Coffee is almost empty!
2022-10-23T14:25:15.537+0200	ERROR	simplog	test-simplog/main.go:14	Unable to operate, caffein levels too low.

 

Client mode in production

In production mode, the client logger will print all messages greater-equal than the info-level in a human readable format and replace the log level with a colored emoji.

The symbols are configurable and can be set to any string.

💡 You're awesome!
⚠️ Coffee is almost empty!
🔥 Unable to operate, caffein levels too low.

 

Server mode in production

In production mode, the server logger will print all messages greater-equal than the info-level in a structured format.

{"level":"info","ts":1666528089.4873903,"logger":"simplog","caller":"test-simplog/main.go:12","msg":"You're awesome!"}
{"level":"warn","ts":1666528089.4874253,"logger":"simplog","caller":"test-simplog/main.go:13","msg":"Coffee is almost empty!"}
{"level":"error","ts":1666528089.487434,"logger":"simplog","caller":"test-simplog/main.go:14","msg":"Unable to operate, caffein levels too low."}

 

Credits

Documentation

Overview

Package simplog provides a simple logging library for Go. It is factually not a logger itself, but rather helps you by providing simple and opinionated ways quickly set up uber-go/zap as a logger.

Usage:

package main

import "github.com/nikoksr/simplog"

func main() {
  // Using the manual configuration; alternatively you can use NewClientLogger() or NewServerLogger().
  logger := simplog.NewWithOptions(&simplog.Options{
    Debug:             false,
    IsServer:          true,
  })

  // At this point, you're using a zap.SugaredLogger and can use it as you would normally do.
  logger.Info("You're awesome!")
  logger.Warn("Coffee is almost empty!")
  logger.Error("Unable to operate, caffein levels too low.")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext added in v0.7.0

func FromContext(ctx context.Context) *zap.SugaredLogger

FromContext returns the logger from the given context. If the context does not contain a logger, the default logger is returned. If the context is nil, the default logger is returned.

func NewClientLogger added in v0.7.0

func NewClientLogger(debug bool) *zap.SugaredLogger

NewClientLogger returns a new logger that's meant to be used by client-type applications. It uses a human-readable format.

func NewServerLogger added in v0.7.0

func NewServerLogger(debug bool) *zap.SugaredLogger

NewServerLogger returns a new logger that's meant to be used by servers. It uses structured logging when run in production, and a human-readable format when run in development.

func NewWithOptions added in v0.7.0

func NewWithOptions(opts *Options) *zap.SugaredLogger

NewWithOptions returns a new logger with the given options. If the options are nil, the default logger is returned.

func SetDPanicSymbol added in v0.8.0

func SetDPanicSymbol(symbol string)

SetDPanicSymbol sets the dpanic symbol.

func SetDebugSymbol added in v0.7.0

func SetDebugSymbol(symbol string)

SetDebugSymbol sets the debug symbol.

func SetErrorSymbol added in v0.7.0

func SetErrorSymbol(symbol string)

SetErrorSymbol sets the error symbol.

func SetFatalSymbol added in v0.8.0

func SetFatalSymbol(symbol string)

SetFatalSymbol sets the fatal symbol.

func SetInfoSymbol added in v0.7.0

func SetInfoSymbol(symbol string)

SetInfoSymbol sets the info symbol.

func SetPanicSymbol added in v0.8.0

func SetPanicSymbol(symbol string)

SetPanicSymbol sets the panic symbol.

func SetWarnSymbol added in v0.7.0

func SetWarnSymbol(symbol string)

SetWarnSymbol sets the warning symbol.

func WithLogger added in v0.7.0

func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context

WithLogger returns a new context.Context with the given logger.

Types

type Options added in v0.7.0

type Options struct {
	// Debug enables debug logging.
	Debug bool

	// IsServer indicates whether the logger is used by a server or a client.
	IsServer bool

	// DisableStacktrace disables the stacktrace.
	DisableStacktrace bool
}

Options are the options for the logger.

Directories

Path Synopsis
examples
advanced
main implements an advanced simplog example.
main implements an advanced simplog example.
basic
main implements a basic simplog example.
main implements a basic simplog example.

Jump to

Keyboard shortcuts

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