diff --git a/api.go b/api.go index bddd34b..402f14d 100644 --- a/api.go +++ b/api.go @@ -6,10 +6,10 @@ import ( //"context" "fmt" - "log" "github.com/go-chi/chi/v5" "github.com/ggicci/httpin" + "github.com/rs/zerolog/log" ) const DEFAULT_RESULT_COUNT = 50; @@ -42,15 +42,19 @@ func getTransactions(w http.ResponseWriter, req *http.Request) { err := db_get_transactions(&transactions, input) if err != nil { - log.Print("Fatal error in getTransactions from db_get_transactions") - log.Fatal(err) + + log.Fatal(). + Err(err). + Msg("Fatal error in getTransactions from db_get_transactions") } for _, trns := range transactions { //bytes, err := json.Marshal(trns) bytes, err := json.MarshalIndent(trns, "", "\t") if err != nil { - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Could not marshal json") } fmt.Fprintf(w, string(bytes)) } @@ -61,7 +65,9 @@ func newTransaction(w http.ResponseWriter, req *http.Request) { var t Transaction err := decoder.Decode(&t) if err != nil { - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Could not decode incoming post data") } //fmt.Fprintf(w, "New transaction created for Account: %d, with an Amount of: %s", // t.Account, t.Amount) diff --git a/db.go b/db.go index 30a9087..552d856 100644 --- a/db.go +++ b/db.go @@ -2,18 +2,19 @@ package main import ( "fmt" - "log" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" "github.com/jmoiron/sqlx" + "github.com/rs/zerolog/log" ) func db_get_transactions(transactions *[]Transaction,r *GetTransactionPaginationInput) (error) { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { - log.Print("Fatal error in db_get_transactions\nCannot connect to server") - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Fatal error in db_get_transactions\nCannot connect to server") } defer db.Close() @@ -33,8 +34,10 @@ func db_get_transactions(transactions *[]Transaction,r *GetTransactionPagination func db_new_transaction(transaction Transaction) (error) { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { - log.Print("Fatal error in db_get_transactions\nCannot connect to server") - log.Fatal(err) + log.Info() + log.Fatal(). + Err(err). + Msg("Fatal error in db_get_transactions\nCannot connect to server") } defer db.Close() @@ -45,7 +48,9 @@ func db_new_transaction(transaction Transaction) (error) { "VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)", transaction) if err != nil { - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Could not exec insert db query") } return nil } diff --git a/go.mod b/go.mod index 1667df6..9695e91 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,12 @@ require ( github.com/jmoiron/sqlx v1.3.5 github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.6 + github.com/rs/zerolog v1.31.0 ) -require github.com/ggicci/owl v0.4.0 // indirect +require ( + github.com/ggicci/owl v0.4.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + golang.org/x/sys v0.12.0 // indirect +) diff --git a/go.sum b/go.sum index 7b05676..1169d8b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 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/ggicci/httpin v0.14.2 h1:rGrG/OgXg3XZbRBBcqf8TSeu1nW9XX6L9F42XGhwgRQ= @@ -8,6 +9,7 @@ github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= @@ -17,11 +19,24 @@ github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNE github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 3efbf41..67e4c64 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,21 @@ package main import ( - "nickiel.net/recount_server/tests" + "nickiel.net/recount_server/tests" - "database/sql" - "net/http" + "database/sql" + "net/http" - "fmt" - "flag" - "log" - "time" + "flag" + "fmt" + "os" + "time" - //"github.com/shopspring/decimal" - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" + //"github.com/shopspring/decimal" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) var DB_TYPE = "postgres" @@ -44,21 +46,23 @@ func headers(w http.ResponseWriter, req *http.Request) { } func main() { - log.SetPrefix("RecountServer: ") - log.SetFlags(0) + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + zerolog.SetGlobalLevel(zerolog.InfoLevel) + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) var nFlag = flag.Bool("d", false, "whether to enable debug mode") flag.Parse() if *nFlag { - log.Println("Is debugging") + zerolog.SetGlobalLevel(zerolog.DebugLevel) + log.Debug().Msg("Is debugging") DB_TYPE = "sqlite3" DB_SCHEMA = "" DB_CONNECTION_STRING = "test.db" debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING) } - log.Print("starting server") + log.Info().Msg("starting server") r := chi.NewRouter() @@ -79,7 +83,9 @@ func main() { err := http.ListenAndServe(":8090", r) if err != nil { - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Could not open server connection") } //fmt.Println("Hello World") diff --git a/tests/testdb.go b/tests/testdb.go index 88324dd..64a416a 100644 --- a/tests/testdb.go +++ b/tests/testdb.go @@ -1,18 +1,18 @@ package debug_mode import ( - "log" - _ "github.com/mattn/go-sqlite3" "github.com/jmoiron/sqlx" + "github.com/rs/zerolog/log" ) func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { - log.Print("couldn't open test db") - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Couldn't open test db") } defer db.Close() @@ -78,14 +78,16 @@ ALTER TABLE transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkd tx := db.MustBegin() tx.MustExec(init_sql) - tx.Commit() + err = tx.Commit() if err != nil { - log.Fatal(err) + log.Fatal(). + Err(err). + Msg("Could not commit transaction") } - log.Print("Test database initialized") + log.Debug().Msg("Test database initialized") }