log

package
v0.0.0-...-d5b0b98 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 9 Imported by: 0

Documentation

Overview

Example
package main

import (
	"github.com/tencentmusic/evhub/pkg/log"
)

func main() {
	log.Init(&log.Config{
		Level:      log.LevelInfo,
		Filename:   "test.log",
		MaxSize:    10,
		MaxAge:     1,
		MaxBackups: 2,
		Compress:   false,
		DevMode:    false,
	})

	people := "Alice"
	log.Debug("Hello", "people", people)
	log.Info("Hello", "people", people)
	log.Warn("Hello", "people", people)
	log.Error("Hello", "people", people)
}
Output:

Index

Examples

Constants

View Source
const (
	OutputStdout = iota
	OutputFile
	OutputStdoutAndFile
)
View Source
const (
	IndexName = "index_name"
	AppIDName = "app_id"

	CallerSkipWith = -2
)

Variables

View Source
var (
	LogIndexName = "evhub"
	AppID        = "evhub"
)

Functions

func Close

func Close()

Close flushes any buffered log entries.

func Debug

func Debug(msg string, keyvals ...interface{})

Debug logs a message with some additional context.

func Debugf

func Debugf(template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func Error

func Error(msg string, keyvals ...interface{})

Error logs a message with some additional context.

func Errorf

func Errorf(template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func Fatal

func Fatal(msg string, keyvals ...interface{})

Fatal logs a message with some additional context, then calls os.Exit.

func Fatalf

func Fatalf(template string, args ...interface{})

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

func Info

func Info(msg string, keyvals ...interface{})

Info logs a message with some additional context.

func Infof

func Infof(template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func Init

func Init(config *Config)

Init init the logger

func LevelHandler

func LevelHandler() http.Handler

LevelHandler returns an HTTP handler that can dynamically modifies the log level.

func Panic

func Panic(msg string, keyvals ...interface{})

Panic logs a message with some additional context, then panics.

func Panicf

func Panicf(template string, args ...interface{})

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

func SetDye

func SetDye(ctx context.Context, appID string, topicID string,
	eventID string, retryTimes, repeatTimes uint32) context.Context

func SetEventAppID

func SetEventAppID(ctx context.Context, appID string) context.Context

func SetEventID

func SetEventID(ctx context.Context, eventID string) context.Context

func SetEventTopicID

func SetEventTopicID(ctx context.Context, topicID string) context.Context

func SetLevel

func SetLevel(level Level)

SetLevel alters the logging level.

func SetRepeatTimes

func SetRepeatTimes(ctx context.Context, repeatTimes uint32) context.Context

func SetRetryTimes

func SetRetryTimes(ctx context.Context, retryTimes uint32) context.Context

func ShouldJSON

func ShouldJSON(v interface{})

func Warn

func Warn(msg string, keyvals ...interface{})

Warn logs a message with some additional context.

func Warnf

func Warnf(template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

func With

func With(ctx context.Context) *zap.SugaredLogger

Types

type Config

type Config struct {
	Level    Level  // Level is the minimum enabled logging level.
	Output   int    // Output determines where the log should be written to.
	Filename string // Filename is the file to write logs to.
	MaxSize  int    // MaxSize is the maximum size in megabytes of the log file before it gets rotated.
	MaxAge   int    // MaxAge is the maximum number of days to
	// retain old log files based on the timestamp encoded in their filename.
	MaxBackups   int  // MaxBackups is the maximum number of old log files to retain.
	Compress     bool // Compress determines if the rotated log files should be compressed using gzip.
	DevMode      bool // DevMode if true -> print colorful log in console and files.
	LogIndexName string
	AppID        string
}

Config is the configuration of the log.

type EventKey

type EventKey string
const (
	EventAppID   EventKey = "event_app_id"
	EventTopicID EventKey = "event_topic_id"
	EventID      EventKey = "event_id"
	RetryTimes   EventKey = "retry_times"
	RepeatTimes  EventKey = "repeat_times"
)

type Level

type Level int

A Level is a logging priority. Higher levels are more important.

const (
	// LevelDebug logs are typically voluminous, and are usually disabled in
	// production.
	LevelDebug Level = iota
	// LevelInfo is the default logging priority.
	LevelInfo
	// LevelWarn logs are more important than Info, but don't need individual
	// human review.
	LevelWarn
	// LevelError logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	LevelError
	// LevelPanic logs a message, then panics.
	LevelPanic
	// LevelFatal logs a message, then calls os.Exit(1).
	LevelFatal
)

func ConvertLevel

func ConvertLevel(level string) Level

func GetLevel

func GetLevel() Level

func (*Level) UnmarshalText

func (lvl *Level) UnmarshalText(text []byte) error

UnmarshalText Unmarshal the text.

func (Level) ZapLevel

func (lvl Level) ZapLevel() zapcore.Level

ZapLevel return a zap level.

type Logger

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

	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Panicf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})

	With(args ...interface{}) Logger

	WithOptions(opts ...zap.Option) *zap.SugaredLogger

	SetLevel(Level)
	LevelHandler() http.Handler

	Close() error
	GetLevel() Level
}

Logger is the fundamental interface for all log operations.

func GetLogger

func GetLogger() Logger

func New

func New(config *Config) Logger

New returns a Logger instance.

Jump to

Keyboard shortcuts

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