Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 01:44:24 +01:00

Clean up log messages (#30313)

`log.Xxx("%v")` is not ideal, this PR adds necessary context messages.
Remove some unnecessary logs.

Co-authored-by: Giteabot <teabot@gitea.io>
(cherry picked from commit 83f83019ef3471b847a300f0821499b3896ec987)

Conflicts:
	- modules/util/util.go
          Conflict resolved by picking `util.Iif` from 654cfd1dfbd3f3f1d94addee50b6fe2b018a49c3
This commit is contained in:
wxiaoguang 2024-04-07 19:17:06 +08:00 committed by Gergely Nagy
parent ceba1abe06
commit 9b8de15797
No known key found for this signature in database
16 changed files with 38 additions and 44 deletions

View file

@ -114,7 +114,7 @@ func showWebStartupMessage(msg string) {
log.Info("* WorkPath: %s", setting.AppWorkPath) log.Info("* WorkPath: %s", setting.AppWorkPath)
log.Info("* CustomPath: %s", setting.CustomPath) log.Info("* CustomPath: %s", setting.CustomPath)
log.Info("* ConfigFile: %s", setting.CustomConf) log.Info("* ConfigFile: %s", setting.CustomConf)
log.Info("%s", msg) log.Info("%s", msg) // show startup message
} }
func serveInstall(ctx *cli.Context) error { func serveInstall(ctx *cli.Context) error {

View file

@ -76,23 +76,14 @@ func calcFingerprintNative(publicKeyContent string) (string, error) {
// CalcFingerprint calculate public key's fingerprint // CalcFingerprint calculate public key's fingerprint
func CalcFingerprint(publicKeyContent string) (string, error) { func CalcFingerprint(publicKeyContent string) (string, error) {
// Call the method based on configuration // Call the method based on configuration
var ( useNative := setting.SSH.KeygenPath == ""
fnName, fp string calcFn := util.Iif(useNative, calcFingerprintNative, calcFingerprintSSHKeygen)
err error fp, err := calcFn(publicKeyContent)
)
if len(setting.SSH.KeygenPath) == 0 {
fnName = "calcFingerprintNative"
fp, err = calcFingerprintNative(publicKeyContent)
} else {
fnName = "calcFingerprintSSHKeygen"
fp, err = calcFingerprintSSHKeygen(publicKeyContent)
}
if err != nil { if err != nil {
if IsErrKeyUnableVerify(err) { if IsErrKeyUnableVerify(err) {
log.Info("%s", publicKeyContent)
return "", err return "", err
} }
return "", fmt.Errorf("%s: %w", fnName, err) return "", fmt.Errorf("CalcFingerprint(%s): %w", util.Iif(useNative, "native", "ssh-keygen"), err)
} }
return fp, nil return fp, nil
} }

View file

@ -53,7 +53,7 @@ func (repo *Repository) IsDependenciesEnabled(ctx context.Context) bool {
var u *RepoUnit var u *RepoUnit
var err error var err error
if u, err = repo.GetUnit(ctx, unit.TypeIssues); err != nil { if u, err = repo.GetUnit(ctx, unit.TypeIssues); err != nil {
log.Trace("%s", err) log.Trace("IsDependenciesEnabled: %v", err)
return setting.Service.DefaultEnableDependencies return setting.Service.DefaultEnableDependencies
} }
return u.IssuesConfig().EnableDependencies return u.IssuesConfig().EnableDependencies

View file

@ -213,6 +213,14 @@ func ToPointer[T any](val T) *T {
return &val return &val
} }
// Iif is an "inline-if", it returns "trueVal" if "condition" is true, otherwise "falseVal"
func Iif[T any](condition bool, trueVal, falseVal T) T {
if condition {
return trueVal
}
return falseVal
}
func ReserveLineBreakForTextarea(input string) string { func ReserveLineBreakForTextarea(input string) string {
// Since the content is from a form which is a textarea, the line endings are \r\n. // Since the content is from a form which is a textarea, the line endings are \r\n.
// It's a standard behavior of HTML. // It's a standard behavior of HTML.

View file

@ -27,7 +27,7 @@ func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
defer rd.Close() defer rd.Close()
if err := json.NewDecoder(rd).Decode(&genRequest); err != nil { if err := json.NewDecoder(rd).Decode(&genRequest); err != nil {
log.Error("%v", err) log.Error("JSON Decode failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(), Err: err.Error(),
}) })
@ -36,7 +36,7 @@ func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
owner, repo, err := parseScope(ctx, genRequest.Scope) owner, repo, err := parseScope(ctx, genRequest.Scope)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("parseScope failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(), Err: err.Error(),
}) })
@ -46,18 +46,18 @@ func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) { if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
token, err = actions_model.NewRunnerToken(ctx, owner, repo) token, err = actions_model.NewRunnerToken(ctx, owner, repo)
if err != nil { if err != nil {
err := fmt.Sprintf("error while creating runner token: %v", err) errMsg := fmt.Sprintf("error while creating runner token: %v", err)
log.Error("%v", err) log.Error("NewRunnerToken failed: %v", errMsg)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err, Err: errMsg,
}) })
return return
} }
} else if err != nil { } else if err != nil {
err := fmt.Sprintf("could not get unactivated runner token: %v", err) errMsg := fmt.Sprintf("could not get unactivated runner token: %v", err)
log.Error("%v", err) log.Error("GetLatestRunnerToken failed: %v", errMsg)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err, Err: errMsg,
}) })
return return
} }

View file

@ -47,7 +47,7 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
_ = stdoutWriter.Close() _ = stdoutWriter.Close()
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env) err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("readAndVerifyCommitsFromShaReader failed: %v", err)
cancel() cancel()
} }
_ = stdoutReader.Close() _ = stdoutReader.Close()
@ -66,7 +66,6 @@ func readAndVerifyCommitsFromShaReader(input io.ReadCloser, repo *git.Repository
line := scanner.Text() line := scanner.Text()
err := readAndVerifyCommit(line, repo, env) err := readAndVerifyCommit(line, repo, env)
if err != nil { if err != nil {
log.Error("%v", err)
return err return err
} }
} }

View file

@ -35,7 +35,7 @@ func SendEmail(ctx *context.PrivateContext) {
defer rd.Close() defer rd.Close()
if err := json.NewDecoder(rd).Decode(&mail); err != nil { if err := json.NewDecoder(rd).Decode(&mail); err != nil {
log.Error("%v", err) log.Error("JSON Decode failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(), Err: err.Error(),
}) })

View file

@ -403,7 +403,6 @@ func EditUserPost(ctx *context.Context) {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplUserEdit, &form) ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplUserEdit, &form)
case password.IsErrIsPwnedRequest(err): case password.IsErrIsPwnedRequest(err):
log.Error("%s", err.Error())
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplUserEdit, &form) ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplUserEdit, &form)
default: default:

View file

@ -214,7 +214,6 @@ func ResetPasswdPost(ctx *context.Context) {
case errors.Is(err, password.ErrIsPwned): case errors.Is(err, password.ErrIsPwned):
ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplResetPassword, nil) ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplResetPassword, nil)
case password.IsErrIsPwnedRequest(err): case password.IsErrIsPwnedRequest(err):
log.Error("%s", err.Error())
ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplResetPassword, nil) ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplResetPassword, nil)
default: default:
ctx.ServerError("UpdateAuth", err) ctx.ServerError("UpdateAuth", err)
@ -298,7 +297,6 @@ func MustChangePasswordPost(ctx *context.Context) {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplMustChangePassword, &form) ctx.RenderWithErr(ctx.Tr("auth.password_pwned"), tplMustChangePassword, &form)
case password.IsErrIsPwnedRequest(err): case password.IsErrIsPwnedRequest(err):
log.Error("%s", err.Error())
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplMustChangePassword, &form) ctx.RenderWithErr(ctx.Tr("auth.password_pwned_err"), tplMustChangePassword, &form)
default: default:

View file

@ -74,7 +74,6 @@ func AccountPost(ctx *context.Context) {
case errors.Is(err, password.ErrIsPwned): case errors.Is(err, password.ErrIsPwned):
ctx.Flash.Error(ctx.Tr("auth.password_pwned")) ctx.Flash.Error(ctx.Tr("auth.password_pwned"))
case password.IsErrIsPwnedRequest(err): case password.IsErrIsPwnedRequest(err):
log.Error("%s", err.Error())
ctx.Flash.Error(ctx.Tr("auth.password_pwned_err")) ctx.Flash.Error(ctx.Tr("auth.password_pwned_err"))
default: default:
ctx.ServerError("UpdateAuth", err) ctx.ServerError("UpdateAuth", err)

View file

@ -79,11 +79,11 @@ func VerifyCaptcha(ctx *Context, tpl base.TplName, form any) {
case setting.CfTurnstile: case setting.CfTurnstile:
valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField)) valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField))
default: default:
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType)) ctx.ServerError("Unknown Captcha Type", fmt.Errorf("unknown Captcha Type: %s", setting.Service.CaptchaType))
return return
} }
if err != nil { if err != nil {
log.Debug("%v", err) log.Debug("Captcha Verify failed: %v", err)
} }
if !valid { if !valid {

View file

@ -91,7 +91,7 @@ func AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues
// NewPullRequest notifies new pull request to notifiers // NewPullRequest notifies new pull request to notifiers
func NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) { func NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) {
if err := pr.LoadIssue(ctx); err != nil { if err := pr.LoadIssue(ctx); err != nil {
log.Error("%v", err) log.Error("LoadIssue failed: %v", err)
return return
} }
if err := pr.Issue.LoadPoster(ctx); err != nil { if err := pr.Issue.LoadPoster(ctx); err != nil {
@ -112,7 +112,7 @@ func PullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *iss
// PullRequestReview notifies new pull request review // PullRequestReview notifies new pull request review
func PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) { func PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
if err := review.LoadReviewer(ctx); err != nil { if err := review.LoadReviewer(ctx); err != nil {
log.Error("%v", err) log.Error("LoadReviewer failed: %v", err)
return return
} }
for _, notifier := range notifiers { for _, notifier := range notifiers {

View file

@ -28,7 +28,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
t, err := NewTemporaryUploadRepository(ctx, repo) t, err := NewTemporaryUploadRepository(ctx, repo)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("NewTemporaryUploadRepository failed: %v", err)
} }
defer t.Close() defer t.Close()
if err := t.Clone(opts.OldBranch, false); err != nil { if err := t.Clone(opts.OldBranch, false); err != nil {

View file

@ -111,7 +111,7 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user
t, err := NewTemporaryUploadRepository(ctx, repo) t, err := NewTemporaryUploadRepository(ctx, repo)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("NewTemporaryUploadRepository failed: %v", err)
} }
defer t.Close() defer t.Close()
if err := t.Clone(opts.OldBranch, true); err != nil { if err := t.Clone(opts.OldBranch, true); err != nil {

View file

@ -143,7 +143,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
t, err := NewTemporaryUploadRepository(ctx, repo) t, err := NewTemporaryUploadRepository(ctx, repo)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("NewTemporaryUploadRepository failed: %v", err)
} }
defer t.Close() defer t.Close()
hasOldBranch := true hasOldBranch := true

View file

@ -209,7 +209,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if isOldWikiExist { if isOldWikiExist {
err := gitRepo.RemoveFilesFromIndex(oldWikiPath) err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("RemoveFilesFromIndex failed: %v", err)
return err return err
} }
} }
@ -219,18 +219,18 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
objectHash, err := gitRepo.HashObject(strings.NewReader(content)) objectHash, err := gitRepo.HashObject(strings.NewReader(content))
if err != nil { if err != nil {
log.Error("%v", err) log.Error("HashObject failed: %v", err)
return err return err
} }
if err := gitRepo.AddObjectToIndex("100644", objectHash, newWikiPath); err != nil { if err := gitRepo.AddObjectToIndex("100644", objectHash, newWikiPath); err != nil {
log.Error("%v", err) log.Error("AddObjectToIndex failed: %v", err)
return err return err
} }
tree, err := gitRepo.WriteTree() tree, err := gitRepo.WriteTree()
if err != nil { if err != nil {
log.Error("%v", err) log.Error("WriteTree failed: %v", err)
return err return err
} }
@ -255,7 +255,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts) commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts)
if err != nil { if err != nil {
log.Error("%v", err) log.Error("CommitTree failed: %v", err)
return err return err
} }
@ -270,11 +270,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
0, 0,
), ),
}); err != nil { }); err != nil {
log.Error("%v", err) log.Error("Push failed: %v", err)
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err return err
} }
return fmt.Errorf("Push: %w", err) return fmt.Errorf("failed to push: %w", err)
} }
return nil return nil