1
0
mirror of https://github.com/taigrr/log-socket synced 2025-01-18 04:53:14 -08:00

Initial commit based on logrus

This commit is contained in:
Tai Groot 2021-04-08 15:12:07 -07:00
commit cbd4896d05
Signed by: taigrr
GPG Key ID: D00C269A87614812
3 changed files with 126 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module bitbucket.org/cellg/lambo-log-socket
go 1.16
require github.com/sirupsen/logrus v1.8.1

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

111
main.go Normal file
View File

@ -0,0 +1,111 @@
package lambo_log_socket
import (
"fmt"
"runtime"
"strings"
"github.com/sirupsen/logrus"
)
type Level int
const (
LTrace Level = iota
LDebug
LInfo
LWarn
LError
LPanic
LFatal
)
var logLevel Level
var logger = logrus.New()
func init() {
logger.SetLevel(logrus.DebugLevel)
}
// SetLogLevel set log level of logger
func SetLogLevel(level Level) {
logLevel = level
}
// Trace prints out logs on trace level
func Trace(args ...interface{}) {
if logLevel >= LTrace {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Debug(args...)
}
}
// Debug prints out logs on debug level
func Debug(args ...interface{}) {
if logLevel >= LDebug {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Debug(args...)
}
}
// Info prints out logs on info level
func Info(args ...interface{}) {
if logLevel >= LInfo {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Info(args...)
}
}
// Warn prints out logs on warn level
func Warn(args ...interface{}) {
if logLevel >= LWarn {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Warn(args...)
}
}
// Error prints out logs on error level
func Error(args ...interface{}) {
if logLevel >= LError {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Error(args...)
}
}
// Fatal prints out logs on fatal level
func Fatal(args ...interface{}) {
if logLevel >= LFatal {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Fatal(args...)
}
}
// Panic prints out logs on panic level
func Panic(args ...interface{}) {
if logLevel >= LPanic {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Panic(args...)
}
}
// fileInfo for getting which line in which file
func fileInfo(skip int) string {
_, file, line, ok := runtime.Caller(skip)
if !ok {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return fmt.Sprintf("%s:%d", file, line)
}