Skip to content

Refactor : Structured logging in util.go#193

Open
Joshna907 wants to merge 1 commit intofossology:mainfrom
Joshna907:utils-new
Open

Refactor : Structured logging in util.go#193
Joshna907 wants to merge 1 commit intofossology:mainfrom
Joshna907:utils-new

Conversation

@Joshna907
Copy link
Copy Markdown
Contributor

Implemented structured logging in pkg/utils/util.go using zap. A previous pull request was closed during development due to conflicts and mixed changes. This PR is created fresh to keep the scope clean.

@Joshna907
Copy link
Copy Markdown
Contributor Author

@Kaushl2208 You could check this pull request, its done now

Comment on lines +569 to +580
if status == IMPORT_FAILED {
shortname := ""
if result.Shortname != nil {
shortname = *result.Shortname
}

logger.LogError(
"License import failed",
zap.String("shortname", shortname),
zap.String("message", msg),
)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in duplicate logs. Since the error is already logged inside InsertOrUpdateLicenseOnImport, please remove the extra LogError here.

Comment on lines +551 to +558
shortname := ""
if result.Shortname != nil {
shortname = *result.Shortname
}

logger.LogError(
"License validation failed",
zap.String("shortname", shortname),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of this , use directly *result.Shortname at logs !

Comment on lines 515 to 541
var licenses []models.LicenseJson
// Read the content of the data file.

// Read JSON file
byteResult, err := os.ReadFile(datafile)
if err != nil {
log.Fatalf("Unable to read JSON file: %v", err)
logger.LogError("Unable to read JSON file", zap.Error(err))
return
}
// Unmarshal the JSON file data into a slice of LicenseJson structs.

// Parse JSON
if err := json.Unmarshal(byteResult, &licenses); err != nil {
log.Fatalf("error reading from json file: %v", err)
logger.LogError("Error unmarshalling JSON file", zap.Error(err))
return
}

// Fetch SUPER_ADMIN user
user := models.User{}
level := "SUPER_ADMIN"

if err := db.DB.Where(&models.User{UserLevel: &level}).First(&user).Error; err != nil {
log.Fatalf("Failed to find a super admin")
logger.LogError(
"Failed to find a super admin",
zap.String("level", level),
zap.Error(err),
)
return
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace LogError + return with LogFatal. This code runs during startup, and if it fails the backend should not continue running in a partially initialized state.


query := fmt.Sprintf("SET pg_trgm.similarity_threshold = %f", threshold)
if err := db.DB.Exec(query).Error; err != nil {
log.Println("Failed to set similarity threshold:", err)
Copy link
Copy Markdown
Contributor

@ChayanDass ChayanDass Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log the query execution error, with the previous log message

Comment on lines 512 to 513
// Populatedb populates the database with license data from a JSON file.
// Populatedb populates the database with license data from a JSON file.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review and test your changes yourself first, and avoid updating comments unnecessarily.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the messy history! I ran into some bad merge conflicts with main earlier. I've cleaned it all up now, so this PR is just the logging changes in util.go

Please review and test your changes yourself first, and avoid updating comments unnecessarily.

@Joshna907 Joshna907 changed the title Refactor : Stuctured logging in util.go Refactor : Structured logging in util.go Jan 15, 2026
@Joshna907 Joshna907 force-pushed the utils-new branch 6 times, most recently from 4590680 to 7560a59 Compare January 15, 2026 07:38
log.Println("Failed to set similarity threshold:", err)
logger.LogError(
"Failed to set similarity threshold",
zap.String("query", query),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to log the query !

logger.LogError(
"License validation failed",
zap.String("shortname", *result.Shortname),
zap.String("error", fmt.Sprintf("field '%s' failed validation: %s", err.(validator.ValidationErrors)[0].Field(), err.(validator.ValidationErrors)[0].Tag())),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly log the field and tag with zap.String , and log the err with zap.Error

@ChayanDass
Copy link
Copy Markdown
Contributor

The rest looks good!

@Joshna907
Copy link
Copy Markdown
Contributor Author

The rest looks good!

Thank you ! It's done now and thanks for suggestions

@ChayanDass
Copy link
Copy Markdown
Contributor

Looks good!

Copy link
Copy Markdown
Member

@Kaushl2208 Kaushl2208 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes looks good. But @Joshna907 as much as I like merging and Reviewing PRs. I hope this ZAP Log update ends with 1 PR. It will be nightmare for me to RollBack if something breaks. So if you are expecting any other file with similar changes. I request you to update all that into this PR itself. Be it any file but make all the changes here in this PR. And if we have already updated this, Let me know. I will merge.

@Joshna907
Copy link
Copy Markdown
Contributor Author

Joshna907 commented Feb 3, 2026

Changes looks good. But @Joshna907 as much as I like merging and Reviewing PRs. I hope this ZAP Log update ends with 1 PR. It will be nightmare for me to RollBack if something breaks. So if you are expecting any other file with similar changes. I request you to update all that into this PR itself. Be it any file but make all the changes here in this PR. And if we have already updated this, Let me know. I will merge.

@Kaushl2208 I've updated this PR to include the changes from #175 and #176 as requested, as they were already approved but only merging was remaining, so now this PR contains all the remaining structured logging refactors in one place. Should I close this other two PR, Thanks for the review!!

@Joshna907
Copy link
Copy Markdown
Contributor Author

Can we merge ?? @Kaushl2208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants