diff options
-rw-r--r-- | .deadcode-out | 3 | ||||
-rw-r--r-- | modules/log/event_writer_buffer.go | 22 | ||||
-rw-r--r-- | modules/log/event_writer_buffer_test.go | 34 |
3 files changed, 59 insertions, 0 deletions
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) +} |