Logger refactoring (#90)

This commit is contained in:
konrad 2019-07-20 18:12:10 +00:00 committed by Gitea
parent 15a0963bd1
commit 48826a6ed7
17 changed files with 124 additions and 56 deletions

View File

@ -183,7 +183,7 @@ Sorry for some of them being in German, I'll tranlate them at some point.
* [x] Check if the team/user really exist before updating them on lists/namespaces * [x] Check if the team/user really exist before updating them on lists/namespaces
* [x] Refactor config handling: Custom type "key" or so which holds the viper const and then mixins on that type to get the values from viper * [x] Refactor config handling: Custom type "key" or so which holds the viper const and then mixins on that type to get the values from viper
* [x] Less files, but with some kind of logic * [x] Less files, but with some kind of logic
* [ ] Have extra functions for logging to call so it is possible to call `log.Info` instead of `log.Log.Info` * [x] Have extra functions for logging to call so it is possible to call `log.Info` instead of `log.Log.Info`
* [ ] `GetUserByID` and the likes should return pointers * [ ] `GetUserByID` and the likes should return pointers
### Linters ### Linters

View File

@ -72,7 +72,7 @@ func initialize() {
// Set Engine // Set Engine
err := models.SetEngine() err := models.SetEngine()
if err != nil { if err != nil {
log.Log.Fatal(err.Error()) log.Fatal(err.Error())
} }
// Start the mail daemon // Start the mail daemon

View File

@ -62,7 +62,7 @@ var webCmd = &cobra.Command{
<-quit <-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
log.Log.Infof("Shutting down...") log.Infof("Shutting down...")
if err := e.Shutdown(ctx); err != nil { if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err) e.Logger.Fatal(err)
} }

View File

@ -35,8 +35,8 @@ const WebFmt = `${time_rfc3339_nano}: WEB ` + "\t" + `▶ ${remote_ip} ${id} ${m
// Fmt is the general log format // Fmt is the general log format
const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}` const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}`
// Log is the handler for the logger // loginstance is the instance of the logger which is used under the hood to log
var Log = logging.MustGetLogger("vikunja") var logInstance = logging.MustGetLogger("vikunja")
// InitLogger initializes the global log handler // InitLogger initializes the global log handler
func InitLogger() { func InitLogger() {
@ -100,3 +100,71 @@ func GetLogWriter(logfile string) (writer io.Writer) {
} }
return return
} }
// GetLogger returns the logging instance. DO NOT USE THIS TO LOG STUFF.
func GetLogger() *logging.Logger {
return logInstance
}
/////
// The following functions are to be used as an "eye-candy", so one can just write log.Error() instead of log.Log.Error()
// Debug is for debug messages
func Debug(args ...interface{}) {
logInstance.Debug(args)
}
// Debugf is for debug messages
func Debugf(format string, args ...interface{}) {
logInstance.Debugf(format, args)
}
// Info is for info messages
func Info(args ...interface{}) {
logInstance.Info(args)
}
// Infof is for info messages
func Infof(format string, args ...interface{}) {
logInstance.Infof(format, args)
}
// Error is for error messages
func Error(args ...interface{}) {
logInstance.Error(args)
}
// Errorf is for error messages
func Errorf(format string, args ...interface{}) {
logInstance.Errorf(format, args)
}
// Warning is for warning messages
func Warning(args ...interface{}) {
logInstance.Warning(args)
}
// Warningf is for warning messages
func Warningf(format string, args ...interface{}) {
logInstance.Warningf(format, args)
}
// Critical is for critical messages
func Critical(args ...interface{}) {
logInstance.Critical(args)
}
// Criticalf is for critical messages
func Criticalf(format string, args ...interface{}) {
logInstance.Critical(format, args)
}
// Fatal is for fatal messages
func Fatal(args ...interface{}) {
logInstance.Fatal(args)
}
// Fatalf is for fatal messages
func Fatalf(format string, args ...interface{}) {
logInstance.Fatal(format, args)
}

View File

@ -36,7 +36,7 @@ func StartMailDaemon() {
} }
if config.MailerHost.GetString() == "" { if config.MailerHost.GetString() == "" {
log.Log.Warning("Mailer seems to be not configured! Please see the config docs for more details.") log.Warning("Mailer seems to be not configured! Please see the config docs for more details.")
return return
} }
@ -55,21 +55,21 @@ func StartMailDaemon() {
} }
if !open { if !open {
if s, err = d.Dial(); err != nil { if s, err = d.Dial(); err != nil {
log.Log.Error("Error during connect to smtp server: %s", err) log.Error("Error during connect to smtp server: %s", err)
} }
open = true open = true
} }
if err := gomail.Send(s, m); err != nil { if err := gomail.Send(s, m); err != nil {
log.Log.Error("Error when sending mail: %s", err) log.Error("Error when sending mail: %s", err)
} }
// Close the connection to the SMTP server if no email was sent in // Close the connection to the SMTP server if no email was sent in
// the last 30 seconds. // the last 30 seconds.
case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second): case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second):
if open { if open {
if err := s.Close(); err != nil { if err := s.Close(); err != nil {
log.Log.Error("Error closing the mail server connection: %s\n", err) log.Error("Error closing the mail server connection: %s\n", err)
} }
log.Log.Infof("Closed connection to mailserver") log.Infof("Closed connection to mailserver")
open = false open = false
} }
} }

View File

@ -88,7 +88,7 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{})
t, err := vfstemplate.ParseGlob(static.Templates, nil, "*.tmpl") t, err := vfstemplate.ParseGlob(static.Templates, nil, "*.tmpl")
if err != nil { if err != nil {
log.Log.Errorf("SendMailWithTemplate: ParseGlob: %v", err) log.Errorf("SendMailWithTemplate: ParseGlob: %v", err)
return return
} }
@ -98,12 +98,12 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{})
data["FrontendURL"] = config.ServiceFrontendurl.GetString() data["FrontendURL"] = config.ServiceFrontendurl.GetString()
if err := t.ExecuteTemplate(&htmlContent, tpl+".html.tmpl", data); err != nil { if err := t.ExecuteTemplate(&htmlContent, tpl+".html.tmpl", data); err != nil {
log.Log.Errorf("ExecuteTemplate: %v", err) log.Errorf("ExecuteTemplate: %v", err)
return return
} }
if err := t.ExecuteTemplate(&plainContent, tpl+".plain.tmpl", data); err != nil { if err := t.ExecuteTemplate(&plainContent, tpl+".plain.tmpl", data); err != nil {
log.Log.Errorf("ExecuteTemplate: %v", err) log.Errorf("ExecuteTemplate: %v", err)
return return
} }

View File

@ -45,7 +45,7 @@ func init() {
allActiveUsers, err := GetActiveUsers() allActiveUsers, err := GetActiveUsers()
if err != nil { if err != nil {
log.Log.Error(err.Error()) log.Error(err.Error())
} }
activeUsersCount := 0 activeUsersCount := 0
for _, u := range allActiveUsers { for _, u := range allActiveUsers {

View File

@ -117,11 +117,11 @@ func UpdateCount(update int64, key string) {
} }
oldtotal, err := GetCount(key) oldtotal, err := GetCount(key)
if err != nil { if err != nil {
log.Log.Error(err.Error()) log.Error(err.Error())
} }
err = SetCount(oldtotal+update, key) err = SetCount(oldtotal+update, key)
if err != nil { if err != nil {
log.Log.Error(err.Error()) log.Error(err.Error())
} }
} }

View File

@ -39,7 +39,7 @@ func initMigration(x *xorm.Engine) *xormigrate.Xormigrate {
var err error var err error
x, err = db.CreateDBEngine() x, err = db.CreateDBEngine()
if err != nil { if err != nil {
log.Log.Criticalf("Could not connect to db: %v", err.Error()) log.Criticalf("Could not connect to db: %v", err.Error())
return nil return nil
} }
} }
@ -61,21 +61,21 @@ func Migrate(x *xorm.Engine) {
m := initMigration(x) m := initMigration(x)
err := m.Migrate() err := m.Migrate()
if err != nil { if err != nil {
log.Log.Fatalf("Migration failed: %v", err) log.Fatalf("Migration failed: %v", err)
} }
log.Log.Info("Ran all migrations successfully.") log.Info("Ran all migrations successfully.")
} }
// ListMigrations pretty-prints a list with all migrations. // ListMigrations pretty-prints a list with all migrations.
func ListMigrations() { func ListMigrations() {
x, err := db.CreateDBEngine() x, err := db.CreateDBEngine()
if err != nil { if err != nil {
log.Log.Fatalf("Could not connect to db: %v", err.Error()) log.Fatalf("Could not connect to db: %v", err.Error())
} }
ms := []*xormigrate.Migration{} ms := []*xormigrate.Migration{}
err = x.Find(&ms) err = x.Find(&ms)
if err != nil { if err != nil {
log.Log.Fatalf("Error getting migration table: %v", err.Error()) log.Fatalf("Error getting migration table: %v", err.Error())
} }
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(os.Stdout)
@ -95,9 +95,9 @@ func Rollback(migrationID string) {
m := initMigration(nil) m := initMigration(nil)
err := m.RollbackTo(migrationID) err := m.RollbackTo(migrationID)
if err != nil { if err != nil {
log.Log.Fatalf("Could not rollback: %v", err) log.Fatalf("Could not rollback: %v", err)
} }
log.Log.Info("Rolled back successfully.") log.Info("Rolled back successfully.")
} }
// Deletes a column from a table. All arguments are strings, to let them be standalone and not depending on any struct. // Deletes a column from a table. All arguments are strings, to let them be standalone and not depending on any struct.
@ -105,14 +105,14 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error {
switch config.DatabaseType.GetString() { switch config.DatabaseType.GetString() {
case "sqlite": case "sqlite":
log.Log.Warning("Unable to drop columns in SQLite") log.Warning("Unable to drop columns in SQLite")
case "mysql": case "mysql":
_, err := x.Exec("ALTER TABLE " + tableName + " DROP COLUMN " + col) _, err := x.Exec("ALTER TABLE " + tableName + " DROP COLUMN " + col)
if err != nil { if err != nil {
return err return err
} }
default: default:
log.Log.Fatal("Unknown db.") log.Fatal("Unknown db.")
} }
return nil return nil
} }
@ -121,14 +121,14 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error {
func modifyColumn(x *xorm.Engine, tableName, col, newDefinition string) error { func modifyColumn(x *xorm.Engine, tableName, col, newDefinition string) error {
switch config.DatabaseType.GetString() { switch config.DatabaseType.GetString() {
case "sqlite": case "sqlite":
log.Log.Warning("Unable to modify columns in SQLite") log.Warning("Unable to modify columns in SQLite")
case "mysql": case "mysql":
_, err := x.Exec("ALTER TABLE " + tableName + " MODIFY COLUMN " + col + " " + newDefinition) _, err := x.Exec("ALTER TABLE " + tableName + " MODIFY COLUMN " + col + " " + newDefinition)
if err != nil { if err != nil {
return err return err
} }
default: default:
log.Log.Fatal("Unknown db.") log.Fatal("Unknown db.")
} }
return nil return nil
} }

View File

@ -55,7 +55,7 @@ func GetTables() []interface{} {
func SetEngine() (err error) { func SetEngine() (err error) {
x, err = db.CreateDBEngine() x, err = db.CreateDBEngine()
if err != nil { if err != nil {
log.Log.Criticalf("Could not connect to db: %v", err.Error()) log.Criticalf("Could not connect to db: %v", err.Error())
return return
} }
@ -71,7 +71,7 @@ func SetEngine() (err error) {
x.SetDefaultCacher(cacher) x.SetDefaultCacher(cacher)
gob.Register(GetTables()) gob.Register(GetTables())
default: default:
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.") log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
} }
} }

View File

@ -42,7 +42,7 @@ func SetupTests(pathToRoot string) {
var err error var err error
fixturesDir := filepath.Join(pathToRoot, "pkg", "models", "fixtures") fixturesDir := filepath.Join(pathToRoot, "pkg", "models", "fixtures")
if err = createTestEngine(fixturesDir); err != nil { if err = createTestEngine(fixturesDir); err != nil {
log.Log.Fatalf("Error creating test engine: %v\n", err) log.Fatalf("Error creating test engine: %v\n", err)
} }
// Start the pseudo mail queue // Start the pseudo mail queue
@ -50,7 +50,7 @@ func SetupTests(pathToRoot string) {
// Create test database // Create test database
if err = LoadFixtures(); err != nil { if err = LoadFixtures(); err != nil {
log.Log.Fatalf("Error preparing test database: %v", err.Error()) log.Fatalf("Error preparing test database: %v", err.Error())
} }
} }

View File

@ -31,7 +31,7 @@ func InitRedis() {
} }
if config.RedisHost.GetString() == "" { if config.RedisHost.GetString() == "" {
log.Log.Fatal("No redis host provided.") log.Fatal("No redis host provided.")
} }
r = redis.NewClient(&redis.Options{ r = redis.NewClient(&redis.Options{
@ -42,9 +42,9 @@ func InitRedis() {
err := r.Ping().Err() err := r.Ping().Err()
if err != nil { if err != nil {
log.Log.Fatal(err.Error()) log.Fatal(err.Error())
} }
log.Log.Debug("Redis initialized") log.Debug("Redis initialized")
} }
// GetRedis returns a pointer to a redis client // GetRedis returns a pointer to a redis client

View File

@ -30,12 +30,12 @@ func DocsJSON(c echo.Context) error {
doc, err := swag.ReadDoc() doc, err := swag.ReadDoc()
if err != nil { if err != nil {
log.Log.Error(err.Error()) log.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError) return echo.NewHTTPError(http.StatusInternalServerError)
} }
_, err = c.Response().Write([]byte(doc)) _, err = c.Response().Write([]byte(doc))
if err != nil { if err != nil {
log.Log.Error(err.Error()) log.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError) return echo.NewHTTPError(http.StatusInternalServerError)
} }

View File

@ -48,7 +48,7 @@ func ListHandler(c echo.Context) error {
u, err := getBasicAuthUserFromContext(c) u, err := getBasicAuthUserFromContext(c)
if err != nil { if err != nil {
log.Log.Error(err) log.Error(err)
return echo.ErrInternalServerError return echo.ErrInternalServerError
} }
@ -66,13 +66,13 @@ func ListHandler(c echo.Context) error {
if vtodo != "" && strings.HasPrefix(vtodo, `BEGIN:VCALENDAR`) { if vtodo != "" && strings.HasPrefix(vtodo, `BEGIN:VCALENDAR`) {
storage.task, err = parseTaskFromVTODO(vtodo) storage.task, err = parseTaskFromVTODO(vtodo)
if err != nil { if err != nil {
log.Log.Error(err) log.Error(err)
return echo.ErrInternalServerError return echo.ErrInternalServerError
} }
} }
log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) log.Debugf("[CALDAV] Request Body: %v\n", string(body))
log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
caldav.SetupStorage(storage) caldav.SetupStorage(storage)
caldav.SetupUser("dav/lists") caldav.SetupUser("dav/lists")
@ -91,7 +91,7 @@ func TaskHandler(c echo.Context) error {
u, err := getBasicAuthUserFromContext(c) u, err := getBasicAuthUserFromContext(c)
if err != nil { if err != nil {
log.Log.Error(err) log.Error(err)
return echo.ErrInternalServerError return echo.ErrInternalServerError
} }
@ -114,7 +114,7 @@ func TaskHandler(c echo.Context) error {
func PrincipalHandler(c echo.Context) error { func PrincipalHandler(c echo.Context) error {
u, err := getBasicAuthUserFromContext(c) u, err := getBasicAuthUserFromContext(c)
if err != nil { if err != nil {
log.Log.Error(err) log.Error(err)
return echo.ErrInternalServerError return echo.ErrInternalServerError
} }
@ -128,8 +128,8 @@ func PrincipalHandler(c echo.Context) error {
// Restore the io.ReadCloser to its original state // Restore the io.ReadCloser to its original state
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body)) c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) log.Debugf("[CALDAV] Request Body: %v\n", string(body))
log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
caldav.SetupStorage(storage) caldav.SetupStorage(storage)
caldav.SetupUser("dav/principals/" + u.Username) caldav.SetupUser("dav/principals/" + u.Username)
@ -144,7 +144,7 @@ func PrincipalHandler(c echo.Context) error {
func EntryHandler(c echo.Context) error { func EntryHandler(c echo.Context) error {
u, err := getBasicAuthUserFromContext(c) u, err := getBasicAuthUserFromContext(c)
if err != nil { if err != nil {
log.Log.Error(err) log.Error(err)
return echo.ErrInternalServerError return echo.ErrInternalServerError
} }
@ -158,8 +158,8 @@ func EntryHandler(c echo.Context) error {
// Restore the io.ReadCloser to its original state // Restore the io.ReadCloser to its original state
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body)) c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) log.Debugf("[CALDAV] Request Body: %v\n", string(body))
log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
caldav.SetupStorage(storage) caldav.SetupStorage(storage)
caldav.SetupUser("dav/principals/" + u.Username) caldav.SetupUser("dav/principals/" + u.Username)

View File

@ -387,7 +387,7 @@ func (vcls *VikunjaCaldavListStorage) getListRessource(isCollection bool) (rr Vi
return return
} }
if !can { if !can {
log.Log.Errorf("User %v tried to access a caldav resource (List %v) which they are not allowed to access", vcls.user.Username, vcls.list.ID) log.Errorf("User %v tried to access a caldav resource (List %v) which they are not allowed to access", vcls.user.Username, vcls.list.ID)
return rr, models.ErrUserDoesNotHaveAccessToList{ListID: vcls.list.ID} return rr, models.ErrUserDoesNotHaveAccessToList{ListID: vcls.list.ID}
} }
err = vcls.list.ReadOne() err = vcls.list.ReadOne()

View File

@ -118,7 +118,7 @@ func caldavTimeToUnixTimestamp(tstring string) int64 {
t, err := time.Parse(caldav.DateFormat, tstring) t, err := time.Parse(caldav.DateFormat, tstring)
if err != nil { if err != nil {
log.Log.Warningf("Error while parsing caldav time %s to unix time: %s", tstring, err) log.Warningf("Error while parsing caldav time %s to unix time: %s", tstring, err)
return 0 return 0
} }
return t.Unix() return t.Unix()

View File

@ -113,7 +113,7 @@ func NewEcho() *echo.Echo {
return models.GetCurrentUser(c) return models.GetCurrentUser(c)
}, },
}) })
handler.SetLoggingProvider(log.Log) handler.SetLoggingProvider(log.GetLogger())
return e return e
} }
@ -158,7 +158,7 @@ func registerAPIRoutes(a *echo.Group) {
if config.ServiceEnableMetrics.GetBool() { if config.ServiceEnableMetrics.GetBool() {
if !config.RedisEnabled.GetBool() { if !config.RedisEnabled.GetBool() {
log.Log.Fatal("You have to enable redis in order to use metrics") log.Fatal("You have to enable redis in order to use metrics")
} }
metrics.InitMetrics() metrics.InitMetrics()
@ -193,16 +193,16 @@ func registerAPIRoutes(a *echo.Group) {
// Set initial totals // Set initial totals
total, err := models.GetTotalCount(c.Type) total, err := models.GetTotalCount(c.Type)
if err != nil { if err != nil {
log.Log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err) log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
} }
if err := metrics.SetCount(total, c.Rediskey); err != nil { if err := metrics.SetCount(total, c.Rediskey); err != nil {
log.Log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err) log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
} }
} }
// init active users, sometimes we'll have garbage from previous runs in redis instead // init active users, sometimes we'll have garbage from previous runs in redis instead
if err := metrics.SetActiveUsers([]*metrics.ActiveUser{}); err != nil { if err := metrics.SetActiveUsers([]*metrics.ActiveUser{}); err != nil {
log.Log.Fatalf("Could not set initial count for active users, error was %s", err) log.Fatalf("Could not set initial count for active users, error was %s", err)
} }
a.GET("/metrics", echo.WrapHandler(promhttp.Handler())) a.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
@ -229,7 +229,7 @@ func registerAPIRoutes(a *echo.Group) {
// Update currently active users // Update currently active users
if err := models.UpdateActiveUsersFromContext(c); err != nil { if err := models.UpdateActiveUsersFromContext(c); err != nil {
log.Log.Error(err) log.Error(err)
return next(c) return next(c)
} }
return next(c) return next(c)
@ -411,7 +411,7 @@ func caldavBasicAuth(username, password string, c echo.Context) (bool, error) {
} }
u, err := models.CheckUserCredentials(creds) u, err := models.CheckUserCredentials(creds)
if err != nil { if err != nil {
log.Log.Errorf("Error during basic auth for caldav: %v", err) log.Errorf("Error during basic auth for caldav: %v", err)
return false, nil return false, nil
} }
// Save the user in echo context for later use // Save the user in echo context for later use