From bbb79f42c97655ea19a903094aeddd1ad898c017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Mon, 28 Mar 2022 21:11:10 +0200 Subject: [PATCH] Add: Logger --- TODO.md | 2 ++ main.go | 10 ++++++++++ model/store_gorm.go | 6 ------ server/app.go | 17 +++++++++-------- server/log.go | 20 ++++++++++++++++++++ server/server.go | 1 + 6 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 server/log.go diff --git a/TODO.md b/TODO.md index df5b24e..284ce37 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,8 @@ ## General > General options - [ ] Flag to select store +- [x] Flag that sets the debug mode +- [ ] Flag to show the version - [ ] Config file (to select store and some other future options) - [ ] Proper logger - [ ] Makefile helper diff --git a/main.go b/main.go index f6f5d59..e6c65b2 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,22 @@ package main import ( + "flag" "fmt" + "github.com/oscarmlage/wikingo/server" ) func main() { fmt.Println("Starting wikingo...") + flag_debug := flag.Bool("d", false, "Enable debug mode") + flag.Parse() + // Logger + server.InitLogger(*flag_debug) + server.Debug.Printf("Yo there") + + fmt.Println("Starting wikingo...") + fmt.Printf("Debug mode: %t\n", *flag_debug) server.Serve() } diff --git a/model/store_gorm.go b/model/store_gorm.go index baee55f..b6fa351 100644 --- a/model/store_gorm.go +++ b/model/store_gorm.go @@ -1,8 +1,6 @@ package model import ( - "fmt" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -19,7 +17,6 @@ func (s *StoreGorm) Open() error { if err != nil { return err } - fmt.Printf("%s\n", s.db.Name()) // Migrate the Schema s.db.AutoMigrate(&Page{}) @@ -36,7 +33,6 @@ func (s *StoreGorm) AddPage(name string, body string) error { if tx.Error != nil { return tx.Error } - fmt.Println("Page created") return nil } @@ -55,7 +51,6 @@ func (s *StoreGorm) GetPage(name string) (Page, error) { if tx.Error != nil { return Page{}, tx.Error } - fmt.Println(page) return page, nil } @@ -65,7 +60,6 @@ func (s *StoreGorm) GetPageVersion(name string, version string) (Page, error) { if tx.Error != nil { return Page{}, tx.Error } - fmt.Println(page) return page, nil } diff --git a/server/app.go b/server/app.go index d2bcde1..d19e25e 100644 --- a/server/app.go +++ b/server/app.go @@ -1,7 +1,6 @@ package server import ( - "fmt" "log" "net/http" "strings" @@ -23,12 +22,12 @@ func WikiPage(c echo.Context) error { page_id := c.Param("page") version := c.Param("version") if version != "" { - fmt.Printf("version: %s", version) + Debug.Printf("version: %s", version) page, err = store.GetPageVersion(page_id, version) if err != nil { // If record not found, show 404 if err.Error() == "record not found" { - return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{ + Debug.Printf("Page not found: %s/%s", page_id, version) return c.Render(http.StatusNotFound, "404.html", map[string]interface{}{ "name": page_id, }) @@ -40,13 +39,14 @@ func WikiPage(c echo.Context) error { if err != nil { // If record not found, show the create if err.Error() == "record not found" { + Debug.Printf("Page not found: %s", page_id) return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{ "name": page_id, }) } } } - fmt.Printf("res: %T\n", page) + Debug.Printf("res: %T\n", page) body := strings.ReplaceAll(page.Body, "\r\n", "\n") unsafe := blackfriday.Run([]byte(body)) html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) @@ -62,10 +62,10 @@ func WikiPageEdit(c echo.Context) error { page_id := c.Param("page") version := c.Param("version") if version != "" { - fmt.Printf("version; %s\n", version) + Debug.Printf("version; %s\n", version) page, err = store.GetPageVersion(page_id, version) } else { - fmt.Println("No version") + Debug.Println("No version") page, err = store.GetPage(page_id) } if err != nil { @@ -75,13 +75,14 @@ func WikiPageEdit(c echo.Context) error { if err != nil { log.Panicln(err.Error()) } + Debug.Printf("Page %s created", page_id) page, err = store.GetPage(page_id) if err != nil { log.Panicln(err) } } } - fmt.Printf("res: %T\n", page) + Debug.Printf("res: %T\n", page) return c.Render(http.StatusOK, "edit.html", map[string]interface{}{ "page": page, }) @@ -104,7 +105,7 @@ func WikiList(c echo.Context) error { if err != nil { log.Panicln(err) } - fmt.Printf("res: %T\n", pages) + Debug.Printf("res: %T\n", pages) return c.Render(http.StatusOK, "list.html", map[string]interface{}{ "pages": pages, }) diff --git a/server/log.go b/server/log.go new file mode 100644 index 0000000..149404a --- /dev/null +++ b/server/log.go @@ -0,0 +1,20 @@ +package server + +import ( + "io" + "log" + "os" +) + +var ( + Debug *log.Logger +) + +func InitLogger(debug bool) { + // Discard policy by default + Debug = log.New(io.Discard, "", 0) + // Enable it only if debug flag was passed + if debug { + Debug = log.New(os.Stderr, "DEBUG: ", log.Ltime|log.Lshortfile) + } +} diff --git a/server/server.go b/server/server.go index 760962c..9ae0f64 100644 --- a/server/server.go +++ b/server/server.go @@ -40,6 +40,7 @@ func Serve() { if err != nil { log.Panicln(err) } + Debug.Printf("test.db open\n") // Echo instance e := echo.New()