zapper

package module
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: GPL-3.0 Imports: 13 Imported by: 0

README

zapper Go Reference Go Report Card

zapper is zap but customized with multi core and sentry support, zapper make easiest usage with zap logger.

Cores
  • Console Writer
  • Sentry Core
  • File Writer
  • Json Core

Install

$ go get -u github.com/GoFarsi/zapper

Example

  • Console writer core
package main

import (
	"github.com/GoFarsi/zapper"
	"log"
)

func main() {
	z := zapper.New(false, zapper.WithTimeFormat(zapper.RFC3339NANO))
	if err := z.NewCore(zapper.ConsoleWriterCore(true)); err != nil {
		log.Fatal(err)
	}

	z.Info("test info")
	z.Debug("debug level")
}
  • Sentry Core
package main

import (
	"github.com/GoFarsi/zapper"
	"log"
	"os"
)

func main() {
	z := zapper.New(false)
	if err := z.NewCore(zapper.SentryCore(os.Getenv("DSN"), "test", zapper.DEVELOPMENT, nil)); err != nil {
		log.Fatal(err)
	}

	err(z)
}

func err(z zapper.Zapper) {
	z.Error("test error new")
}
  • File Writer Core
package main

import (
	"github.com/GoFarsi/zapper"
	"log"
)

func main() {
	z := zapper.New(true, zapper.WithDebugLevel())
	if err := z.NewCore(zapper.FileWriterCore("./test_data", nil)); err != nil {
		log.Fatal(err)
	}

	z.Debug("debug log")
	z.Info("info log")
	z.Warn("warn log")
	z.Error("error log")
}
  • Json Core
package main

import (
	"github.com/GoFarsi/zapper"
	"log"
)

func main() {
	z := zapper.New(true, zapper.WithDebugLevel(), zapper.WithServiceDetails(23, "zapper"))
	if err := z.NewCore(zapper.JsonWriterCore("./test_data", ".json", nil)); err != nil {
		log.Fatal(err)
	}

	z.Debug("debug log")
	z.Info("info log")
	z.Warn("warn log")
	z.Error("error log")
}
  • Multi Core
package main

import (
	"github.com/GoFarsi/zapper"
	"log"
)

func main() {
	z := zapper.New(false, zapper.WithDebugLevel())
	if err := z.NewCore(
		zapper.ConsoleWriterCore(true),
		zapper.FileWriterCore("./test_data", nil),
	); err != nil {
		log.Fatal(err)
	}

	z.Debug("debug log")
	z.Info("info log")
	z.Warn("warn log")
	z.Error("error log")
}

Contributing

  1. Fork zapper repository
  2. Clone forked project
  3. create new branch from main
  4. change things in new branch
  5. then send Pull Request from your changes in new branch

Documentation

Index

Constants

View Source
const (
	CONSOLE coreType = iota
	SENTRY
	FILE
	JSON
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller interface {
	Debug(...any)
	DebugF(string, ...any)
	DebugW(string, ...any)

	Info(...any)
	InfoF(string, ...any)
	InfoW(string, ...any)

	Warn(...any)
	WarnF(string, ...any)
	WarnW(string, ...any)

	Error(...any)
	ErrorF(string, ...any)
	ErrorW(string, ...any)

	DPanic(...any)
	DPanicF(string, ...any)
	DPanicW(string, ...any)

	Panic(...any)
	PanicF(string, ...any)
	PanicW(string, ...any)

	Fatal(...any)
	FatalF(string, ...any)
	FatalW(string, ...any)
}

type Core

type Core interface {
	// contains filtered or unexported methods
}

Core zapper base abstract

func ConsoleWriterCore

func ConsoleWriterCore(colorable bool) Core

ConsoleWriterCore create console writer for zapper to show log in console

func FileWriterCore added in v1.1.0

func FileWriterCore(logPath string, rotation *Rotation) Core

FileWriterCore write logs into file

func JsonWriterCore added in v1.3.0

func JsonWriterCore(logPath, fileExtension string, rotation *Rotation) Core

JsonWriterCore write logs with json format, fileExtension is for set output file extension json,log and etc

func SentryCore

func SentryCore(dsn string, serverName string, environment SentryEnvironment, cfg *SentryConfig) Core

SentryCore send log into sentry service

type Err

type Err struct {
	Message string
	Params  []any
}

func NewError

func NewError(msg string, params ...any) *Err

func (*Err) Error

func (e *Err) Error() string

type Level

type Level zapcore.Level

Level zapper levels

const (
	Debug  Level = iota - 1 // Debug logs are typically voluminous, and are usually disabled in production
	Info                    // Info is the default logging priority
	Warn                    // Warn logs are more important than Info, but don't need individual human review
	Error                   // Error logs are high-priority. If an application is running smoothly, it shouldn't generate any error-level logs
	DPanic                  // DPanic logs are particularly important errors. In development the logger panics after writing the message
	Panic                   // Panic logs a message, then panics
	Fatal                   // Fatal logs a message, then calls os.Exit(1)
)

func (Level) String

func (i Level) String() string

type Option

type Option func(*Zap)

func WithCustomStackTraceLevel

func WithCustomStackTraceLevel(level Level) Option

WithCustomStackTraceLevel set custom level for show stacktrace, min level warn

func WithDebugLevel

func WithDebugLevel() Option

WithDebugLevel enable debug level for logging

func WithServiceDetails added in v1.2.0

func WithServiceDetails(serviceCode uint, serviceName string) Option

WithServiceDetails set service name for zapper and show service name for log files

func WithTimeFormat

func WithTimeFormat(format TimeFormat) Option

WithTimeFormat set custom time format for zapper logs

type Rotation

type Rotation struct {
	MaxAge   int  // MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename. Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc. The default is not to remove old log files based on age.
	FileSize int  // FileSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes
	Compress bool // Compress determines if the rotated log files should be compressed using gzip. The default is not to perform compression
}

Rotation config log file rotation in log path

type SentryConfig

type SentryConfig struct {
	AttachStacktrace  bool // AttachStacktrace attach stacktrace to event
	Debug             bool // Debug add debug data to event
	EnableTracing     bool
	Environment       string
	Dist              string
	EnableBreadcrumbs bool
	BreadcrumbLevel   Level
	MaxBreadcrumbs    int
	MaxSpans          int
	Tags              map[string]string
	MinLevel          Level
	FlushTimeout      time.Duration
}

SentryConfig for sentry core set custom configs

type SentryEnvironment added in v1.4.0

type SentryEnvironment int
const (
	DEVELOPMENT SentryEnvironment = iota // DEVELOPMENT application environment
	PRODUCTION                           // PRODUCTION application environment
)

func (SentryEnvironment) String added in v1.4.0

func (e SentryEnvironment) String() string

type TimeFormat

type TimeFormat int

TimeFormat set custom time format for zap log

const (
	ISO8601     TimeFormat = iota // ISO8601 serializes a time.Time to an ISO8601-formatted string with millisecond precision
	RFC3339                       // RFC3339 serializes a time.Time to an RFC3339-formatted string
	RFC3339NANO                   // RFC3339NANO  serializes a time.Time to an RFC3339-formatted string with nanosecond precision
)

func (TimeFormat) String

func (i TimeFormat) String() string

type Zap

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

func (*Zap) DPanic

func (z *Zap) DPanic(args ...any)

DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func (*Zap) DPanicF

func (z *Zap) DPanicF(message string, args ...any)

DPanicF uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func (*Zap) DPanicW

func (z *Zap) DPanicW(message string, keyAndValues ...any)

DPanicW 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

func (*Zap) Debug

func (z *Zap) Debug(args ...any)

Debug uses fmt.Sprint to construct and log a message

func (*Zap) DebugF

func (z *Zap) DebugF(message string, args ...any)

DebugF uses fmt.Sprintf to log a templated message

func (*Zap) DebugW

func (z *Zap) DebugW(message string, keyAndValues ...any)

DebugW logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func (*Zap) Error

func (z *Zap) Error(args ...any)

Error uses fmt.Sprint to construct and log a message

func (*Zap) ErrorF

func (z *Zap) ErrorF(message string, args ...any)

ErrorF uses fmt.Sprintf to log a templated message

func (*Zap) ErrorW

func (z *Zap) ErrorW(message string, keyAndValues ...any)

ErrorW logs a message with some additional context. The variadic key-value pairs are treated as they are in With

func (*Zap) Fatal

func (z *Zap) Fatal(args ...any)

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit

func (*Zap) FatalF

func (z *Zap) FatalF(message string, args ...any)

FatalF uses fmt.Sprintf to log a templated message, then calls os.Exit

func (*Zap) FatalW

func (z *Zap) FatalW(message string, keyAndValues ...any)

FatalW logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With

func (*Zap) GetSentryClient added in v1.4.0

func (z *Zap) GetSentryClient() *sentry.Client

func (*Zap) GetServiceCode added in v1.2.0

func (z *Zap) GetServiceCode() uint

func (*Zap) GetServiceName added in v1.2.0

func (z *Zap) GetServiceName() string

func (*Zap) GetZap added in v1.3.1

func (z *Zap) GetZap() *zap.Logger

func (*Zap) Info

func (z *Zap) Info(args ...any)

Info uses fmt.Sprint to construct and log a message

func (*Zap) InfoF

func (z *Zap) InfoF(message string, args ...any)

InfoF uses fmt.Sprintf to log a templated message

func (*Zap) InfoW

func (z *Zap) InfoW(message string, keyAndValues ...any)

InfoW logs a message with some additional context. The variadic key-value pairs are treated as they are in With

func (*Zap) NewCore

func (z *Zap) NewCore(cores ...Core) error

NewCore create cores for zapper

func (*Zap) Panic

func (z *Zap) Panic(args ...any)

Panic uses fmt.Sprint to construct and log a message, then panics.

func (*Zap) PanicF

func (z *Zap) PanicF(message string, args ...any)

PanicF uses fmt.Sprintf to log a templated message, then panics

func (*Zap) PanicW

func (z *Zap) PanicW(message string, keyAndValues ...any)

PanicW logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With

func (*Zap) Warn

func (z *Zap) Warn(args ...any)

Warn uses fmt.Sprint to construct and log a message

func (*Zap) WarnF

func (z *Zap) WarnF(message string, args ...any)

WarnF uses fmt.Sprintf to log a templated message

func (*Zap) WarnW

func (z *Zap) WarnW(message string, keyAndValues ...any)

WarnW logs a message with some additional context. The variadic key-value pairs are treated as they are in With

type Zapper

type Zapper interface {
	Caller

	NewCore(...Core) error
	GetServiceCode() uint
	GetServiceName() string
	GetZap() *zap.Logger
	GetSentryClient() *sentry.Client
}

func New

func New(development bool, opts ...Option) Zapper

New create new Zap object

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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