This commit is contained in:
lbenedar
2026-03-19 13:44:50 +03:00
parent 5cc1196085
commit b8fff8f7d5
4 changed files with 112 additions and 8 deletions

View File

@@ -6,7 +6,10 @@ import (
)
func (app *application) logError(r *http.Request, err error) {
app.logger.Print(err)
app.logger.PrintError(err, map[string]string{
"request_method": r.Method,
"request_url": r.URL.String(),
})
}
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) {

View File

@@ -14,7 +14,6 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques
}
err := app.writeJSON(w, http.StatusOK, env, nil)
if err != nil {
app.logger.Print(err)
app.serverErrorResponse(w, r, err)
}
}

View File

@@ -11,6 +11,7 @@ import (
"time"
"gitea.local.lab/Lbenedar/greenlight/internal/data"
"gitea.local.lab/Lbenedar/greenlight/internal/jsonlog"
_ "github.com/lib/pq"
)
@@ -18,7 +19,7 @@ const version = "1.0.0"
type application struct {
config config
logger *log.Logger
logger *jsonlog.Logger
models data.Models
}
@@ -70,15 +71,15 @@ func main() {
flag.StringVar(&cfg.db.maxIdleTime, "db-max-idle-time", "15m", "PostgreSQL max connection idle time")
flag.Parse()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
logger := jsonlog.New(os.Stdout, jsonlog.LevelInfo)
db, err := openDB(cfg)
if err != nil {
logger.Fatal(err)
logger.PrintFatal(err, nil)
}
defer db.Close()
logger.Printf("database connection pool established")
logger.PrintInfo("database connection pool established", nil)
app := &application{
config: cfg,
@@ -89,11 +90,15 @@ func main() {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
ErrorLog: log.New(logger, "", 0),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
logger.Printf("starting %s server on %s", cfg.env, srv.Addr)
logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": cfg.env,
})
err = srv.ListenAndServe()
logger.Fatal(err)
logger.PrintFatal(err, nil)
}