Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Add logrus wrapper as alternative logger example.
Browse files Browse the repository at this point in the history
Signed-off-by: s3rj1k <evasive.gyron@gmail.com>
  • Loading branch information
s3rj1k committed Apr 13, 2021
1 parent 3d401ad commit 4246eb4
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/minio/highwayhash v1.0.2
github.com/s3rj1k/ninit/pkg/log/logger v0.0.0-00010101000000-000000000000
github.com/sirupsen/logrus v1.8.1
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
k8s.io/api v0.20.5
k8s.io/apimachinery v0.20.5
Expand Down Expand Up @@ -72,6 +73,7 @@ replace (
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c => github.com/onsi/gomega v1.7.0
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff => github.com/spf13/pflag v1.0.5
github.com/stretchr/objx v0.1.0 => github.com/stretchr/objx v0.2.0
github.com/stretchr/testify v1.2.2 => github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.3.0 => github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.4.0 => github.com/stretchr/testify v1.6.1
go.opencensus.io v0.21.0 => go.opencensus.io v0.22.3
Expand Down Expand Up @@ -128,6 +130,7 @@ replace (
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24 => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 => golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
Expand Down
178 changes: 178 additions & 0 deletions pkg/log/wlogrus/logrus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package wlogrus

import (
"fmt"
"path"
"runtime"
"strings"
"sync"

"github.com/s3rj1k/ninit/pkg/capitalise"
"github.com/s3rj1k/ninit/pkg/log/logger"
"github.com/sirupsen/logrus"
)

// LogrusLogger is a package level logger using logrus.
type LogrusLogger struct {
Log *logrus.Logger

level logger.Level
mu sync.Mutex
}

// New creates new Logrus logger.
func New() *LogrusLogger {
l := new(LogrusLogger)

l.Log = logrus.New()

l.Log.SetFormatter(
&logrus.TextFormatter{
DisableColors: true,
DisableSorting: true,
DisableTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
pc := make([]uintptr, 20)
n := runtime.Callers(1, pc)

if n > 0 {
pc = pc[:n]
frames := runtime.CallersFrames(pc)
next := false

for {
frame, more := frames.Next()

if next {
return fmt.Sprintf("%s()", path.Base(frame.Function)), fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line)
}

if f.PC == frame.PC {
next = true
}

if !more {
break
}
}
}

return "", ""
},
},
)

l.Log.SetReportCaller(true)

return l
}

// Logf is unleveled logger.
func (l *LogrusLogger) Logf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Logf(logrus.TraceLevel, strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Tracef is a trace level logger.
func (l *LogrusLogger) Tracef(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Tracef(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Debugf is a debug level logger.
func (l *LogrusLogger) Debugf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Debugf(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Infof is a info level logger.
func (l *LogrusLogger) Infof(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Infof(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Warnf is a warn level logger.
func (l *LogrusLogger) Warnf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Warnf(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Errorf is a error level logger.
func (l *LogrusLogger) Errorf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Errorf(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Fatalf is a fatal level logger.
func (l *LogrusLogger) Fatalf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Fatalf(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// Panicf is a panic level logger.
func (l *LogrusLogger) Panicf(format string, args ...interface{}) {
if l == nil {
panic("logger undefined")
}

l.Log.Panicf(strings.TrimSpace(capitalise.First(fmt.Sprintf(format, args...))))
}

// SetLevel defines maximum level of a logger output verbosity.
func (l *LogrusLogger) SetLevel(level logger.Level) {
if l == nil {
panic("logger undefined")
}

var logrusLevel logrus.Level

switch level {
case logger.TraceLevelLog:
logrusLevel = logrus.TraceLevel
case logger.DebugLevelLog:
logrusLevel = logrus.DebugLevel
case logger.InfoLevelLog:
logrusLevel = logrus.InfoLevel
case logger.WarnLevelLog:
logrusLevel = logrus.WarnLevel
case logger.ErrorLevelLog:
logrusLevel = logrus.ErrorLevel
case logger.FatalLevelLog:
logrusLevel = logrus.FatalLevel
case logger.PanicLevelLog:
logrusLevel = logrus.PanicLevel
}

l.mu.Lock()

l.Log.SetLevel(logrusLevel)
l.level = level

l.mu.Unlock()
}

// GetLevel returns current level of a logger output verbosity.
func (l *LogrusLogger) GetLevel() logger.Level {
return l.level
}

0 comments on commit 4246eb4

Please sign in to comment.