zaplog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 10 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zaplog

Simple packaging for zap

Usage

package main

import (
	"context"
	"fmt"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	zlog "github.com/vearne/zaplog"
	"go.opentelemetry.io/otel"
	otelProm "go.opentelemetry.io/otel/exporters/prometheus"
	sdkmetric "go.opentelemetry.io/otel/sdk/metric"
	"go.uber.org/zap"
	"net/http"
	"sync/atomic"
	"time"
)

var ops1 uint64
var ops2 uint64

func main() {
	InitMeterProvider()

	zlog.InitLogger("/tmp/withContext.log", "debug")
	go func() {
		//logger1 := zlog.Named("worker1")
		ctx := context.Background()
		for {
			atomic.AddUint64(&ops1, 1)

			zlog.InfoContext(ctx, "test info1", zap.Uint64("ops", atomic.LoadUint64(&ops1)))
			zlog.WarnContext(ctx, "test warn1", zap.Uint64("ops", atomic.LoadUint64(&ops1)))
			zlog.ErrorContext(ctx, "test error1", zap.Uint64("ops", atomic.LoadUint64(&ops1)))
			time.Sleep(200 * time.Millisecond)
		}

	}()
	go func() {
		//logger2 := zlog.Named("worker2")
		ctx := context.Background()
		for {
			atomic.AddUint64(&ops2, 1)

			zlog.InfoContext(ctx, "test info2", zap.Uint64("ops", atomic.LoadUint64(&ops2)))
			zlog.WarnContext(ctx, "test warn2", zap.Uint64("ops", atomic.LoadUint64(&ops2)))
			zlog.ErrorContext(ctx, "test error2", zap.Uint64("ops", atomic.LoadUint64(&ops2)))
			time.Sleep(200 * time.Millisecond)
		}
	}()

	http.Handle("/metrics", promhttp.Handler())
	fmt.Println("starting...")
	// http://localhost:9090/metrics
	http.ListenAndServe(":9090", nil)
}

func InitMeterProvider() *sdkmetric.MeterProvider {
	promExporter, err := otelProm.New(otelProm.WithNamespace("otel-metrics"),
		otelProm.WithRegisterer(prometheus.DefaultRegisterer),
	)
	if err != nil {
		panic(err)
	}
	mp := sdkmetric.NewMeterProvider(
		sdkmetric.WithReader(promExporter),
		//sdkmetric.WithResource(initResource()),
	)
	otel.SetMeterProvider(mp)

	return mp
}

Documentation

Index

Constants

View Source
const LabelLevel = "level"

Variables

View Source
var (
	DefaultLogger *otelzap.Logger
)

Functions

func Debug

func Debug(msg string, fields ...zapcore.Field)

func DebugContext added in v0.0.2

func DebugContext(ctx context.Context, msg string, fields ...zapcore.Field)

func Error

func Error(msg string, fields ...zapcore.Field)

func ErrorContext added in v0.0.2

func ErrorContext(ctx context.Context, msg string, fields ...zapcore.Field)

func Fatal

func Fatal(msg string, fields ...zapcore.Field)

func FatalContext added in v0.0.2

func FatalContext(ctx context.Context, msg string, fields ...zapcore.Field)

func GetDefaultLogger

func GetDefaultLogger() *otelzap.Logger

func GetTraceID added in v0.1.0

func GetTraceID(ctx context.Context) string

GetTraceID returns the 32-character lowercase hexadecimal string of the TraceID from the current context. Returns an empty string if there is no Span in the ctx.

func Info

func Info(msg string, fields ...zapcore.Field)

func InfoContext added in v0.0.2

func InfoContext(ctx context.Context, msg string, fields ...zapcore.Field)

func InitLogger

func InitLogger(logPath string, level string, opts ...Option)

func Named added in v0.0.5

func Named(s string) *otelzap.Logger

func Warn

func Warn(msg string, fields ...zapcore.Field)

func WarnContext added in v0.0.2

func WarnContext(ctx context.Context, msg string, fields ...zapcore.Field)

Types

type Config added in v0.1.0

type Config struct {
	lumberjack.Logger
	WithTraceID bool
}

type Option added in v0.0.7

type Option func(c *Config)

func WithCompress added in v0.0.7

func WithCompress(compress bool) Option

func WithMaxAge added in v0.0.7

func WithMaxAge(days int) Option

days

func WithMaxBackups added in v0.0.7

func WithMaxBackups(maxBackups int) Option

func WithMaxSize added in v0.0.7

func WithMaxSize(maxSize int) Option

megabytes

func WithTraceID added in v0.1.0

func WithTraceID(on bool) Option

WithTraceIDField configures the logger to add `trace_id` field to structured log messages.

Directories

Path Synopsis
example
noContext command
withContext command

Jump to

Keyboard shortcuts

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