Repository navigation
log: replace clog with charmlog and logx - #8529
Merged
Merged
Conversation
unknwon
force-pushed
the
logging/charmlog-logx
branch
3 times, most recently
from
November 14, 2026 05:34
ef6c5ef to
71fd6a6
Compare
unknwon
force-pushed
the
logging/charmlog-logx
branch
4 times, most recently
from
November 15, 2026 03:06
4e8f44f to
67c648d
Compare
This was referenced Nov 15, 2026
Closed
unknwon
force-pushed
the
logging/charmlog-logx
branch
from
November 16, 2026 14:59
67c648d to
a2301ce
Compare
Logging now uses charm.land/log/v2 as the slog handler and unknwon.dev/x/logx as the logger, matching pgrok and git-lfs-server. Console, file, Slack, and Discord stay configurable, and database logs go through the same logger instead of xorm.log and gorm.log.
Every entry point builds its logger and passes it down. Functions take `context.Context` and `*logx.Logger` first, route handlers use `c.Logger`, and macaron and flamego get the logger mapped for injection. The `internal/applog` global is removed. Methods whose signatures are fixed by xorm hooks or templates read the logger stored on the database handle, falling back to a no-op logger when the handle is nil. Also documents the logger argument convention in AGENTS.md, and labels trace lines `TRAC` in text output.
Drop the internal/logx wrapper. Loggers are *logx.Logger from
unknwon.dev/x/logx, as in git-lfs-server and pgrok, and every call is a
structured slog call such as `logger.Error("Failed to get user", "userID", id, "error", err)`.
Handler setup lives in internal/conf/logging.go with the rotating file
writer and the Slack and Discord handlers. Trace maps to the debug level.
LFS and Git HTTP loggers are scoped at the call site.
GORM and xorm now log through real logger implementations instead of writer adapters, so each query is one line with sql, args, rows, elapsed, and source attributes, and failed GORM queries are errors. Long xorm arguments are truncated. Internal server errors log the request method and path. Production timestamps are RFC 3339 with the time zone.
- Wrap AvatarLink in the template FuncMap, templates pass only the email. - Reopen the log file after a failed rotation so logging resumes. - Post Slack and Discord messages from a queue of BUFFER_LEN. - Map LEVEL = Fatal to the error level that FatalContext logs at. - Keep the console logger when the hook log file cannot be created.
- Drop log lines the file writer cannot write instead of buffering them. - Recreate a missing log directory when reopening the log file. - Treat a negative BUFFER_LEN as 0. - Refuse to start when a Slack or Discord logger has no URL. - Ignore duplicate entries in [log] MODE.
- Post queued Slack and Discord messages before FatalContext exits. - Log errors that stop gogs web through the configured logger once it exists.
- Hide query values in failed and slow GORM query logs unless every query is logged, so they do not reach Slack or Discord. - Write console logs to stdout like clog, except in serv and hooks. - Label FatalContext records as fatal, so LEVEL = Fatal sinks get only them. - Bound the fatal webhook flush even when the queue is full. - Reset rotation counters once the log file is renamed. - Show each sink's level in the startup log, name the failed admin command, and honor TTY_FORCE for console colors.
- Log every query only in the web server and serv. admin, backup and restore log failed and slow queries, which main wrote to xorm.log and gorm.log. - Keep query values on the debug Query line, off the error and warning that Slack or Discord may receive. - Log the enabled log modes through the configured logger, so production output stays JSON.
Covers the new admin, organization settings and user settings handlers, and the participant mail and email activation changes merged from main.
Covers the new issue, label, milestone, release, branch, wiki, editor, commit, repository settings, webhook, Git hook, fork, new repository, passkey and authentication source handlers, plus the remaining calls merged into existing files.
unknwon
force-pushed
the
logging/charmlog-logx
branch
from
November 16, 2026 17:41
a2301ce to
606e744
Compare
Bumps unknwon.dev/x so Scoped sets a dotted "component" attribute instead of prefixing the message. Text sinks and webhooks still show it as a "[gorm] " prefix, while JSON keeps it as a field that log explorers can filter on.
Trace stays accepted as the same level as Debug.
FORMAT is "text" or "json", and empty keeps JSON for production only. The default config reads it from GOGS_LOG_FORMAT, which both Docker images set to "json".
The run mode no longer switches the output to JSON. FORMAT defaults to text, and the Docker images set it to JSON.
Empty is JSON in production and text otherwise, and "text" or "json" overrides it, e.g., to keep text logs in production.
unknwon
added a commit
that referenced
this pull request
Nov 16, 2026
This was referenced Nov 16, 2026
mvanhorn
added a commit
to mvanhorn/gogs
that referenced
this pull request
Nov 17, 2026
Resolve the conflict in cmd/gogs/restore.go: keep the custom/data hold, moveTree and rollback steps from this branch, and switch their fatal logging to the logger.FatalContext style that main adopted in gogs#8529.
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the pull request
Replaces
unknwon.dev/clog/v2withcharm.land/log/v2as the slog handler andunknwon.dev/x/logxas the logger, the same pairing pgrok and git-lfs-server use. There is no wrapper package: loggers are*logx.Logger, and every call is structured, e.g.,logger.Error("Failed to get user", "userID", id, "error", err). The handler setup lives ininternal/conf/logging.go.Configuration
[log] MODEstill picks console, file, Slack, and Discord, each with its own level. A mode that does not setLEVELnow inherits[log] LEVEL, which the config comment already claimed and the old code did not do.[log] LEVELis nowDebug.Traceis still accepted as the same level.[log] FORMATchooses the console and file output. Empty (the default) follows the run mode: JSON whenRUN_MODE = prod, text otherwise.textorjsonoverrides it, e.g., to keep text logs in production. The default config reads it fromGOGS_LOG_FORMAT, which both Docker images set tojson, and a user's own[log] FORMATstill wins.gogs.logis unchanged. SSH hook commands still write tolog/hooks/and never to the client.Output
xormandgorm, one structured line per query:sql,rows,elapsed, andsourcefor GORM,sqlandargsfor xorm. Long xorm arguments, such as webhook payloads, are truncated. Failed GORM queries are errors and slow ones are warnings, where a failed query used to be logged at debug level.xorm.log,gorm.log, and the[log.xorm]and[log.gorm]sections are gone.componentfield, and nested scopes join with dots, e.g.,db.gorm, so log explorers can filter by component and by prefix. Text output (console, file, hooks, Slack, and Discord) shows it as a[gorm]message prefix. This needs unknwon/x#2, which is merged.Text (default outside production):
JSON (default in production and in the Docker images):
{"time":"2026-11-14T05:50:35Z","level":"info","msg":"Server is available","externalURL":"http://localhost:3999/"} {"time":"2026-11-16T19:01:47Z","level":"error","msg":"Query failed","component":"gorm","sql":"SELECT * FROM `access_token` WHERE uid = ? AND name = ? ORDER BY `access_token`.`id` LIMIT 1","rows":0,"elapsed":"41.837µs","source":"/gogs/internal/database/access_tokens.go:76","error":"SQL logic error: no such table: access_token (1)"} {"time":"2026-11-14T05:50:46Z","level":"error","msg":"Internal server error","method":"POST","path":"/api/v1/user/repos","operation":"create repository","error":"initRepository: prepareRepoCommit: getRepoInitFile[]: unknown readme template \"\""}Code
There is no global logger. Each command builds its logger and passes
*logx.Loggerdown, aftercontext.Contextwhen there is one. Macaron and flamego have the logger mapped for injection, and macaron handlers also usec.Logger. The exception is methods whose signatures are fixed by xorm hooks or templates, which read the logger stored on the database handle. Loggers are scoped at the call site. AGENTS.md documents the argument order and scoping rules.Differences from main
Some clog assumptions no longer hold, so these differ on purpose:
FatalContextlogs at the error level, and a handler raises those records to fatal, so sinks still showFATAandLEVEL = Fatalstill receives only fatal records.BUFFER_LEN, like clog. clog drained the queue without limit before a fatal exit, and this waits at most 10 seconds. Console and file write synchronously, soBUFFER_LENonly applies to the webhooks.xorm.logandgorm.log. The web server andservlog every query in dev mode.admin,backup, andrestoreonly log failed and slow queries, so their console output does not fill with SQL. Query values appear only on the debugQueryline, never on the error or warning for a failed or slow query, so password hashes and tokens never reach a webhook.servand hooks cannot do because stdout carries the Git protocol.servand hooks never write logs to stdout. Everything else on the console stays on stdout. The Windows service script now captures stderr too.[log.xorm]and[log.gorm]produce the usual invalid option warning.componentfield instead of part of the message. Text sinks render it as the old[name]prefix.CodeQL reports clear-text logging in
internal/context/auth.goand weak hashing ininternal/cryptox/sha.go. Both are false positives that exist on main: CodeQL treats slog calls as logging calls but did not model clog, and the hashed "password" is an access token used as a basic auth password. This PR changes those lines only to pass the logger.Link to the issue: n/a
Checklist
Test plan
go test ./internal/conf/covers level parsing (includingTrace),[log] FORMATwith and without the production run mode, per-handler levels (a debug line stays out of an info console and lands in the file logger), JSON output with RFC 3339 timestamps, and a nested scope rendered as a[database.gorm]prefix in text and acomponentfield in JSON. It also runs a child process that callsFatalContextagainst a slow Slack server and aLEVEL = Fatalconsole: both queued posts arrive, the fatal one is labelled fatal, and the console shows only the fatal line.go test ./internal/dbx/covers the GORM logger's levels (a failed query is an error, a slow query is a warning, record not found stays at debug) and checks that a failed insert hides its values unless every query is logged.go vet ./...,golangci-lint run, andgo test ./...pass.log/hooks/), LFS batch and basic download, and the/api/webpages. Trigger a 500 and confirm the error line has the request method and path.MODE = console, fileand confirmlog/gogs.logreceives the same lines as the console. WithRUN_MODE = prodthe output is JSON, andFORMAT = text(orGOGS_LOG_FORMAT=text) switches it back to text. In dev modeFORMAT = jsonswitches it to JSON.log/hooks/.AI usage
This pull request was written by Amp, using the logging setup in pgrok and git-lfs-server as the reference.