logger

package module
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 15 Imported by: 7

README

DO NOT use this in your business project

a thin wraper of uber-go/zap logger for personal project

GoDocGo Report Card

0. thanks uber-go/zap

Blazing fast, structured, leveled logging in Go.

---- uber-go/zap

this thin wrapper of uber-go/zap

chinese readme here 中文说明

1. feature

  • Import from github.com/rs/zerolog/diode Support many-to-one caching to enhance log file write performance
  • Add gopkg.in/natefinch/lumberjack.v2 for split log file by file size
  • Add github.com/lestrrat-go/file-rotatelogs for split log file  every day
  • Support github.com/jackc/pgx/v4 log 
  • Support fasthttp log 
  • setup default log storage path, log file name....
  • debug option that logs to STDOUT 
  • add global log level setting

2. example

in linux os macOS

go source code in ./example-test/main.go

package main

import (
    "go.uber.org/zap"

    "github.com/tsingson/logger"
)

func main() {

		log := logger.New(logger.WithStoreInDay(),
		logger.WithDebug(),
		logger.WithDays(31),
		logger.WithLevel(zapcore.InfoLevel))
		
    defer log.Sync()

    logger.SetLevel(zap.DebugLevel)
    log.Info("info logging enabled 1")
    log.Debug("------------------------------------ 2")
    log.Warn(`logger.SetLevel(zap.ErrorLevel) 3`)
    log.Info("info logging disabled aaaaaaaaaa 4")
    log.Debug("info logging disabled aaaaaaa 5")
    log.Error("info logging disabled aaaaaaaaa 6")
    log.Warn("info logging disabled aaaaaaaa 7")

    logger.SetLevel(zap.ErrorLevel)
    log.Info("info logging disabled 8")
    log.Debug("info logging disabled 9")
    log.Error("info logging disabled 10")
    log.Warn("info logging disabled 11")
    log.Error("------------------------------------ 12")
    log.Info(`	logger.SetLevel(zap.DebugLevel) 13`)

}

 

build and runing

 /home/go/bin   ./example-test                      
2019-11-10T23:06:59.399+0800	INFO	logger@/zaploggerfunc.go:21	info logging enabled 1
2019-11-10T23:06:59.399+0800	DEBUG	logger@/zaploggerfunc.go:11	------------------------------------ 2
2019-11-10T23:06:59.399+0800	WARN	logger@/zaploggerfunc.go:31	logger.SetLevel(zap.ErrorLevel) 3
2019-11-10T23:06:59.399+0800	INFO	logger@/zaploggerfunc.go:21	info logging disabled aaaaaaaaaa 4
2019-11-10T23:06:59.399+0800	DEBUG	logger@/zaploggerfunc.go:11	info logging disabled aaaaaaa 5
2019-11-10T23:06:59.399+0800	ERROR	logger@/zaploggerfunc.go:51	info logging disabled aaaaaaaaa 6
2019-11-10T23:06:59.399+0800	WARN	logger@/zaploggerfunc.go:31	info logging disabled aaaaaaaa 7
2019-11-10T23:06:59.399+0800	ERROR	logger@/zaploggerfunc.go:51	info logging disabled 10
2019-11-10T23:06:59.399+0800	ERROR	logger@/zaploggerfunc.go:51	------------------------------------ 12
/home/go/bin   cat ./log/example-test-2019-11-10-23.log 
{"level":"info","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:21","msg":"info logging enabled 1"}
{"level":"debug","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:11","msg":"------------------------------------ 2"}
{"level":"warn","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:31","msg":"logger.SetLevel(zap.ErrorLevel) 3"}
{"level":"info","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:21","msg":"info logging disabled aaaaaaaaaa 4"}
{"level":"debug","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:11","msg":"info logging disabled aaaaaaa 5"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:51","msg":"info logging disabled aaaaaaaaa 6"}
{"level":"warn","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:31","msg":"info logging disabled aaaaaaaa 7"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:51","msg":"info logging disabled 10"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"logger@/zaploggerfunc.go:51","msg":"------------------------------------ 12"}

3. how to use it

3.1 go get and import it

this repo use go module

get it

 go get github.com/tsingson/logger

import in go code

 import "github.com/tsingson/logger"

logger struct in ./zaplogger.go

// 
// ZapLogger a wrap of uber-go/zap
type ZapLogger struct {
	debug      bool    // debut is true, send log to  STDOUT
	storeInDay bool    // storeInDay is true,  save log file day by day 
	addCaller  bool    // 
	days     int64     // max storage days
	path       string   // the path to save log file
	prefix     string   // the prefix of log file name
	Log        *zap.Logger
	logLevel zapcore.LevelEnabler  // global log level setting, default is info level
}

// Logger  nick name
type Logger = ZapLogger

initial the logger

simple :

    log := logger.New()
    defer log.Sync()

add options

    log := logger.New(logger.WithDebug(true ), logger.WithAddCaller())
    defer log.Sync()
    
    logger.SetLevel(zap.DebugLevel) // change log level in runtime

4. change log

  1. 2019/10/28 move code from project as single repo
  2. 2019/12/24 add log storage in every day and splite error log
  3. 2020/02/08 clean up for golangci-lint

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLevel

func SetLevel(level zapcore.Level)

SetLevel set log level for zap adapter log

func StrBuilder added in v1.2.3

func StrBuilder(args ...string) string

StrBuilder strings builder

Types

type CronLogger added in v1.2.8

type CronLogger struct {
	// contains filtered or unexported fields
}
type Logger interface {
	// Info logs routine messages about cron's operation.
	Info(msg string, keysAndValues ...interface{})
	// Error logs an error condition.
	Error(err error, msg string, keysAndValues ...interface{})
}

func (*CronLogger) Error added in v1.2.8

func (c *CronLogger) Error(err error, msg string, keysAndValues ...interface{})

func (*CronLogger) Info added in v1.2.8

func (c *CronLogger) Info(msg string, keysAndValues ...interface{})

type Logger

type Logger struct {
	Log *zap.Logger
	// contains filtered or unexported fields
}

Logger zap adapter log

func (*Logger) Core added in v1.3.9

func (s *Logger) Core() zapcore.Core

func (*Logger) CronLogger added in v1.2.8

func (s *Logger) CronLogger() *CronLogger

func (*Logger) DPanic added in v1.3.0

func (s *Logger) DPanic(args ...interface{})

DPanic logs a message at level Painc on the ZapLogger.

func (*Logger) Debug added in v1.3.0

func (s *Logger) Debug(args ...interface{})

Debug logs a message at level DebugMode on the ZapLogger.

func (*Logger) Debugf added in v1.3.0

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

Debugf logs a message at level DebugMode on the ZapLogger.

func (*Logger) Error added in v1.3.0

func (s *Logger) Error(args ...interface{})

Error logs a message at level Error on the ZapLogger.

func (*Logger) Errorf added in v1.3.0

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

Errorf logs a message at level Warn on the ZapLogger.

func (*Logger) Fatal added in v1.3.0

func (s *Logger) Fatal(args ...interface{})

Fatal logs a message at level Fatal on the ZapLogger.

func (*Logger) Fatalf added in v1.3.0

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

Fatalf logs a message at level Warn on the ZapLogger.

func (*Logger) Info added in v1.3.0

func (s *Logger) Info(args ...interface{})

Info logs a message at level Info on the ZapLogger.

func (*Logger) Infof added in v1.3.0

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

Infof logs a message at level Info on the ZapLogger.

func (*Logger) Named added in v1.3.0

func (s *Logger) Named(str string) *zap.Logger

Named name logger

func (*Logger) Panic added in v1.3.0

func (s *Logger) Panic(args ...interface{})

Panic logs a message at level Painc on the ZapLogger.

func (*Logger) Panicf added in v1.3.0

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

Panicf logs a message at level Warn on the ZapLogger.

func (*Logger) PgxLogger added in v1.3.1

func (s *Logger) PgxLogger() *PgxLogger

func (*Logger) Print added in v1.3.0

func (s *Logger) Print(args ...interface{})

Print logs a message at level Info on the ZapLogger.

func (*Logger) Printf added in v1.3.0

func (s *Logger) Printf(format string, args ...interface{})

Printf logs a message at level Info on the ZapLogger.

func (*Logger) Println added in v1.3.0

func (s *Logger) Println(v ...interface{})

Println logs a message at level Info on the ZapLogger.

func (*Logger) Sentinel added in v1.3.1

func (s *Logger) Sentinel() *Sentinel

func (*Logger) SetLevel added in v1.0.0

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

SetLevel set log level for zap adapter log

func (*Logger) Sync added in v1.3.0

func (s *Logger) Sync()

Sync wrap sync

func (*Logger) Warn added in v1.3.0

func (s *Logger) Warn(args ...interface{})

Warn logs a message at level Warn on the ZapLogger.

func (*Logger) Warnf added in v1.3.0

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

Warnf logs a message at level Warn on the ZapLogger.

func (*Logger) Warning added in v1.3.0

func (s *Logger) Warning(args ...interface{})

Warning logs a message at level Warn on the ZapLogger.

func (*Logger) Warningf added in v1.3.0

func (s *Logger) Warningf(template string, args ...interface{})

Warningf logs a message at level Warn on the ZapLogger.

func (*Logger) With added in v1.3.0

func (s *Logger) With(k string, v interface{}) *ZapLogger

With return a log with an extra field.

func (*Logger) WithField added in v1.3.0

func (s *Logger) WithField(k string, v interface{}) *ZapLogger

WithField return a log with an extra field.

func (*Logger) WithFields added in v1.3.0

func (s *Logger) WithFields(fields map[string]interface{}) *ZapLogger

WithFields return a log with extra fields.

func (*Logger) WithOptions

func (s *Logger) WithOptions(opts ...Option) *Logger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type MyLogger

type MyLogger interface {
	Fatalf(string, ...interface{})
	Debugf(string, ...interface{})
	Errorf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Debug(...interface{})
	Warn(...interface{})
	Info(...interface{})
	Fatal(...interface{})
}

MyLogger interface

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Logger.

type PgxLogger added in v1.2.10

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

func NewPgxLogger added in v1.2.10

func NewPgxLogger(logger *zap.Logger) *PgxLogger

NewPgxLogger creates a new PgxLogger.

func (*PgxLogger) Log added in v1.2.10

func (pl *PgxLogger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]interface{})

type PrintfLogger

type PrintfLogger interface {
	Printf(format string, args ...interface{})
}

PrintfLogger for fasthttp log interface

type Sentinel added in v1.3.1

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

func (*Sentinel) Debug added in v1.3.1

func (s *Sentinel) Debug(msg string, args ...interface{})

func (*Sentinel) DebugEnabled added in v1.3.1

func (s *Sentinel) DebugEnabled() bool

func (*Sentinel) Error added in v1.3.1

func (s *Sentinel) Error(err error, msg string, args ...interface{})

func (*Sentinel) ErrorEnabled added in v1.3.1

func (s *Sentinel) ErrorEnabled() bool

func (*Sentinel) Info added in v1.3.1

func (s *Sentinel) Info(msg string, args ...interface{})

func (*Sentinel) InfoEnabled added in v1.3.1

func (s *Sentinel) InfoEnabled() bool

func (*Sentinel) Warn added in v1.3.1

func (s *Sentinel) Warn(msg string, args ...interface{})

func (*Sentinel) WarnEnabled added in v1.3.1

func (s *Sentinel) WarnEnabled() bool

type ZapLogger

type ZapLogger = Logger

ZapLogger logger

func FromZap

func FromZap(log *zap.Logger) *ZapLogger

FromZap initial

func New

func New(opts ...ZapLoggerOption) *ZapLogger

New init a log

type ZapLoggerOption

type ZapLoggerOption func(*ZapLogger)

ZapLoggerOption options

func WithCaller added in v1.0.0

func WithCaller() ZapLoggerOption

WithCaller set debug

func WithDays

func WithDays(days int64) ZapLoggerOption

WithDays set debug

func WithDebug

func WithDebug() ZapLoggerOption

WithDebug set debug

func WithLevel

func WithLevel(level zapcore.Level) ZapLoggerOption

WithLevel set log level

func WithPath

func WithPath(path string) ZapLoggerOption

WithPath set log level

func WithPrefix

func WithPrefix(prefix string) ZapLoggerOption

WithPrefix set log level

func WithSamplingCore added in v1.3.4

func WithSamplingCore() ZapLoggerOption

func WithStoreInDay

func WithStoreInDay() ZapLoggerOption

WithStoreInDay set debug

Directories

Path Synopsis
cmd
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.
example
package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Jump to

Keyboard shortcuts

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