initial commit 2
This commit is contained in:
53
infra/logger/logger.go
Normal file
53
infra/logger/logger.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"io"
|
||||
|
||||
"bitbucket.org/nemt/nemt-portal-api/infra/config"
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Logger is the default application logger compatible with the echo.Logger interface
|
||||
type Logger struct {
|
||||
cfg *config.Config
|
||||
*logrus.Entry
|
||||
}
|
||||
|
||||
// InfoWriter returns the io.Writer for info level
|
||||
func (l *Logger) InfoWriter() io.Writer {
|
||||
return l.WriterLevel(logrus.InfoLevel)
|
||||
}
|
||||
|
||||
// ErrorWriter returns the io.Writer for error level
|
||||
func (l *Logger) ErrorWriter() io.Writer {
|
||||
return l.WriterLevel(logrus.ErrorLevel)
|
||||
}
|
||||
|
||||
// New returns a new Logger instance
|
||||
func New(cfg *config.Config) (*Logger, error) {
|
||||
if cfg.Log.LogToFile {
|
||||
file, err := os.Create(cfg.Log.Path)
|
||||
if err != nil {
|
||||
fmt.Printf("Error to create log file for library: %s\n", err.Error())
|
||||
panic(err)
|
||||
}
|
||||
logrus.SetOutput(file)
|
||||
}
|
||||
|
||||
logrus.SetFormatter(&logrus.JSONFormatter{})
|
||||
|
||||
if cfg.App.Debug {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
} else {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
|
||||
entry := logrus.WithFields(logrus.Fields{
|
||||
"app": cfg.App.Name,
|
||||
})
|
||||
|
||||
return &Logger{cfg, entry}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user