loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add a buffer writer to the logger, for internal use (#6551)

Identical to console, file or conn but writes to a buffer instead.

It is useful in two contexts:

- tests that need to assert the logs in a way that is simpler than
LogChecker.
- capturing the logs of a given task to display in the web UI,
return from the API, etc.

Since all logged events at a given level are written to the buffer by default, it is best used with WriterMode.Expression to only keep the log lines of interest.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6551
Reviewed-by: Antonin Delpeuch <wetneb@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>

authored by

Earl Warren
Earl Warren
and committed by
Earl Warren
9a608a03 0858a879

+59
+3
.deadcode-out
··· 169 169 StdJSON.NewDecoder 170 170 StdJSON.Indent 171 171 172 + code.gitea.io/gitea/modules/log 173 + NewEventWriterBuffer 174 + 172 175 code.gitea.io/gitea/modules/markup 173 176 GetRendererByType 174 177 RenderString
+22
modules/log/event_writer_buffer.go
··· 1 + // Copyright 2025 The Forgejo Authors. 2 + // SPDX-License-Identifier: GPL-3.0-or-later 3 + 4 + package log 5 + 6 + import ( 7 + "bytes" 8 + ) 9 + 10 + type EventWriterBuffer struct { 11 + *EventWriterBaseImpl 12 + Buffer *bytes.Buffer 13 + } 14 + 15 + var _ EventWriter = (*EventWriterBuffer)(nil) 16 + 17 + func NewEventWriterBuffer(name string, mode WriterMode) *EventWriterBuffer { 18 + w := &EventWriterBuffer{EventWriterBaseImpl: NewEventWriterBase(name, "buffer", mode)} 19 + w.Buffer = new(bytes.Buffer) 20 + w.OutputWriteCloser = nopCloser{w.Buffer} 21 + return w 22 + }
+34
modules/log/event_writer_buffer_test.go
··· 1 + // Copyright 2025 The Forgejo Authors. 2 + // SPDX-License-Identifier: GPL-3.0-or-later 3 + 4 + package log_test 5 + 6 + import ( 7 + "context" 8 + "testing" 9 + 10 + "code.gitea.io/gitea/modules/log" 11 + 12 + "github.com/stretchr/testify/assert" 13 + ) 14 + 15 + func TestBufferLogger(t *testing.T) { 16 + prefix := "TestPrefix " 17 + level := log.INFO 18 + expected := "something" 19 + 20 + bufferWriter := log.NewEventWriterBuffer("test-buffer", log.WriterMode{ 21 + Level: level, 22 + Prefix: prefix, 23 + Expression: expected, 24 + }) 25 + 26 + logger := log.NewLoggerWithWriters(context.Background(), "test", bufferWriter) 27 + 28 + logger.SendLogEvent(&log.Event{ 29 + Level: log.INFO, 30 + MsgSimpleText: expected, 31 + }) 32 + logger.Close() 33 + assert.Contains(t, bufferWriter.Buffer.String(), expected) 34 + }