From 9a608a034d870a83567baabdaafab0774df4b231 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 13 Jan 2025 13:40:24 +0000 Subject: [PATCH] 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/.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 Co-authored-by: Earl Warren Co-committed-by: Earl Warren --- .deadcode-out | 3 +++ modules/log/event_writer_buffer.go | 22 ++++++++++++++++ modules/log/event_writer_buffer_test.go | 34 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 modules/log/event_writer_buffer.go create mode 100644 modules/log/event_writer_buffer_test.go diff --git a/.deadcode-out b/.deadcode-out index ae0d358b51..64741ec7ac 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -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 diff --git a/modules/log/event_writer_buffer.go b/modules/log/event_writer_buffer.go new file mode 100644 index 0000000000..28857c2189 --- /dev/null +++ b/modules/log/event_writer_buffer.go @@ -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 +} diff --git a/modules/log/event_writer_buffer_test.go b/modules/log/event_writer_buffer_test.go new file mode 100644 index 0000000000..5c0343b8a8 --- /dev/null +++ b/modules/log/event_writer_buffer_test.go @@ -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) +}