WLog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

WLog

WLog is a convenient and easy-to-use secondary encapsulation of ZapLog, providing different encoding formats, supporting message queues, and file logging.

Requirement

  • Go: >=1.22.1
  • OS: Linux / MacOS / Windows
  • CPU: AMD64 / ARM64

Features

  • Provides encapsulation for various settings, allowing simple configuration to complete formatted logging.
  • Offers default log rotation functionality.
  • Supports message queues (currently only supports sending to a specified topic in Kafka).

Getting WLog

go get -u github.com/WwhdsOne/Wlog
go get -u go.uber.org/zap 
go get -u github.com/natefinch/lumberjack 
go get -u github.com/IBM/sarama 

Usage

Direct Usage
func main() {
  l := WLog.Default()
  lo := &WLog.Loptions{
    Package: "testPackage",
    Option:  []any{"LOL", 123},
  }
  l.Debug("Debug %s %d", lo)
  l.Info("Info %s %d", lo)
  l.Warn("Warn %s %d", lo)
}
Specify File Object
ls := &WLog.LogSummary{
  LocalFileWriter: &file.LocalFileLogWriter{FileName: "app.log", FileDirPath: "./logs"},
}
fmt.Println(ls)
lo := &WLog.Loptions{
    Package: "testPackage",
    Option:  []any{"LOL", 123},
  }
build := WLog.Build(ls)
build.Info("Hello World",lo)
build.Error("Hello Error",lo)
build.Debug("Hello Debug",lo)
Specify Kafka and Topic
ls := &WLog.LogSummary{
  KafkaWriter:     &mq.KafkaLogProducer{Topic: "test-topic", 
                                        Partition: 0, 
                                        Host: "localhost", 
                                        Port: 9092
                                       },
}
fmt.Println(ls)
lo := &WLog.Loptions{
  Package: "testPackage",
  Option:  []any{"LOL", 123},
}
build := WLog.Build(ls)
build.Info("Hello World",lo)
build.Error("Hello Error",lo)
build.Debug("Hello Debug",lo)
Modify Log Format
ls := &WLog.LogSummary{
  LocalFileWriter: &file.LocalFileLogWriter{FileName: "app.log", FileDirPath: "./logs"},
  LogFormatConfig: &WLog.LogFormatConfig{
    Level:           WLog.DebugLevel,
    Prefix:          "[TEST-ZAP-JSON]",
    IsJson:          false,
    EncoderLevel:    "CapitalColorLevelEncoder",
    StacktraceLevel: WLog.ErrorLevel,
  },
}
fmt.Println(ls)
lo := &WLog.Loptions{
    Package: "testPackage",
    Option:  []any{"LOL", 123},
  }
build := WLog.Build(ls)
build.Info("Hello World",lo)
build.Error("Hello Error",lo)
build.Debug("Hello Debug",lo)
Result
> JSON
{"level":"info","time":"2024-10-23 14:14:48.293","msg":"[TEST-ZAP-JSON] package = test Info message LOL 123"}
Message sent to partition 0 at offset 20116
{"level":"info","time":"2024-10-23 14:14:48.295","msg":"[TEST-ZAP-JSON] package = test Info message LOL 123"}

> Default
2024-10-23 14:30:29.550 DEBUG   [TEST-ZAP-JSON] Debug message
2024-10-23 14:30:29.550 INFO    [TEST-ZAP-JSON] package = test Info message LOL 123
2024-10-23 14:30:29.550 WARN    [TEST-ZAP-JSON] package = test Warn message LOL 123

Documentation

Index

Constants

View Source
const (
	LowercaseLevelEncoder      = "LowercaseLevelEncoder"      // 小写编码器(默认)
	LowercaseColorLevelEncoder = "LowercaseColorLevelEncoder" // 小写编码器带颜色
	CapitalLevelEncoder        = "CapitalLevelEncoder"        // 大写编码器
	CapitalColorLevelEncoder   = "CapitalColorLevelEncoder"   // 大写编码器带颜色
)
View Source
const (
	DebugLevel = zapcore.DebugLevel
	InfoLevel  = zapcore.InfoLevel
	WarnLevel  = zapcore.WarnLevel
	ErrorLevel = zapcore.ErrorLevel
	PanicLevel = zapcore.PanicLevel
	FatalLevel = zapcore.FatalLevel
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

func Error

func Error(msg string)

func Fatal

func Fatal(msg string)

func Info

func Info(msg string)

func Panic

func Panic(msg string)

func ReplaceDefault

func ReplaceDefault(l *Logger)

func Sync

func Sync() error

func Warn

func Warn(msg string)

Types

type Logger

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

func Build

func Build(ls *wlcore.LogSummary) *Logger

func Default

func Default() *Logger

func (*Logger) Debug

func (l *Logger) Debug(msg string, loptions *wlcore.Loptions)

func (*Logger) Error

func (l *Logger) Error(msg string, loptions *wlcore.Loptions)

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, loptions *wlcore.Loptions)

func (*Logger) Info

func (l *Logger) Info(msg string, loptions *wlcore.Loptions)

func (*Logger) Panic

func (l *Logger) Panic(msg string, loptions *wlcore.Loptions)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level zapcore.Level)

SetLevel 设置日志等级

func (*Logger) Sync

func (l *Logger) Sync() error

Sync 同步日志

func (*Logger) Warn

func (l *Logger) Warn(msg string, loptions *wlcore.Loptions)

Directories

Path Synopsis
mq
sys

Jump to

Keyboard shortcuts

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