log

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2021 License: MIT Imports: 6 Imported by: 3

README

Log

Installation

go get go.pixelfactory.io/pkg/observability/log

Usage

Basic usage :
package main

import (
	"errors"
	"fmt"
	"os"

	"go.pixelfactory.io/pkg/observability/log"
	"go.pixelfactory.io/pkg/observability/log/fields"
)

func main() {
	logger := log.New()
	logger.Debug("Debug Msg")
	logger.Info("Info Msg")
	logger.Warn("Warn Msg")
	logger.Error("Error Msg", fields.Error(fmt.Errorf("An error happened")))
}
Capture error in Sentry

Sentry is supported by implementing the zapcore.Core interface.

package main

import (
	"errors"
	"fmt"
	"net/http"
	"os"
	"time"

	"github.com/getsentry/sentry-go"
	"go.uber.org/zap"

	"go.pixelfactory.io/pkg/observability/log"
	"go.pixelfactory.io/pkg/observability/log/fields"
)

func main() {
	// Basic Usage (defaults to Info level)
	logger := log.New()
	logger.Debug("Debug Msg")
	logger.Info("Info Msg")
	logger.Warn("Warn Msg")
	logger.Error("Error Msg", fields.Error(fmt.Errorf("An error happened")))

	// Read DSN from the environment.
	dsn := os.Getenv("SENTRY_DSN")
	if dsn == "" {
		logger.Error("Failed to get a Sentry client", fields.Error(errors.New("SENTRY_DSN is not set")))
	}

	// Instantiate a client.
	err := sentry.Init(sentry.ClientOptions{
		Dsn:              dsn,
		AttachStacktrace: true,
		Environment:      "production",
	})
	if err != nil {
		logger.Error("Failed to get a Sentry client", fields.Error(err))
	}

	// Using Sentry Core
	logger = log.New(
		log.WithLevel("debug"),
		log.WithSentry(sentry.CurrentHub().Client()),
		log.WithZapOption(zap.Development()),
	)

	// Add Fields
	// Fields must be added after logger creation
	logger = logger.With(fields.Service("myapp", "v1.0"))
	defer logger.Sync()

	err = errors.New("Invalid ID '1234' not found")
	if err != nil {
		logger.Error("An error happened", fields.Error(err))
	}
}

Elastic Common Schema

The Logger is using ECS version 1.5.0 https://www.elastic.co/guide/en/ecs/current/index.html.

Usage :

package main

import (
	"go.pixelfactory.io/pkg/observability/log"
)

func main() {

	// New Logger with ECS Service field
	logger := log.New().With(fields.Service("myapp", "v1.0.0"))

	uaString := "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"

	// Log UserAgent
	logger.Info("Info Msg", fields.UserAgent(uaString))
}

The produced log entry follows ECS specification and is understood by Elasticsearch :

{
  "log.level": "info",
  "@timestamp": "2020-07-31T12:51:38.313+0200",
  "log.origin": {
    "file.name": "observability/titi.go",
    "file.line": 15
  },
  "message": "Info Msg",
  "service": {
    "name": "myapp",
    "version": "v1.0.0"
  },
  "user_agent": {
    "original": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
    "name": "Chrome",
    "version": "23.0.1271.97"
  },
  "ecs.version": "1.5.0"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetZapLogLevel

func GetZapLogLevel(logLevel string) zapcore.Level

GetZapLogLevel returns zap.AtomicLevel from string

Types

type DefaultLogger added in v1.2.0

type DefaultLogger struct {
	// contains filtered or unexported fields
}

DefaultLogger delegates all calls to the underlying zap.Logger.

func New

func New(opts ...Option) *DefaultLogger

New returns a new logger with default values.

func (*DefaultLogger) Debug added in v1.2.0

func (l *DefaultLogger) Debug(msg string, fields ...zapcore.Field)

Debug logs a debug msg with fields.

func (*DefaultLogger) Error added in v1.2.0

func (l *DefaultLogger) Error(msg string, fields ...zapcore.Field)

Error logs an error msg with fields.

func (*DefaultLogger) Fatal added in v1.2.0

func (l *DefaultLogger) Fatal(msg string, fields ...zapcore.Field)

Fatal logs a fatal error msg with fields and panics. Apps will have to recover if ever needed.

func (*DefaultLogger) Info added in v1.2.0

func (l *DefaultLogger) Info(msg string, fields ...zapcore.Field)

Info logs an info msg with fields.

func (*DefaultLogger) Panic added in v1.2.0

func (l *DefaultLogger) Panic(msg string, fields ...zapcore.Field)

Panic logs a fatal error msg and fields and panics. Apps will have to recover if ever needed.

func (*DefaultLogger) Sync added in v1.2.0

func (l *DefaultLogger) Sync() error

Sync call zap.Logger Sync() method

func (*DefaultLogger) Warn added in v1.2.0

func (l *DefaultLogger) Warn(msg string, fields ...zapcore.Field)

Warn logs an warning msg with fields.

func (*DefaultLogger) With added in v1.2.0

func (l *DefaultLogger) With(fields ...zapcore.Field) *DefaultLogger

With creates a child logger, and optionally adds some context fields to that logger.

type Logger

type Logger interface {
	Debug(msg string, fields ...zapcore.Field)
	Info(msg string, fields ...zapcore.Field)
	Warn(msg string, fields ...zapcore.Field)
	Error(msg string, fields ...zapcore.Field)
	Fatal(msg string, fields ...zapcore.Field)
	Panic(msg string, fields ...zapcore.Field)
}

Logger is a simplified abstraction of the zap.Logger

type Option

type Option func(*DefaultLogger)

Option type

func WithLevel

func WithLevel(level string) Option

WithLevel logger level option

func WithSentry

func WithSentry(client *sentry.Client) Option

WithSentry enables sentry

func WithZapOption

func WithZapOption(opts ...zap.Option) Option

WithZapOption add fields.Service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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