Logger - A thin wrapper of uber-go/zap logger for personal project

Overview

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	[email protected]/zaploggerfunc.go:21	info logging enabled 1
2019-11-10T23:06:59.399+0800	DEBUG	[email protected]/zaploggerfunc.go:11	------------------------------------ 2
2019-11-10T23:06:59.399+0800	WARN	[email protected]/zaploggerfunc.go:31	logger.SetLevel(zap.ErrorLevel) 3
2019-11-10T23:06:59.399+0800	INFO	[email protected]/zaploggerfunc.go:21	info logging disabled aaaaaaaaaa 4
2019-11-10T23:06:59.399+0800	DEBUG	[email protected]/zaploggerfunc.go:11	info logging disabled aaaaaaa 5
2019-11-10T23:06:59.399+0800	ERROR	[email protected]/zaploggerfunc.go:51	info logging disabled aaaaaaaaa 6
2019-11-10T23:06:59.399+0800	WARN	[email protected]/zaploggerfunc.go:31	info logging disabled aaaaaaaa 7
2019-11-10T23:06:59.399+0800	ERROR	[email protected]/zaploggerfunc.go:51	info logging disabled 10
2019-11-10T23:06:59.399+0800	ERROR	[email protected]/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":"[email protected]/zaploggerfunc.go:21","msg":"info logging enabled 1"}
{"level":"debug","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:11","msg":"------------------------------------ 2"}
{"level":"warn","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:31","msg":"logger.SetLevel(zap.ErrorLevel) 3"}
{"level":"info","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:21","msg":"info logging disabled aaaaaaaaaa 4"}
{"level":"debug","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:11","msg":"info logging disabled aaaaaaa 5"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:51","msg":"info logging disabled aaaaaaaaa 6"}
{"level":"warn","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:31","msg":"info logging disabled aaaaaaaa 7"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/zaploggerfunc.go:51","msg":"info logging disabled 10"}
{"level":"error","ts":"2019-11-10T23:06:59.399+0800","caller":"[email protected]/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
You might also like...
Binalyze logger is an easily customizable wrapper for logrus with log rotation

logger logger is an easily customizable wrapper for logrus with log rotation Usage There is only one function to initialize logger. logger.Init() When

Alternative logging through zap.

wzap Alternative logging through zap Usage Start using it Download and install it: go get github.com/wyy-go/wzap Import it in your code: import "githu

Log to cloud object storage for golang. Zap extension.

cos-logger Log to cloud object storage for golang implemented as io.Writer. Use it as a plugin/extension to uber-go/zap logger Configure logger and ad

A project that adds color to golang's logger module

logger A project that adds color to golang's log module Installation go get github.com/christopher18/logger Usage // Import the module import "github

A logger, for Go

Go-Log A logger, for Go! It's sort of log and code.google.com/p/log4go compatible, so in most cases can be used without any code changes. Breaking cha

Simple logger for Go programs. Allows custom formats for messages.
Simple logger for Go programs. Allows custom formats for messages.

go-logger A simple go logger for easy logging in your programs. Allows setting custom format for messages. Preview Install go get github.com/apsdehal/

Loggly Hooks for GO Logrus logger

Loggly Hooks for Logrus Usage package main import ( "github.com/sirupsen/logrus" "github.com/sebest/logrusly" ) var logglyToken string = "YOUR_LOG

A 12-factor app logger built for performance and happy development
A 12-factor app logger built for performance and happy development

logxi log XI is a structured 12-factor app logger built for speed and happy development. Simpler. Sane no-configuration defaults out of the box. Faste

Dead simple, super fast, zero allocation and modular logger for Golang

Onelog Onelog is a dead simple but very efficient JSON logger. It is one of the fastest JSON logger out there. Also, it is one of the logger with the

Releases(v1.3.1)
Owner
tsingson
IPTV/OTT architect / soluction engineer / blues harmonica player / jeeper / photographer...
tsingson
logger wraps uber/zap and trace with opentelemetry

logger 特性 支持 uber/zap 日志 支持 log rolling,使用 lumberjace 支持日志追踪 支持debug、info、warn、e

xiaolei 9 Sep 17, 2022
Rzap - Log rotate for uber-zap

rzap Log rotate for uber-zap How to install go get github.com/winking324/rzap Ho

ShanHui 1 Jan 26, 2022
Convenient Logger interface and std logger wrapper

Convenient logger interface and wrapper around std logger Interface type Logger interface { Error(err error) Debugf(format string, args ...interface

Denis Mitrofanov 1 Nov 28, 2021
Hook for sending events zap logger to telegram.

zaptelegram Hook for sending events to telegram for zap logger. Install: go get -u github.com/strpc/zaptelegram Basic usage: package main import ( "

null 6 Oct 15, 2022
A logrus.Hook that logs with a zap.Logger

zaprus Ever had a 3rd-party dependency requiring a logrus, but you're using zap? zaprus provides a logrus.Hook that makes a logrus.(Entry|Logger) repl

Blox Staking 1 Feb 27, 2022
Zapctx: an encapsulation of zap, adding WithContext to the logger

Abstract zapctx is an encapsulation of zap, adding WithContext to the logger. Fe

oscar 3 Oct 14, 2022
Go starter project with Gin, Viper, postgres , redis, zap, prometheus metrics etc setup

Go REST Service Starter/Boilerplate Easily extendible REST API Service boilerplate aiming to follow idiomatic go and best practice. Any feedback and p

Kshitij Singh 1 Jun 23, 2022
Based uber/prototool

Prototool Update: We recommend checking out Buf, which is under active development. There are a ton of docs for getting started, including for migrati

null 0 Dec 30, 2021
Uber-cute UUID's alternative !

UwUID ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⣿⣿⡆⠀⠀⢸⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⡇⠀⠀⣾⣿⡆⠀ ⠀⠀⠀⣿⣿⡇⠀⠀⢸⣿⢰⣿⡆⠀⣾⣿⡆⠀

Germain GAU 5 Jul 22, 2022
Logger - Simple logger without written with std pkg

Go-Logger Simple usage is: package main

MaskedTrench 2 Jan 2, 2022