summaryrefslogtreecommitdiffstats
path: root/modules/log/event_writer_file.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /modules/log/event_writer_file.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/log/event_writer_file.go')
-rw-r--r--modules/log/event_writer_file.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/modules/log/event_writer_file.go b/modules/log/event_writer_file.go
new file mode 100644
index 0000000..fd73d7d
--- /dev/null
+++ b/modules/log/event_writer_file.go
@@ -0,0 +1,53 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package log
+
+import (
+ "io"
+
+ "code.gitea.io/gitea/modules/util/rotatingfilewriter"
+)
+
+type WriterFileOption struct {
+ FileName string
+ MaxSize int64
+ LogRotate bool
+ DailyRotate bool
+ MaxDays int
+ Compress bool
+ CompressionLevel int
+}
+
+type eventWriterFile struct {
+ *EventWriterBaseImpl
+ fileWriter io.WriteCloser
+}
+
+var _ EventWriter = (*eventWriterFile)(nil)
+
+func NewEventWriterFile(name string, mode WriterMode) EventWriter {
+ w := &eventWriterFile{EventWriterBaseImpl: NewEventWriterBase(name, "file", mode)}
+ opt := mode.WriterOption.(WriterFileOption)
+ var err error
+ w.fileWriter, err = rotatingfilewriter.Open(opt.FileName, &rotatingfilewriter.Options{
+ Rotate: opt.LogRotate,
+ MaximumSize: opt.MaxSize,
+ RotateDaily: opt.DailyRotate,
+ KeepDays: opt.MaxDays,
+ Compress: opt.Compress,
+ CompressionLevel: opt.CompressionLevel,
+ })
+ if err != nil {
+ // if the log file can't be opened, what should it do? panic/exit? ignore logs? fallback to stderr?
+ // it seems that "fallback to stderr" is slightly better than others ....
+ FallbackErrorf("unable to open log file %q: %v", opt.FileName, err)
+ w.fileWriter = nopCloser{Writer: LoggerToWriter(FallbackErrorf)}
+ }
+ w.OutputWriteCloser = w.fileWriter
+ return w
+}
+
+func init() {
+ RegisterEventWriter("file", NewEventWriterFile)
+}