1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-01-20 16:50:28 -05:00

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>
This commit is contained in:
Earl Warren 2025-01-13 13:40:24 +00:00 committed by Earl Warren
parent 0858a879e5
commit 9a608a034d
3 changed files with 59 additions and 0 deletions

View file

@ -169,6 +169,9 @@ code.gitea.io/gitea/modules/json
StdJSON.NewDecoder
StdJSON.Indent
code.gitea.io/gitea/modules/log
NewEventWriterBuffer
code.gitea.io/gitea/modules/markup
GetRendererByType
RenderString

View file

@ -0,0 +1,22 @@
// Copyright 2025 The Forgejo Authors.
// SPDX-License-Identifier: GPL-3.0-or-later
package log
import (
"bytes"
)
type EventWriterBuffer struct {
*EventWriterBaseImpl
Buffer *bytes.Buffer
}
var _ EventWriter = (*EventWriterBuffer)(nil)
func NewEventWriterBuffer(name string, mode WriterMode) *EventWriterBuffer {
w := &EventWriterBuffer{EventWriterBaseImpl: NewEventWriterBase(name, "buffer", mode)}
w.Buffer = new(bytes.Buffer)
w.OutputWriteCloser = nopCloser{w.Buffer}
return w
}

View file

@ -0,0 +1,34 @@
// Copyright 2025 The Forgejo Authors.
// SPDX-License-Identifier: GPL-3.0-or-later
package log_test
import (
"context"
"testing"
"code.gitea.io/gitea/modules/log"
"github.com/stretchr/testify/assert"
)
func TestBufferLogger(t *testing.T) {
prefix := "TestPrefix "
level := log.INFO
expected := "something"
bufferWriter := log.NewEventWriterBuffer("test-buffer", log.WriterMode{
Level: level,
Prefix: prefix,
Expression: expected,
})
logger := log.NewLoggerWithWriters(context.Background(), "test", bufferWriter)
logger.SendLogEvent(&log.Event{
Level: log.INFO,
MsgSimpleText: expected,
})
logger.Close()
assert.Contains(t, bufferWriter.Buffer.String(), expected)
}