diff --git a/Featurecreep.md b/Featurecreep.md index 6a4f42cc..558956ad 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -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] 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 -* [ ] 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 ### Linters diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 87fbc95d..af411b13 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -72,7 +72,7 @@ func initialize() { // Set Engine err := models.SetEngine() if err != nil { - log.Log.Fatal(err.Error()) + log.Fatal(err.Error()) } // Start the mail daemon diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 4661006f..c0550d9d 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -62,7 +62,7 @@ var webCmd = &cobra.Command{ <-quit ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - log.Log.Infof("Shutting down...") + log.Infof("Shutting down...") if err := e.Shutdown(ctx); err != nil { e.Logger.Fatal(err) } diff --git a/pkg/log/logging.go b/pkg/log/logging.go index ca28800c..d957b71c 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -35,8 +35,8 @@ const WebFmt = `${time_rfc3339_nano}: WEB ` + "\t" + `▶ ${remote_ip} ${id} ${m // Fmt is the general log format const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}` -// Log is the handler for the logger -var Log = logging.MustGetLogger("vikunja") +// loginstance is the instance of the logger which is used under the hood to log +var logInstance = logging.MustGetLogger("vikunja") // InitLogger initializes the global log handler func InitLogger() { @@ -100,3 +100,71 @@ func GetLogWriter(logfile string) (writer io.Writer) { } 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) +} diff --git a/pkg/mail/mail.go b/pkg/mail/mail.go index 8cfff9d5..c9be440e 100644 --- a/pkg/mail/mail.go +++ b/pkg/mail/mail.go @@ -36,7 +36,7 @@ func StartMailDaemon() { } 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 } @@ -55,21 +55,21 @@ func StartMailDaemon() { } if !open { 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 } 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 // the last 30 seconds. case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second): if open { 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 } } diff --git a/pkg/mail/send_mail.go b/pkg/mail/send_mail.go index fed53efb..20979a77 100644 --- a/pkg/mail/send_mail.go +++ b/pkg/mail/send_mail.go @@ -88,7 +88,7 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{}) t, err := vfstemplate.ParseGlob(static.Templates, nil, "*.tmpl") if err != nil { - log.Log.Errorf("SendMailWithTemplate: ParseGlob: %v", err) + log.Errorf("SendMailWithTemplate: ParseGlob: %v", err) return } @@ -98,12 +98,12 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{}) data["FrontendURL"] = config.ServiceFrontendurl.GetString() if err := t.ExecuteTemplate(&htmlContent, tpl+".html.tmpl", data); err != nil { - log.Log.Errorf("ExecuteTemplate: %v", err) + log.Errorf("ExecuteTemplate: %v", err) return } if err := t.ExecuteTemplate(&plainContent, tpl+".plain.tmpl", data); err != nil { - log.Log.Errorf("ExecuteTemplate: %v", err) + log.Errorf("ExecuteTemplate: %v", err) return } diff --git a/pkg/metrics/active_users.go b/pkg/metrics/active_users.go index f835f757..9e5feb9d 100644 --- a/pkg/metrics/active_users.go +++ b/pkg/metrics/active_users.go @@ -45,7 +45,7 @@ func init() { allActiveUsers, err := GetActiveUsers() if err != nil { - log.Log.Error(err.Error()) + log.Error(err.Error()) } activeUsersCount := 0 for _, u := range allActiveUsers { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index eafdd4d0..236f56f9 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -117,11 +117,11 @@ func UpdateCount(update int64, key string) { } oldtotal, err := GetCount(key) if err != nil { - log.Log.Error(err.Error()) + log.Error(err.Error()) } err = SetCount(oldtotal+update, key) if err != nil { - log.Log.Error(err.Error()) + log.Error(err.Error()) } } diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index 70ba8625..91a04baa 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -39,7 +39,7 @@ func initMigration(x *xorm.Engine) *xormigrate.Xormigrate { var err error x, err = db.CreateDBEngine() 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 } } @@ -61,21 +61,21 @@ func Migrate(x *xorm.Engine) { m := initMigration(x) err := m.Migrate() 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. func ListMigrations() { x, err := db.CreateDBEngine() 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{} err = x.Find(&ms) 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) @@ -95,9 +95,9 @@ func Rollback(migrationID string) { m := initMigration(nil) err := m.RollbackTo(migrationID) 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. @@ -105,14 +105,14 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error { switch config.DatabaseType.GetString() { case "sqlite": - log.Log.Warning("Unable to drop columns in SQLite") + log.Warning("Unable to drop columns in SQLite") case "mysql": _, err := x.Exec("ALTER TABLE " + tableName + " DROP COLUMN " + col) if err != nil { return err } default: - log.Log.Fatal("Unknown db.") + log.Fatal("Unknown db.") } 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 { switch config.DatabaseType.GetString() { case "sqlite": - log.Log.Warning("Unable to modify columns in SQLite") + log.Warning("Unable to modify columns in SQLite") case "mysql": _, err := x.Exec("ALTER TABLE " + tableName + " MODIFY COLUMN " + col + " " + newDefinition) if err != nil { return err } default: - log.Log.Fatal("Unknown db.") + log.Fatal("Unknown db.") } return nil } diff --git a/pkg/models/models.go b/pkg/models/models.go index d781997d..79683501 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -55,7 +55,7 @@ func GetTables() []interface{} { func SetEngine() (err error) { x, err = db.CreateDBEngine() 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 } @@ -71,7 +71,7 @@ func SetEngine() (err error) { x.SetDefaultCacher(cacher) gob.Register(GetTables()) 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.") } } diff --git a/pkg/models/unit_tests.go b/pkg/models/unit_tests.go index e869a71c..dc41ab8a 100644 --- a/pkg/models/unit_tests.go +++ b/pkg/models/unit_tests.go @@ -42,7 +42,7 @@ func SetupTests(pathToRoot string) { var err error fixturesDir := filepath.Join(pathToRoot, "pkg", "models", "fixtures") 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 @@ -50,7 +50,7 @@ func SetupTests(pathToRoot string) { // Create test database if err = LoadFixtures(); err != nil { - log.Log.Fatalf("Error preparing test database: %v", err.Error()) + log.Fatalf("Error preparing test database: %v", err.Error()) } } diff --git a/pkg/red/redis.go b/pkg/red/redis.go index c13371d1..942fc01e 100644 --- a/pkg/red/redis.go +++ b/pkg/red/redis.go @@ -31,7 +31,7 @@ func InitRedis() { } if config.RedisHost.GetString() == "" { - log.Log.Fatal("No redis host provided.") + log.Fatal("No redis host provided.") } r = redis.NewClient(&redis.Options{ @@ -42,9 +42,9 @@ func InitRedis() { err := r.Ping().Err() 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 diff --git a/pkg/routes/api/v1/docs.go b/pkg/routes/api/v1/docs.go index 561b6551..9f6884c1 100644 --- a/pkg/routes/api/v1/docs.go +++ b/pkg/routes/api/v1/docs.go @@ -30,12 +30,12 @@ func DocsJSON(c echo.Context) error { doc, err := swag.ReadDoc() if err != nil { - log.Log.Error(err.Error()) + log.Error(err.Error()) return echo.NewHTTPError(http.StatusInternalServerError) } _, err = c.Response().Write([]byte(doc)) if err != nil { - log.Log.Error(err.Error()) + log.Error(err.Error()) return echo.NewHTTPError(http.StatusInternalServerError) } diff --git a/pkg/routes/caldav/handler.go b/pkg/routes/caldav/handler.go index 7b73135f..87c95bc9 100644 --- a/pkg/routes/caldav/handler.go +++ b/pkg/routes/caldav/handler.go @@ -48,7 +48,7 @@ func ListHandler(c echo.Context) error { u, err := getBasicAuthUserFromContext(c) if err != nil { - log.Log.Error(err) + log.Error(err) return echo.ErrInternalServerError } @@ -66,13 +66,13 @@ func ListHandler(c echo.Context) error { if vtodo != "" && strings.HasPrefix(vtodo, `BEGIN:VCALENDAR`) { storage.task, err = parseTaskFromVTODO(vtodo) if err != nil { - log.Log.Error(err) + log.Error(err) return echo.ErrInternalServerError } } - log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) - log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) + log.Debugf("[CALDAV] Request Body: %v\n", string(body)) + log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) caldav.SetupStorage(storage) caldav.SetupUser("dav/lists") @@ -91,7 +91,7 @@ func TaskHandler(c echo.Context) error { u, err := getBasicAuthUserFromContext(c) if err != nil { - log.Log.Error(err) + log.Error(err) return echo.ErrInternalServerError } @@ -114,7 +114,7 @@ func TaskHandler(c echo.Context) error { func PrincipalHandler(c echo.Context) error { u, err := getBasicAuthUserFromContext(c) if err != nil { - log.Log.Error(err) + log.Error(err) return echo.ErrInternalServerError } @@ -128,8 +128,8 @@ func PrincipalHandler(c echo.Context) error { // Restore the io.ReadCloser to its original state c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body)) - log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) - log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) + log.Debugf("[CALDAV] Request Body: %v\n", string(body)) + log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) caldav.SetupStorage(storage) caldav.SetupUser("dav/principals/" + u.Username) @@ -144,7 +144,7 @@ func PrincipalHandler(c echo.Context) error { func EntryHandler(c echo.Context) error { u, err := getBasicAuthUserFromContext(c) if err != nil { - log.Log.Error(err) + log.Error(err) return echo.ErrInternalServerError } @@ -158,8 +158,8 @@ func EntryHandler(c echo.Context) error { // Restore the io.ReadCloser to its original state c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body)) - log.Log.Debugf("[CALDAV] Request Body: %v\n", string(body)) - log.Log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) + log.Debugf("[CALDAV] Request Body: %v\n", string(body)) + log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header) caldav.SetupStorage(storage) caldav.SetupUser("dav/principals/" + u.Username) diff --git a/pkg/routes/caldav/listStorageProvider.go b/pkg/routes/caldav/listStorageProvider.go index 7d0a4dd3..432e2682 100644 --- a/pkg/routes/caldav/listStorageProvider.go +++ b/pkg/routes/caldav/listStorageProvider.go @@ -387,7 +387,7 @@ func (vcls *VikunjaCaldavListStorage) getListRessource(isCollection bool) (rr Vi return } 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} } err = vcls.list.ReadOne() diff --git a/pkg/routes/caldav/parsing.go b/pkg/routes/caldav/parsing.go index 088525e9..121fdf71 100644 --- a/pkg/routes/caldav/parsing.go +++ b/pkg/routes/caldav/parsing.go @@ -118,7 +118,7 @@ func caldavTimeToUnixTimestamp(tstring string) int64 { t, err := time.Parse(caldav.DateFormat, tstring) 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 t.Unix() diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index b10618fa..93986b09 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -113,7 +113,7 @@ func NewEcho() *echo.Echo { return models.GetCurrentUser(c) }, }) - handler.SetLoggingProvider(log.Log) + handler.SetLoggingProvider(log.GetLogger()) return e } @@ -158,7 +158,7 @@ func registerAPIRoutes(a *echo.Group) { if config.ServiceEnableMetrics.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() @@ -193,16 +193,16 @@ func registerAPIRoutes(a *echo.Group) { // Set initial totals total, err := models.GetTotalCount(c.Type) 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 { - 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 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())) @@ -229,7 +229,7 @@ func registerAPIRoutes(a *echo.Group) { // Update currently active users if err := models.UpdateActiveUsersFromContext(c); err != nil { - log.Log.Error(err) + log.Error(err) 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) 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 } // Save the user in echo context for later use