Skip to content

Commit

Permalink
feat: add logger and redis
Browse files Browse the repository at this point in the history
  • Loading branch information
piavgh committed Jan 19, 2024
0 parents commit 59a7f7c
Show file tree
Hide file tree
Showing 12 changed files with 768 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<!--- Provide a general summary of your changes in the Title above -->

## Why did we need it?
<!--- Describe your changes in detail -->

## Related Issue
<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Screenshots (if appropriate):
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Toolkit Go CI

concurrency:
group: ci-workflow-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

on:
workflow_dispatch:
push:
branches:
- main
- release-v**
pull_request:

env:
SERVICE: logger

jobs:
lint:
name: Run golangci-lint
runs-on: [ ubuntu-22.04 ]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: "1.20.x"
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
skip-pkg-cache: true
skip-build-cache: true
args: --timeout=2m
test:
runs-on: [ ubuntu-22.04 ]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: "1.20.x"
- name: Run tests
run: go test -race -coverprofile cover.out -vet=off ./...

- name: Print coverage
run: |
go tool cover -func cover.out | grep total | awk '{notice="Statement Coverage: " substr($3, 1, length($3))} END {print notice}'
67 changes: 67 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: 'Release'

permissions: write-all

on:
workflow_dispatch:
inputs:
version:
description: 'Release version'
type: string
required: true

jobs:
prepare:
runs-on: [ ubuntu-22.04 ]
outputs:
version_tag: ${{ steps.version_tag.outputs.value }}
build_date: ${{ steps.build_date.outputs.value }}
steps:
- name: Format version tag
shell: bash
id: version_tag
env:
INPUT_TAG: ${{ github.event.inputs.version }}
run: |
TAG=${INPUT_TAG#v}
echo "::set-output name=value::v$TAG"
- name: Build date
shell: bash
id: build_date
run: echo "::set-output name=value::$(date +%FT%T%z)"

release:
needs:
- prepare
runs-on: [ ubuntu-22.04 ]
env:
VERSION_TAG: ${{ needs.prepare.outputs.version_tag }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.20.x"

- name: Setup Git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Create tag
run: |
git tag -d "$VERSION_TAG" 2> /dev/null || echo "Release tag '$VERSION_TAG' does NOT exist"
git tag --annotate --message "dmm-aggregator-backend $VERSION_TAG" "$VERSION_TAG"
git push origin "refs/tags/$VERSION_TAG"
- name: Create release
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag_name: ${{ env.VERSION_TAG }}
prerelease: false
name: "Toolkit Go ${{ env.VERSION_TAG }}"
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generate:
go generate ./...

lint:
golangci-lint run ./...

test:
go test ./... -count=1


.PHONY: generate lint test
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/KyberNetwork/toolkit-go

go 1.21

require (
github.com/redis/go-redis/v9 v9.4.0
github.com/sirupsen/logrus v1.9.3
go.uber.org/zap v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk=
github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions pkg/logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# logger

Flexible logger interface that supports both zap and logrus
170 changes: 170 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package logger

import (
"errors"
"sync"
)

// A global variable so that log functions can be directly accessed
var log = DefaultLogger()

// Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}

// LoggerBackend represents the int enum for backend of logger.
// nolint:revive
type LoggerBackend int

const (
// Debug has verbose message
debugLvl = "debug"
// Info is default log level
infoLvl = "info"
// Warn is for logging messages about possible issues
warnLvl = "warn"
// Error is for logging errors
errorLvl = "error"
// Fatal is for logging fatal messages. The system shutdowns after logging the message.
fatalLvl = "fatal"
)

const (
// LoggerBackendZap logging using Uber's zap backend
LoggerBackendZap LoggerBackend = iota
// LoggerBackendLogrus logging using logrus backend
LoggerBackendLogrus
)

var (
errInvalidLoggerInstance = errors.New("invalid logger instance")

once sync.Once
)

// Logger is our contract for the logger
type Logger interface {
Debug(msg string)
Debugf(format string, args ...interface{})

Info(msg string)
Infof(format string, args ...interface{})
Infoln(msg string)

Warn(msg string)
Warnf(format string, args ...interface{})

Error(msg string)
Errorf(format string, args ...interface{})

Fatal(msg string)
Fatalf(format string, args ...interface{})

WithFields(keyValues Fields) Logger

// extract instance logger.
GetDelegate() interface{}
SetLogLevel(level string) error
}

// Configuration stores the config for the logger
// For some loggers there can only be one level across writers, for such the level of Console is picked by default
type Configuration struct {
EnableConsole bool `mapstructure:"enable_console"`
EnableJSONFormat bool `mapstructure:"enable_json_format"`
ConsoleLevel string `mapstructure:"console_level"`
EnableFile bool
FileJSONFormat bool
FileLevel string
FileLocation string
}

// DefaultLogger creates default logger, which uses zap sugarLogger and outputs to console
func DefaultLogger() Logger {
cfg := Configuration{
EnableConsole: true,
EnableJSONFormat: false,
ConsoleLevel: "warn",
EnableFile: false,
FileJSONFormat: false,
}
logger, _ := newZapLogger(cfg)
return logger
}

// InitLogger returns an instance of logger
func InitLogger(config Configuration, backend LoggerBackend) (Logger, error) {
var err error
once.Do(func() {
log, err = NewLogger(config, backend)
})
return log, err
}

func NewLogger(config Configuration, backend LoggerBackend) (Logger, error) {
switch backend {
case LoggerBackendZap:
return newZapLogger(config)

case LoggerBackendLogrus:
return newLogrusLogger(config)

default:
return nil, errInvalidLoggerInstance
}
}

func Debug(msg string) {
log.Debugf(msg)
}

func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}

func Info(msg string) {
log.Infof(msg)
}

func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}

func Infoln(msg string) {
log.Infoln(msg)
}

func Warn(msg string) {
log.Warnf(msg)
}

func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}

func Error(msg string) {
log.Errorf(msg)
}

func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}

func Fatal(msg string) {
log.Fatalf(msg)
}

func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}

func WithFields(keyValues Fields) Logger {
return log.WithFields(keyValues)
}

func GetDelegate() interface{} {
return log.GetDelegate()
}

func SetLogLevel(level string) error {
return log.SetLogLevel(level)
}
Loading

0 comments on commit 59a7f7c

Please sign in to comment.