From 674df05b16f396bf6f2a952923b9c2dfe371415b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 24 Jul 2023 12:57:21 +0800 Subject: [PATCH] Use stderr as fallback if the log file can't be opened (#26074) 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 .... --- modules/log/event_writer_file.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/log/event_writer_file.go b/modules/log/event_writer_file.go index 4f41b96453..fd73d7d30a 100644 --- a/modules/log/event_writer_file.go +++ b/modules/log/event_writer_file.go @@ -4,6 +4,8 @@ package log import ( + "io" + "code.gitea.io/gitea/modules/util/rotatingfilewriter" ) @@ -19,7 +21,7 @@ type WriterFileOption struct { type eventWriterFile struct { *EventWriterBaseImpl - fileWriter *rotatingfilewriter.RotatingFileWriter + fileWriter io.WriteCloser } var _ EventWriter = (*eventWriterFile)(nil) @@ -37,7 +39,10 @@ func NewEventWriterFile(name string, mode WriterMode) EventWriter { 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