Skip to content

Commit

Permalink
Merge pull request #113 from ArtisanCloud/feature/logger
Browse files Browse the repository at this point in the history
Feature/logger
  • Loading branch information
Matrix-X authored Sep 9, 2024
2 parents c58edfb + 32ab7fb commit 1639b0f
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 81 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea/


*.log
.DS_Store
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
module github.com/ArtisanCloud/PowerLibs/v3

go 1.17
go 1.23

require (
github.com/clbanning/mxj/v2 v2.7.0
github.com/golang-module/carbon v1.6.0
github.com/google/uuid v1.1.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/redis/go-redis/v9 v9.0.3
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
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
github.com/google/go-cmp v0.6.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

Expand All @@ -33,6 +34,9 @@ require (
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.3.0 // indirect
go.opentelemetry.io/otel v1.4.0
go.opentelemetry.io/otel/sdk v1.4.0
go.opentelemetry.io/otel/trace v1.4.0
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
25 changes: 17 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
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/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU=
github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
github.com/gobuffalo/logger v1.0.0/go.mod h1:2zbswyIUa45I+c+FLXuWl9zSWEiVuthsk8ze5s8JvPs=
Expand All @@ -31,8 +35,8 @@ github.com/gobuffalo/packr v1.30.1/go.mod h1:ljMyFO2EcrnzsHsN99cvbq055Y9OhRrIavi
github.com/gobuffalo/packr/v2 v2.5.1/go.mod h1:8f9c96ITobJlPzI44jj+4tHnEKNt0xXWSVlXRN9X1Iw=
github.com/golang-module/carbon v1.6.0 h1:OIQn0ClDmu4er9gQ6KIR64VkGFTySbEwtKTk8I+G80A=
github.com/golang-module/carbon v1.6.0/go.mod h1:M/TDTYPp3qWtW68u49dLDJOyGmls6L6BXdo/pyvkMaU=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand Down Expand Up @@ -80,16 +84,20 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opentelemetry.io/otel v1.4.0 h1:7ESuKPq6zpjRaY5nvVDGiuwK7VAJ8MwkKnmNJ9whNZ4=
go.opentelemetry.io/otel v1.4.0/go.mod h1:jeAqMFKy2uLIxCtKxoFj0FAL5zAPKQagc3+GtBWakzk=
go.opentelemetry.io/otel/sdk v1.4.0 h1:LJE4SW3jd4lQTESnlpQZcBhQ3oci0U2MLR5uhicfTHQ=
go.opentelemetry.io/otel/sdk v1.4.0/go.mod h1:71GJPNJh4Qju6zJuYl1CrYtXbrgfau/M9UAggqiy1UE=
go.opentelemetry.io/otel/trace v1.4.0 h1:4OOUrPZdVFQkbzl/JSdvGCWIdw5ONXXxzHlaLlWppmo=
go.opentelemetry.io/otel/trace v1.4.0/go.mod h1:uc3eRsqDfWs9R7b92xbQbU42/eTNz4N+gLP8qJCi4aE=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
Expand Down Expand Up @@ -120,6 +128,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190515120540-06a5c4944438/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -131,14 +141,13 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
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.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
20 changes: 20 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package helper

import (
"context"
"crypto/sha256"
"fmt"
. "github.com/ArtisanCloud/PowerLibs/v3/object"
"go.opentelemetry.io/otel/trace"
"golang.org/x/crypto/bcrypt"
"log"
"reflect"
Expand Down Expand Up @@ -123,3 +125,21 @@ func CheckPassword(hashedPassword string, password string) (isPasswordValid bool

return true
}

func SpanIDFromContext(ctx context.Context) string {
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasSpanID() {
return spanCtx.SpanID().String()
}

return ""
}

func TraceIDFromContext(ctx context.Context) string {
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasTraceID() {
return spanCtx.TraceID().String()
}

return ""
}
31 changes: 19 additions & 12 deletions logger/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"context"
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"github.com/ArtisanCloud/PowerLibs/v3/logger/drivers/zap"
Expand All @@ -10,6 +11,7 @@ import (

type Logger struct {
Driver contract.LoggerInterface
ctx context.Context
}

func NewLogger(driver interface{}, config *object.HashMap) (logger *Logger, err error) {
Expand All @@ -33,42 +35,47 @@ func NewLogger(driver interface{}, config *object.HashMap) (logger *Logger, err

}

func (log *Logger) WithContext(ctx context.Context) contract.LoggerInterface {
log.ctx = ctx
return log
}

func (log *Logger) Debug(msg string, v ...interface{}) {
log.Driver.Debug(msg, v...)
log.Driver.WithContext(log.ctx).Debug(msg, v...)
}
func (log *Logger) Info(msg string, v ...interface{}) {
log.Driver.Info(msg, v...)
log.Driver.WithContext(log.ctx).Info(msg, v...)
}
func (log *Logger) Warn(msg string, v ...interface{}) {
log.Driver.Warn(msg, v...)
log.Driver.WithContext(log.ctx).Warn(msg, v...)
}
func (log *Logger) Error(msg string, v ...interface{}) {
log.Driver.Error(msg, v...)
log.Driver.WithContext(log.ctx).Error(msg, v...)
}
func (log *Logger) Panic(msg string, v ...interface{}) {
log.Driver.Panic(msg, v...)
log.Driver.WithContext(log.ctx).Panic(msg, v...)
}
func (log *Logger) Fatal(msg string, v ...interface{}) {
log.Driver.Fatal(msg, v...)
log.Driver.WithContext(log.ctx).Fatal(msg, v...)
}

func (log *Logger) DebugF(format string, args ...interface{}) {
log.Driver.DebugF(format, args)
log.Driver.WithContext(log.ctx).DebugF(format, args)
}
func (log *Logger) InfoF(format string, args ...interface{}) {
log.Driver.InfoF(format, args)
log.Driver.WithContext(log.ctx).InfoF(format, args)
}
func (log *Logger) WarnF(format string, args ...interface{}) {
log.Driver.WarnF(format, args)
log.Driver.WithContext(log.ctx).WarnF(format, args)
}
func (log *Logger) ErrorF(format string, args ...interface{}) {
log.Driver.ErrorF(format, args)
log.Driver.WithContext(log.ctx).ErrorF(format, args)
}
func (log *Logger) PanicF(format string, args ...interface{}) {
log.Driver.PanicF(format, args)
log.Driver.WithContext(log.ctx).PanicF(format, args)
}
func (log *Logger) FatalF(format string, args ...interface{}) {
log.Driver.FatalF(format, args)
log.Driver.WithContext(log.ctx).FatalF(format, args)
}

func InitLogPath(path string, files ...string) (err error) {
Expand Down
52 changes: 49 additions & 3 deletions logger/app_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package logger

import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"net/http"
"os"
"testing"
"time"
)

var strArtisanCloudPath = "/var/log/ArtisanCloud/PowerLibs"
var strArtisanCloudPath = "./"
var strOutputPath = strArtisanCloudPath + "/output.log"
var strErrorPath = strArtisanCloudPath + "/errors.log"

Expand All @@ -16,21 +20,38 @@ func init() {
if err != nil {
panic(err)
}

initTracer()
}

func initTracer() {
tp := trace.NewTracerProvider()
// Set Global Tracer Provider
otel.SetTracerProvider(tp)
}

func Test_Log_Info(t *testing.T) {
driver := "zap"
logger, err := NewLogger(driver, &object.HashMap{
logger, err := NewLogger(nil, &object.HashMap{
"env": "test",
"outputPath": strOutputPath,
"errorPath": strErrorPath,
"stdout": true,
})
if err != nil {
t.Error(err)
}

// without context
logger.Info("test info", "app response", &http.Response{})

// log with context,will append traceId and spanId if ctx hash trace info
tracer := otel.Tracer("example-tracer")
ctx, span := tracer.Start(context.Background(), "test")
defer span.End()

logger.WithContext(ctx).Info("test info with context")
logger.WithContext(ctx).InfoF("current time %s", time.Now().Format("2006-01-02 15:04:05"))

}

func initLogPath(path string, files ...string) (err error) {
Expand All @@ -55,3 +76,28 @@ func initLogPath(path string, files ...string) (err error) {
return err

}

func Test_Log_Info_To_File(t *testing.T) {
logger, err := NewLogger(nil, &object.HashMap{
"env": "test",
"outputPath": strOutputPath,
"errorPath": strErrorPath,
"stdout": false,
})
if err != nil {
t.Error(err)
}

// without context
logger.Info("test info", "app response", &http.Response{})

// log with context,will append traceId and spanId if ctx hash trace info
tracer := otel.Tracer("example-tracer")
ctx, span := tracer.Start(context.Background(), "test")
logger.WithContext(ctx)
defer span.End()

logger.Info("test info with context")
logger.InfoF("current time %s", time.Now().Format("2006-01-02 15:04:05"))
logger.Error("test error with context")
}
3 changes: 3 additions & 0 deletions logger/contract/loggerInterface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package contract

import "context"

const (
DebugLevel int8 = 0
InfoLevel int8 = 1
Expand All @@ -23,6 +25,7 @@ type LoggerInterface interface {
ErrorF(format string, args ...interface{})
PanicF(format string, args ...interface{})
FatalF(format string, args ...interface{})
WithContext(ctx context.Context) LoggerInterface

//Log( level int8, msg string, objs ...interface{})
//Logf( format string, v ...interface{})
Expand Down
14 changes: 13 additions & 1 deletion logger/drivers/dummyLogger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package drivers

type DummyLogger struct{}
import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
)

type DummyLogger struct {
ctx context.Context
}

func (l *DummyLogger) WithContext(ctx context.Context) contract.LoggerInterface {
l.ctx = ctx
return l
}

func (l *DummyLogger) Debug(msg string, v ...interface{}) {}

Expand Down
11 changes: 10 additions & 1 deletion logger/drivers/simpleLogger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package drivers

import (
"context"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"log"
)

Expand All @@ -14,7 +16,14 @@ const (
FatalLevel int8 = 5
)

type SimpleLogger struct{}
type SimpleLogger struct {
ctx context.Context
}

func (l *SimpleLogger) WithContext(ctx context.Context) contract.LoggerInterface {
l.ctx = ctx
return l
}

func (l *SimpleLogger) Debug(msg string, v ...interface{}) {
log.Printf("[DEBUG] "+msg, v...)
Expand Down
Loading

0 comments on commit 1639b0f

Please sign in to comment.