mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-01-21 16:55:06 -05:00
9a608a034d
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>
34 lines
715 B
Go
34 lines
715 B
Go
// 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)
|
|
}
|