Optimize logger #17308
Open
wxiaoguang
wants to merge 12 commits from
wxiaoguang/optimize-logger
into main
pull from: wxiaoguang/optimize-logger
merge into: lunny:main
lunny:main
lunny:KN4CK3R/fix-17378
lunny:yarg-kane/main
lunny:6543-forks/refactor_teams-to-api-convert
lunny:qwerty287/timeline-api
lunny:99rgosse/migration-history
lunny:qwerty287/wiki-api
lunny:qwerty287/notifications-site
lunny:qwerty287/fork-dialog
lunny:lunny/lunny/api_ui
lunny:wxiaoguang/issue-content-history-fix
lunny:wxiaoguang/frontend-index-init
lunny:release/v1.15
lunny:zeripath/go-json
lunny:andrebruch/feature-add-issue-and-comment-attachments-api
lunny:SahAssar/patch-1
lunny:delvh/refactor-js
lunny:noerw/fix-17179
lunny:zeripath/request-as-process
lunny:KN4CK3R/feature-migrate-gitbucket
lunny:dpschen/patch-1
lunny:wxiaoguang/enforce-2fa
lunny:raygervais/update/rel-nofollow
lunny:kolaente/feature/auto-merge
lunny:lunny/lunny/user_orgs_pagination
lunny:techknowlogick/user-settings
lunny:lunny/lunny/refactor_issues
lunny:zeripath/oidc-claims
lunny:techknowlogick/milestone-labels
lunny:n194/wiki-footer-sidebar-hints
lunny:KN4CK3R/feature-packages
lunny:techknowlogick/snap-fix
lunny:zeripath/attempt-to-fix-17138
lunny:anbraten/sort-project-issuses
lunny:a1012112796/double_clicked_button
lunny:netzbegruenung/feature/ldap-group-sync
lunny:zeripath/backport-17154
lunny:OleHusgaard/main
lunny:KN4CK3R/feature-migrate-codebase
lunny:zeripath/page-directory-entries-on-viewcode
lunny:osmosBe/patch-1
lunny:release/v1.14
lunny:silverwind/hashlink
lunny:fuzzing-stuff/add-custom-date-16841
lunny:derekhu/patch-1
lunny:lunny/lunny/rename_modules_pkgs2
lunny:lunny/lunny/refactor_context
lunny:zeripath/pause-queues
lunny:lunny/lunny/http_refactor
lunny:boppy/pwd-update
lunny:zeripath/fix-15178-provide-aliases-for-submodules
lunny:noerw/docs-stl-update-url
lunny:KN4CK3R/refactor-sanitize
lunny:drachenfels-de/main
lunny:sschroe/main
lunny:zeripath/fix-7508-support-proxy-protocol
lunny:lunny/lunny/cache_ledis
lunny:a1012112796/hooks
lunny:ibpl/master-IB#1107572
lunny:KN4CK3R/fix-12331
lunny:dragonflylee/container
lunny:6543-forks/update-vendor-cli
lunny:zeripath/add-max-concurrent-hashes
lunny:a1012112796/migration_trn
lunny:a1012112796/ui_create_repo_bug
lunny:zeripath/speed-up-testpatch
lunny:a1012112796/issue_lable_api
lunny:colorfulwolf/fix-unuse-truncate
lunny:a1012112796/imap
lunny:release/v1.13
lunny:kolaente/feature/pr-prompt
lunny:a1012112796/custom-repo-buttons
lunny:POPSuL/f/save-whitespace-behavior
lunny:a1012112796/docker_support
lunny:lunny/lunny/services
lunny:andrebruch/fix/release_api
lunny:6543-forks/private-issue_3217
lunny:jolheiser/template-branch-prot
lunny:silverwind/textarea
lunny:rogerluo410/dev-project-board
lunny:CL-Jeremy/svg-vertical-alignment
lunny:CL-Jeremy/mobile-repo-rework
lunny:lunny/lunny/user
lunny:silverwind/jsx
lunny:CL-Jeremy/mobile-tabs
lunny:rogerluo410/dev-find-file
lunny:lunny/lunny/inline_comment
lunny:ibpl/master-IB#1105051
lunny:appleboy/patch2
lunny:lunny/lunny/sqlite
lunny:noerw/fix-14942
lunny:lunny/lunny/fix_http_bug
lunny:AllSpiceIO/kd/fix-docker_rootless_docs
lunny:pini-gh/pini
lunny:daniil-pankratov/create-admin-auths-api
lunny:6543-forks/docs_man_config-pages
lunny:openscript/patch-1
lunny:a1012112796/init_graphql
lunny:jolheiser/commit-hide-email
lunny:jamesorlakin/feature/mfaDocumentationPage
lunny:lunny/lunny/system_hook_api
lunny:Rob61/add-support-for-multiple-ldap-server
lunny:lunny/lunny/reaction_col_size
lunny:lafriks-fork/feat/indexer_availability
lunny:lafriks-fork/feat/secrets
lunny:lunny/lunny/new_file_blank_repo
lunny:lunny/lunny/improve_notification
lunny:mrsdizzie/better-diff
lunny:CirnoT/commit-refs
lunny:zeripath/no-go-git-refactor-all-the-git
lunny:6543-forks/issue-page-filter-author
lunny:6543-forks/ui-migration-cancel
lunny:macvag/master
lunny:6543-forks/release-attachments_reject-duplicate-names
lunny:johanvdw/commit_sha
lunny:release/v1.12
lunny:a1012112796/feature/Pinned_repo
lunny:bhalbright/graphql-poc
lunny:sapk-fork/limit-repo-size
lunny:a1012112796/pr_template_enhancement
lunny:0xC4N1/copyable-line-numbers
lunny:M4RKUS-11111/add_steam
lunny:alvardes/revisions
lunny:ibpl/master-IB#1104925
lunny:ibpl/master-IB#1105071
lunny:adelowo/show_commit_diffs_btw_current_branch_and_master
lunny:zeripath/show-storage-configuration-on-admin-configuration-page
lunny:6543-forks/api-add-reaction-summary
lunny:ibpl/master-IB#1105104
lunny:zeripath/fix-916-go-camo-media-proxy
lunny:lunny/lunny/github_projects
lunny:mvdkleijn/limit-private-repos
lunny:a1012112796/table_name_prefix
lunny:foxy82/patch-1
lunny:TheoLassonder/master
lunny:release/v1.11
lunny:silverwind/rmtheme
lunny:techknowlogick/cdb-support
lunny:zeripath/apply-patch
lunny:guillep2k/user-repo-unit
lunny:zeripath/queues-notification
lunny:release/v1.10
lunny:guillep2k/backport-migration
lunny:jolheiser/loading-button
lunny:mqudsi/attachment_allow_prefix
lunny:lafriks-fork/feat/priority
lunny:Cherrg/gitea_md_wiki_auto_toc
lunny:release/v1.9
lunny:strk/local-source-oauth-users
lunny:gary-kim/security/noid/require-login-for-non-existent-repo
lunny:lunny/lunny/support_hsts
lunny:Cherrg/wiki_directories
lunny:Cherrg/gitea_wiki_page_toc
lunny:release/v1.8
lunny:coolaj86/ux-register-is-signin
lunny:AlexanderBeyn/custom-regexp-external-issues
lunny:ollien/pr-branch-links
lunny:Codeberg-org/global-wiki-edit
lunny:lunny/lunny/private_repo_public_units
lunny:beeonthego/beeonthego-jwt-ui
lunny:lunny/lunny/dump_restore
No reviewers
Labels
Clear labels
Pull requests that update a dependency file
Likely to be an easy fix
features for moderateing spam, abuse, ...
An issue that is caused by an upstream dependency
Performance Issues affecting Big Repositories
Performance issues affecting memory use
performance issues with slow downs
Issue has been reviewed and confirmed to be present or accepted to be implemented
A fix for this issue is already in the attached milestone
The reported issue is the intended behavior
Docker themed issued
Migration issues
backport/done
backport/v1.0
backport/v1.1
backport/v1.10
backport/v1.11
backport/v1.12
backport/v1.13
backport/v1.14
backport/v1.15
backport/v1.2
backport/v1.3
backport/v1.4
backport/v1.5
backport/v1.6
backport/v1.7
backport/v1.8
backport/v1.9
bounty
changelog
dependencies
Pull requests that update a dependency file
frontport/done
frontport/main
good first issue
Likely to be an easy fix
Hacktoberfest
hacktoberfest-accepted
in progress
kind/api
kind/breaking
kind/bug
kind/build
kind/deployment
kind/deprecated
kind/docs
kind/enhancement
kind/feature
kind/lint
kind/misc
kind/moderation
features for moderateing spam, abuse, ...
kind/package
kind/proposal
kind/question
kind/refactor
kind/regression
kind/security
kind/summary
kind/testing
kind/translation
kind/ui
kind/upstream-related
An issue that is caused by an upstream dependency
kind/usability
kind/ux
lgtm/done
lgtm/need 1
lgtm/need 2
performance/bigrepo
Performance Issues affecting Big Repositories
performance/cpu
performance/memory
Performance issues affecting memory use
performance/speed
performance issues with slow downs
priority/critical
priority/low
priority/maybe
priority/medium
proposal/rejected
reviewed/confirmed
Issue has been reviewed and confirmed to be present or accepted to be implemented
reviewed/duplicate
reviewed/fixed
A fix for this issue is already in the attached milestone
reviewed/invalid
reviewed/not-a-bug
The reported issue is the intended behavior
reviewed/wontfix
skip-changelog
stale
status/blocked
status/needs-feedback
status/wip
theme/2fa
theme/authentication
theme/avatar
theme/backup-restore
theme/docker
Docker themed issued
theme/federation
theme/issues
theme/kanban
theme/markdown
theme/migration
Migration issues
theme/mobile
theme/pr
theme/signing
theme/sqlite
theme/timetracker
theme/webhook
theme/wiki
No Label
backport/done
backport/v1.0
backport/v1.1
backport/v1.10
backport/v1.11
backport/v1.12
backport/v1.13
backport/v1.14
backport/v1.15
backport/v1.2
backport/v1.3
backport/v1.4
backport/v1.5
backport/v1.6
backport/v1.7
backport/v1.8
backport/v1.9
bounty
changelog
dependencies
frontport/done
frontport/main
good first issue
Hacktoberfest
hacktoberfest-accepted
in progress
kind/api
kind/breaking
kind/bug
kind/build
kind/deployment
kind/deprecated
kind/docs
kind/enhancement
kind/feature
kind/lint
kind/misc
kind/moderation
kind/package
kind/proposal
kind/question
kind/refactor
kind/regression
kind/security
kind/summary
kind/testing
kind/translation
kind/ui
kind/upstream-related
kind/usability
kind/ux
lgtm/done
lgtm/need 1
lgtm/need 2
performance/bigrepo
performance/cpu
performance/memory
performance/speed
priority/critical
priority/low
priority/maybe
priority/medium
proposal/rejected
reviewed/confirmed
reviewed/duplicate
reviewed/fixed
reviewed/invalid
reviewed/not-a-bug
reviewed/wontfix
skip-changelog
stale
status/blocked
status/needs-feedback
status/wip
theme/2fa
theme/authentication
theme/avatar
theme/backup-restore
theme/docker
theme/federation
theme/issues
theme/kanban
theme/markdown
theme/migration
theme/mobile
theme/pr
theme/signing
theme/sqlite
theme/timetracker
theme/webhook
theme/wiki
Milestone
Clear milestone
No items
No Milestone
Projects
Clear projects
No project
Assignees
Clear assignees
No Assignees
2 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.
No due date set.
Dependencies
No dependencies set.
Reference: lunny/gitea#17308
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
No description provided.
Delete Branch "wxiaoguang/optimize-logger"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Background:
This PR
Fixes the problems above. It makes the log system more friendly to developers.
A setting option
ROUTER_LOG_HANDLER
is introduced. It's default tov2
(the new). If users need to usev1
for fail2ban and others, the option can be set tov1
to use the old handler.ps: In this PR, the long polling handlers also can be recognized, see the
still-executing
log in the screenshot, so no worry about can not see logs for slow queries.About performance:
In v1, every request outputs 2 logs (Started/Completed)
In v2 (this), every request only outputs one log
So generally speaking, the
logger context
doesn't cost much, using v2 withlogger context
will not affect performanceInstead, the performance may be improved because now only 1 log is outputted for each request.
Screenshots
Before
After
This is wrong.
Why do you want the stack trace to start at the log.Stack level?
It is correct. If you do not print the whole stack, it's very difficult for developers to find where the panic was recovered. See the background (4).
I spent much time debugging this annoying issue.
The first stack frame is skipped already, I think ....
but the log line itself tells you where it was logged from?
Let's look at an example from https://github.com/go-gitea/gitea/issues/17030
The panic was clearly caught in
.../graceful/manager.go:135:func1()
If you don't skip the first two frames it looks like it arose from there.
OK, I can revert the behavior. But in my mind depending the
.../graceful/manager.go:135:func1()
prefix of the log is not a good idea. And some stack traces are directly outputted to HTML response (in development mode), there is no way to see the recovery point if the prefix is missing.Reverted.
I don't completely understand what you're trying to do here but I really dislike the "panic occurred " changes.
Hmm I don't like this change of log line as it's pretty clear that it doesn't get recovered because it's logged at Fatal level. I don't understand what this "panic occurred, no recover," adds except make it more difficult to see the actual error that caused the panic.
@ -126,6 +126,7 @@ func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string
func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
level := getLogLevel(sec, "LEVEL", LogLevel)
levelName = level.String()
This is what fixes the log level reporting regression.
Seems somewhat of a shame to recouple common with public.
How about:
I deliberately chose to leave the anonymous function reporting in the logger because I thought it was useful to indicate that this was coming from within an anonymous function instead of giving the false impression of being within the main function.
Why isn't this function in modules/log?
@ -49,0 +53,4 @@
}
if routerLogHandler == nil {
log.Warn("unknown router log handler '%s', fall back to v2")
routerLogHandler = NewLoggerHandlerV2(setting.RouterLogLevel)
Same as above, IMHO "panic occurred and gets recovered" adds nothing and makes it harder to see the panic.
What does recovered here mean?
It's a panic just log it as a PANIC. This "panic occurred and gets recovered" adds nothing but make it harder to see the PANIC.
Sometimes the code may just print the error and panic again to pass the error to caller. So I think this message makes it clear that the panic recovery stops here.
Why it should be? It is really simple and no other modules need it. And it is specialized for the new logger system (eg: fileNameShort, funcNameShort).
@ -49,0 +53,4 @@
}
if routerLogHandler == nil {
log.Warn("unknown router log handler '%s', fall back to v2")
routerLogHandler = NewLoggerHandlerV2(setting.RouterLogLevel)
As above.
As above.
As above.
We have line numbers in logs, it is easier to indicate where the log comes from. And when you see a anonymous func name like
func1
,func2
, you still do not know what it is, it just makes the log longer and hard to read important information.Trust me, I just tried and finally choose to remove the suffix. It makes the log more clear. ?
But the couple was already there (in
routers/install/routes.go
), I just move the code to a new place.OK, I get your point, will improve it later. Thanks ~
This should be:
or if you're really sure you don't need to wait for this goroutine to finish before moving on to terminate:
But it's probably better to just have it tracked.
Graceful tracks functions that are run and will ensure that they're finished before moving on to terminate. Avoid allowing goroutines to escape outside of that control.
I don't think the ticker is the right thing here as this function could take more than a second to process
@ -0,0 +217,4 @@
if localPanicErr != nil {
// we do not recover any panic, so let the framework handle the panic error
panic(localPanicErr)
hmm... won't this make the panic stack start here when it's eventually caught?
I am sure I don't want to wait for the goroutine. It just works like a background thread to check slow queries.
Update to
go func(basectx context.Context) { ... } (graceful.getManager().ShutdownContext())
, but I can not quite understand what's the difference here. They seem the same in my mind.I think the ticker is the right thing. We do not care about accurate duration here. It just does the check periodly, 500ms or 1500ms are all OK. And the code runs really fast here, no problem. I did my best to make sure everything work as fast as possible.
Using NewTicker can save a memory allocation, because time.After always allocates a new Timer struct.
@ -0,0 +217,4 @@
if localPanicErr != nil {
// we do not recover any panic, so let the framework handle the panic error
panic(localPanicErr)
I think no problem here. the stack is full:
@ -0,0 +217,4 @@
if localPanicErr != nil {
// we do not recover any panic, so let the framework handle the panic error
panic(localPanicErr)
And in real usage, this panic recover in logger context won't be triggered. We have other recovers. For example, if I do panic in
web.Home
, the output is:Everything is fine here. The recover in logger context is just a fallback mechanism, make sure we can see a
failed
log, it doesn't affect anything.