init commit

This commit is contained in:
alexcarrdev 2021-03-29 23:27:40 -04:00
parent 4b0a7f6a9a
commit 0fae26a780
3 changed files with 59 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module repo.carr.codes/alex/logger
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=

44
logger.go Normal file
View File

@ -0,0 +1,44 @@
package logger
import (
"fmt"
"go/build"
"os"
"sync"
"github.com/sirupsen/logrus"
)
var once sync.Once
var instance *logrus.Logger
func Get() logrus.Logger {
// once.Do(func() {
// instance = initializeLogger()
// })
return *instance
}
func Init(logFile string) {
log := logrus.New()
path := build.Default.GOPATH + "/log/"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModeDir)
}
var logpath = path + logFile
fmt.Println(logpath)
f, err := os.OpenFile(logpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
Formatter := new(logrus.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
log.SetFormatter(Formatter)
if err != nil {
fmt.Println(err)
} else {
log.SetOutput(f)
}
instance = log
//return *log
}