zaplog

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 9 Imported by: 2

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 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 Option added in v0.0.7

type Option func(l *lumberjack.Logger)

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

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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