logging

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README ΒΆ

Logging

This package is a fork of the package found at https://github.com/google/exposure-notifications-server/tree/main/pkg/logging.

Overview

This article does an excellent job of describing the importance of structured logs.

Log aggregating platforms tend to prefer structured logs in JSON since parsing them is easier. However, for local development and better human readability, programmers tend to prefer structured logs in plain text.

The original package provides structured logs in either JSON or plain text format, is simple and easy to use.

Uber's zap logger is used under the hood.

Tweaks/modifications have been me made to the original package to automatically pipe logs to STDOUT or STDERR based on the severity level.

Finally, the intention of this fork is to create an easier way to import the original package without running into errors.

Features

  • Ability to specify min log level, defaults to WARN, (example usage: LOG_LEVEL=ERROR)
  • Ability to specify console encoding vs JSON encoding
  • DEBUG and INFO logs are written to STDOUT
  • All other log levels (WARN, ERROR, DPANIC, PANIC, FATAL) are written to STDERR

Example usage

// main.go
package main

import (
	"gitlab.com/ownageoss/logging"
	"go.uber.org/zap"
)

type server struct {
	logger *zap.SugaredLogger
}

func main() {
	instance := &server{}

	instance.logger = logging.NewLoggerFromEnv()

	instance.logger.Debug("debug message...")

	instance.logger.Infof("%s", "info message...")

	instance.logger.Warn("warning message...")

	instance.logger.Errorf("%s", "error message...")
}

Run as follows for console encoding:

LOG_LEVEL=DEBUG LOG_MODE=DEV go run main.go

Run as follows for JSON encoding:

LOG_LEVEL=INFO go run main.go

Versioning

This project uses Semantic Versioning 2.0.0.

Project status

This library is used in production environments and is actively maintained.

Supported Go versions

Go version support is aligned with the Go's release policy.

Software Bill of Materials (SBOM)

               _      
 ___ _ __   __| |_  __
/ __| '_ \ / _` \ \/ /
\__ \ |_) | (_| |>  < 
|___/ .__/ \__,_/_/\_\
    |_|               

 πŸ“‚ SPDX Document gitlab.com/ownageoss/logging
  β”‚ 
  β”‚ πŸ“¦ DESCRIBES 1 Packages
  β”‚ 
  β”œ logging
  β”‚  β”‚ πŸ”— 12 Relationships
  β”‚  β”œ CONTAINS FILE Makefile (Makefile)
  β”‚  β”œ CONTAINS FILE README.md (README.md)
  β”‚  β”œ CONTAINS FILE .gitlab-ci.yml (.gitlab-ci.yml)
  β”‚  β”œ CONTAINS FILE .gitignore (.gitignore)
  β”‚  β”œ CONTAINS FILE go.sum (go.sum)
  β”‚  β”œ CONTAINS FILE LICENSE (LICENSE)
  β”‚  β”œ CONTAINS FILE go.mod (go.mod)
  β”‚  β”œ CONTAINS FILE logger.go (logger.go)
  β”‚  β”œ CONTAINS FILE logger_test.go (logger_test.go)
  β”‚  β”œ CONTAINS FILE logging.png (logging.png)
  β”‚  β”œ DEPENDS_ON PACKAGE go.uber.org/multierr@v1.11.0
  β”‚  β”” DEPENDS_ON PACKAGE go.uber.org/zap@v1.27.0
  β”‚ 
  β”” πŸ“„ DESCRIBES 0 Files

Documentation ΒΆ

Overview ΒΆ

Package logging sets up and configures logging.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func DefaultLogger ΒΆ

func DefaultLogger() *zap.SugaredLogger

DefaultLogger returns the default logger for the package.

func FromContext ΒΆ

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

FromContext returns the logger stored in the context. If no such logger exists, a default logger is returned.

func NewLogger ΒΆ

func NewLogger(level string, development bool) *zap.SugaredLogger

NewLogger creates a new logger with the given configuration.

func NewLoggerFromEnv ΒΆ

func NewLoggerFromEnv() *zap.SugaredLogger

NewLoggerFromEnv creates a new logger from the environment. It consumes LOG_LEVEL for determining the level and LOG_MODE for determining the output parameters.

func NewLoggerWithTeeCore ΒΆ added in v0.7.0

func NewLoggerWithTeeCore(levelEnv string, development bool) *zap.SugaredLogger

NewLoggerWithTeeCore creates a new logger with a Tee zapcore(multiple).

func WithLogger ΒΆ

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

WithLogger creates a new context with the provided logger attached.

Types ΒΆ

This section is empty.

Jump to

Keyboard shortcuts

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