zlog

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: GPL-3.0 Imports: 16 Imported by: 7

README

zlog

simple wrapper for zap logger

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWriterClosed     = errors.New("writer already closed")
	ErrWriterIsStopping = errors.New("writer is stopping")
)
View Source
var ErrCreateOpensearchCore = errors.New("failed to create OpenSearch core")

Functions

func DefaultOpenSearchConfig added in v0.1.2

func DefaultOpenSearchConfig(url string, insecure bool) opensearch.Config

func FlushLogsWithTimeout added in v0.1.2

func FlushLogsWithTimeout(flushFunc CleanUp, timeout time.Duration, logger *zap.Logger) func()

FlushLogsWithTimeout attempts to flush logs with a timeout. It returns a function suitable for use with defer.

func IsOpenSearchReady added in v0.1.3

func IsOpenSearchReady(url string, timeout time.Duration, insecure bool) bool

func MustLoadLocation added in v0.1.4

func MustLoadLocation(name string) *time.Location

LoadLocation loads a timezone location or panics on error

func MustNewLoggerDebug added in v0.1.1

func MustNewLoggerDebug(opts ...LogOptFunc) *zap.Logger

func MustNewZapLogger

func MustNewZapLogger(opts ...LogOptFunc) *zap.Logger

MustNewZapLogger create a simple zap logger

func MustNewZapLoggerWithFlush added in v0.1.2

func MustNewZapLoggerWithFlush(opts ...LogOptFunc) (*zap.Logger, func() error)

MustNewZapLoggerWithFlush creates a zap logger and returns it along with a flush function. This function wraps MustNewZapLogger to provide a consistent interface with MustNewZapLoggerWithOpenSearch.

func ReplaceGlobalToShowLogZapL

func ReplaceGlobalToShowLogZapL(logger *zap.Logger)

Types

type CleanUp added in v0.1.5

type CleanUp func(context.Context) error

func MustNewZapLoggerWithOpenSearch added in v0.1.2

func MustNewZapLoggerWithOpenSearch(opts ...LogOptFunc) (*zap.Logger, CleanUp)

MustNewZapLoggerWithOpenSearch creates a zap logger with OpenSearch support. It panics if the required OpenSearch configuration is missing or if initialization fails.

Parameters:

  • opts: Variadic LogOptFunc parameters for configuring the logger

Returns:

  • *zap.Logger: A configured zap logger instance
  • func() error: A flush function that should be called before program termination to ensure all logs are written to OpenSearch

OpenSearch Configuration:

  • Bulk Indexing: Uses OpenSearch bulk API for efficient log shipping
  • Workers: 2 concurrent workers for processing logs
  • Buffer Size: 256KB before forcing flush
  • Flush Interval: Every 10 seconds
  • Error Handling: Logs bulk indexing errors through internal logger

The function supports both console and OpenSearch output. When OpenSearch is enabled, both openSearchConfig and openSearchIndex must be provided through the options.

type IndexConfig added in v0.1.4

type IndexConfig struct {
	BaseIndexName string
	Format        string         // If empty, defaults to FormatDot
	Location      *time.Location // If nil, defaults to UTC
}

IndexConfig configures how index names are generated

type IndexFormat added in v0.1.4

type IndexFormat string

IndexFormat defines standard time formats for index naming

const (
	DateFormatDot   IndexFormat = "2006.01.02" // example: logs-2024.01.25
	DateFormatDash  IndexFormat = "2006-01-02" // example: logs-2024-01-25
	DateFormatShort IndexFormat = "20060102"   // example: logs-20240125
)

Predefined index formats

type IndexGenerator added in v0.1.4

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

IndexGenerator generates time-based index names

func NewIndexGenerator added in v0.1.4

func NewIndexGenerator(config IndexConfig) *IndexGenerator

NewIndexGenerator creates a new index name generator

func (*IndexGenerator) GetIndexName added in v0.1.4

func (g *IndexGenerator) GetIndexName() string

type LogOptFunc

type LogOptFunc func(o *LogOpts)

func WithConsole

func WithConsole(b bool) LogOptFunc

func WithDevEnv

func WithDevEnv(b bool) LogOptFunc

func WithInsecure added in v0.1.2

func WithInsecure(insecure bool) LogOptFunc

func WithInternalLogger added in v0.1.2

func WithInternalLogger(logger *zap.Logger) LogOptFunc

func WithLJ

func WithLJ(b bool) LogOptFunc

func WithLjFilename

func WithLjFilename(s string) LogOptFunc

WithLjFilename if name is supplied

func WithLogLevel

func WithLogLevel(lvl zapcore.Level) LogOptFunc

func WithOpenSearchConfig added in v0.1.2

func WithOpenSearchConfig(config *opensearch.Config) LogOptFunc

func WithOpenSearchIndex added in v0.1.2

func WithOpenSearchIndex(baseIndex string, dateFormat string) LogOptFunc

WithOpenSearchIndex sets the base index name and optional date format for rotation

func WithTimeLocation added in v0.1.4

func WithTimeLocation(location *time.Location) LogOptFunc

WithTimeLocation sets the timezone for index rotation

type LogOpts

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

Jump to

Keyboard shortcuts

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