Skip to content

Commit

Permalink
Merge pull request #276 from cho4036/dev_improve_logger
Browse files Browse the repository at this point in the history
�Logging 개선
  • Loading branch information
ktkfree authored Mar 18, 2024
2 parents 9ecb641 + 481e019 commit 14d7fd9
Show file tree
Hide file tree
Showing 80 changed files with 2,536 additions and 3,352 deletions.
28 changes: 15 additions & 13 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"net/http"
Expand Down Expand Up @@ -72,7 +73,7 @@ func init() {
flag.Parse()

if err := viper.BindPFlags(pflag.CommandLine); err != nil {
log.Error(err)
log.Error(context.Background(), err)
}

address := viper.GetString("external-address")
Expand Down Expand Up @@ -102,19 +103,20 @@ func init() {
// @host tks-api-dev.taco-cat.xyz
// @BasePath /api/1.0/
func main() {
log.Info("*** Arguments *** ")
ctx := context.Background()
log.Info(ctx, "*** Arguments *** ")
for i, s := range viper.AllSettings() {
log.Info(fmt.Sprintf("%s : %v", i, s))
log.Info(ctx, fmt.Sprintf("%s : %v", i, s))
}
log.Info("****************** ")
log.Info(ctx, "****************** ")

// For web service
asset := route.NewAssetHandler(viper.GetString("web-root"))

// Initialize database
db, err := database.InitDB()
if err != nil {
log.Fatal("cannot connect gormDB")
log.Fatal(ctx, "cannot connect gormDB")
}

// Ensure default rows in database
Expand All @@ -125,12 +127,12 @@ func main() {
if viper.GetString("argo-address") == "" || viper.GetInt("argo-port") == 0 {
argoClient, err = argowf.NewMock()
if err != nil {
log.Fatal("failed to create argowf client : ", err)
log.Fatal(ctx, "failed to create argowf client : ", err)
}
} else {
argoClient, err = argowf.New(viper.GetString("argo-address"), viper.GetInt("argo-port"), false, "")
if err != nil {
log.Fatal("failed to create argowf client : ", err)
log.Fatal(ctx, "failed to create argowf client : ", err)
}
}

Expand All @@ -141,20 +143,20 @@ func main() {
ClientSecret: viper.GetString("keycloak-client-secret"),
})

err = keycloak.InitializeKeycloak()
err = keycloak.InitializeKeycloak(ctx)
if err != nil {
log.Fatal("failed to initialize keycloak : ", err)
log.Fatal(ctx, "failed to initialize keycloak : ", err)
}
err = mail.Initialize()
err = mail.Initialize(ctx)
if err != nil {
log.Fatal("failed to initialize ses : ", err)
log.Fatal(ctx, "failed to initialize ses : ", err)
}

route := route.SetupRouter(db, argoClient, keycloak, asset)

log.Info("Starting server on ", viper.GetInt("port"))
log.Info(ctx, "Starting server on ", viper.GetInt("port"))
err = http.ListenAndServe("0.0.0.0:"+strconv.Itoa(viper.GetInt("port")), route)
if err != nil {
log.Fatal(err)
log.Fatal(ctx, err)
}
}
14 changes: 11 additions & 3 deletions internal/database/database.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package database

import (
"context"
"fmt"
"github.com/openinfradev/tks-api/internal/pagination"
"os"
"strings"

"github.com/openinfradev/tks-api/internal/delivery/api"

internal_gorm "github.com/openinfradev/tks-api/internal/gorm"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -37,9 +40,10 @@ func InitDB() (*gorm.DB, error) {
default:
level = logger.Silent
}
newLogger := internal_gorm.NewGormLogger().LogMode(level)

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(level),
Logger: newLogger,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -174,7 +178,11 @@ func EnsureDefaultRows(db *gorm.DB) error {
}

//
eps, err := repoFactory.Endpoint.List(nil)

ctx := context.Background()
pg := pagination.NewPagination(nil)
pg.Limit = 1000
eps, err := repoFactory.Endpoint.List(ctx, pg)
if err != nil {
return err
}
Expand All @@ -185,7 +193,7 @@ func EnsureDefaultRows(db *gorm.DB) error {
}
for _, ep := range api.ApiMap {
if _, ok := storedEps[ep.Name]; !ok {
if err := repoFactory.Endpoint.Create(&model.Endpoint{
if err := repoFactory.Endpoint.Create(ctx, &model.Endpoint{
Name: ep.Name,
Group: ep.Group,
}); err != nil {
Expand Down
32 changes: 16 additions & 16 deletions internal/delivery/http/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func (h *AlertHandler) CreateAlert(w http.ResponseWriter, r *http.Request) {
// webhook 으로 부터 받은 body parse
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.ErrorWithContext(r.Context(),err)
log.Error(r.Context(),err)
}
bodyString := string(bodyBytes)
log.InfoWithContext(r.Context(),bodyString)
log.Info(r.Context(),bodyString)
*/

// 외부로부터(alert manager) 오는 데이터이므로, dto 변환없이 by-pass 처리한다.
Expand Down Expand Up @@ -126,14 +126,14 @@ func (h *AlertHandler) GetAlerts(w http.ResponseWriter, r *http.Request) {
var out domain.GetAlertsResponse
out.Alerts = make([]domain.AlertResponse, len(alerts))
for i, alert := range alerts {
if err := serializer.Map(alert, &out.Alerts[i]); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), alert, &out.Alerts[i]); err != nil {
log.Info(r.Context(), err)
}

outAlertActions := make([]domain.AlertActionResponse, len(alert.AlertActions))
for j, alertAction := range alert.AlertActions {
if err := serializer.Map(alertAction, &outAlertActions[j]); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), alertAction, &outAlertActions[j]); err != nil {
log.Info(r.Context(), err)
}
}
out.Alerts[i].AlertActions = outAlertActions
Expand All @@ -142,8 +142,8 @@ func (h *AlertHandler) GetAlerts(w http.ResponseWriter, r *http.Request) {
}
}

if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
if out.Pagination, err = pg.Response(r.Context()); err != nil {
log.Info(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
Expand Down Expand Up @@ -182,13 +182,13 @@ func (h *AlertHandler) GetAlert(w http.ResponseWriter, r *http.Request) {
}

var out domain.GetAlertResponse
if err := serializer.Map(alert, &out.Alert); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), alert, &out.Alert); err != nil {
log.Info(r.Context(), err)
}
outAlertActions := make([]domain.AlertActionResponse, len(alert.AlertActions))
for j, alertAction := range alert.AlertActions {
if err := serializer.Map(alertAction, &outAlertActions[j]); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), alertAction, &outAlertActions[j]); err != nil {
log.Info(r.Context(), err)
continue
}
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (h *AlertHandler) AlertTest(w http.ResponseWriter, r *http.Request) {
return
}

log.InfoWithContext(r.Context(), "TEST ", body)
log.Info(r.Context(), "TEST ", body)
}

// CreateAlertAction godoc
Expand Down Expand Up @@ -271,11 +271,11 @@ func (h *AlertHandler) CreateAlertAction(w http.ResponseWriter, r *http.Request)
return
}

log.InfoWithContext(r.Context(), "alert : ", helper.ModelToJson(input))
log.Info(r.Context(), "alert : ", helper.ModelToJson(input))

var dto model.AlertAction
if err = serializer.Map(input, &dto); err != nil {
log.InfoWithContext(r.Context(), err)
if err = serializer.Map(r.Context(), input, &dto); err != nil {
log.Info(r.Context(), err)
}
dto.AlertId = alertId

Expand Down
30 changes: 15 additions & 15 deletions internal/delivery/http/app-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (h *AppGroupHandler) CreateAppGroup(w http.ResponseWriter, r *http.Request)
}

var dto model.AppGroup
if err = serializer.Map(input, &dto); err != nil {
log.InfoWithContext(r.Context(), err)
if err = serializer.Map(r.Context(), input, &dto); err != nil {
log.Info(r.Context(), err)
}

appGroupId, err := h.usecase.Create(r.Context(), dto)
Expand Down Expand Up @@ -96,14 +96,14 @@ func (h *AppGroupHandler) GetAppGroups(w http.ResponseWriter, r *http.Request) {
var out domain.GetAppGroupsResponse
out.AppGroups = make([]domain.AppGroupResponse, len(appGroups))
for i, appGroup := range appGroups {
if err := serializer.Map(appGroup, &out.AppGroups[i]); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), appGroup, &out.AppGroups[i]); err != nil {
log.Info(r.Context(), err)
continue
}
}

if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
if out.Pagination, err = pg.Response(r.Context()); err != nil {
log.Info(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
Expand Down Expand Up @@ -139,8 +139,8 @@ func (h *AppGroupHandler) GetAppGroup(w http.ResponseWriter, r *http.Request) {
}

var out domain.GetAppGroupResponse
if err := serializer.Map(appGroup, &out.AppGroup); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), appGroup, &out.AppGroup); err != nil {
log.Info(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
Expand Down Expand Up @@ -173,7 +173,7 @@ func (h *AppGroupHandler) DeleteAppGroup(w http.ResponseWriter, r *http.Request)

err := h.usecase.Delete(r.Context(), appGroupId)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to delete appGroup err : ", err)
log.Error(r.Context(), "Failed to delete appGroup err : ", err)
ErrorJSON(w, r, err)
return
}
Expand Down Expand Up @@ -217,16 +217,16 @@ func (h *AppGroupHandler) GetApplications(w http.ResponseWriter, r *http.Request

applications, err := h.usecase.GetApplications(r.Context(), appGroupId, applicationType)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to get applications err : ", err)
log.Error(r.Context(), "Failed to get applications err : ", err)
ErrorJSON(w, r, err)
return
}

var out domain.GetApplicationsResponse
out.Applications = make([]domain.ApplicationResponse, len(applications))
for i, application := range applications {
if err := serializer.Map(application, &out.Applications[i]); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), application, &out.Applications[i]); err != nil {
log.Info(r.Context(), err)
continue
}
}
Expand Down Expand Up @@ -266,14 +266,14 @@ func (h *AppGroupHandler) CreateApplication(w http.ResponseWriter, r *http.Reque
}

var dto model.Application
if err := serializer.Map(input, &dto); err != nil {
log.InfoWithContext(r.Context(), err)
if err := serializer.Map(r.Context(), input, &dto); err != nil {
log.Info(r.Context(), err)
}
dto.AppGroupId = appGroupId

err = h.usecase.UpdateApplication(r.Context(), dto)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to update application err : ", err)
log.Error(r.Context(), "Failed to update application err : ", err)
ErrorJSON(w, r, err)
return
}
Expand Down
Loading

0 comments on commit 14d7fd9

Please sign in to comment.