1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-16 15:14:24 +00:00
gitea/modules/util/rotatingfilewriter/writer.go
wxiaoguang 4647660776
Rewrite logger system (#24726)
## ⚠️ Breaking

The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.

Although many legacy options still work, it's encouraged to upgrade to
the new options.

The SMTP logger is deleted because SMTP is not suitable to collect logs.

If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.

## Description

Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)

Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)

There is a new document (with examples): `logging-config.en-us.md`

This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.

## The old problems

The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.


## The new design

See `logger.go` for documents.


## Screenshot

<details>


![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)


![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)


![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)

</details>

## TODO

* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)

---------

Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 22:35:11 +00:00

247 lines
5.5 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package rotatingfilewriter
import (
"bufio"
"compress/gzip"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"code.gitea.io/gitea/modules/graceful/releasereopen"
"code.gitea.io/gitea/modules/util"
)
type Options struct {
Rotate bool
MaximumSize int64
RotateDaily bool
KeepDays int
Compress bool
CompressionLevel int
}
type RotatingFileWriter struct {
mu sync.Mutex
fd *os.File
currentSize int64
openDate int
options Options
cancelReleaseReopen func()
}
var ErrorPrintf func(format string, args ...interface{})
// errorf tries to print error messages. Since this writer could be used by a logger system, this is the last chance to show the error in some cases
func errorf(format string, args ...interface{}) {
if ErrorPrintf != nil {
ErrorPrintf("rotatingfilewriter: "+format+"\n", args...)
}
}
// Open creates a new rotating file writer.
// Notice: if a file is opened by two rotators, there will be conflicts when rotating.
// In the future, there should be "rotating file manager"
func Open(filename string, options *Options) (*RotatingFileWriter, error) {
if options == nil {
options = &Options{}
}
rfw := &RotatingFileWriter{
options: *options,
}
if err := rfw.open(filename); err != nil {
return nil, err
}
rfw.cancelReleaseReopen = releasereopen.GetManager().Register(rfw)
return rfw, nil
}
func (rfw *RotatingFileWriter) Write(b []byte) (int, error) {
if rfw.options.Rotate && ((rfw.options.MaximumSize > 0 && rfw.currentSize >= rfw.options.MaximumSize) || (rfw.options.RotateDaily && time.Now().Day() != rfw.openDate)) {
if err := rfw.DoRotate(); err != nil {
// if this writer is used by a logger system, it's the logger system's responsibility to handle/show the error
return 0, err
}
}
n, err := rfw.fd.Write(b)
if err == nil {
rfw.currentSize += int64(n)
}
return n, err
}
func (rfw *RotatingFileWriter) Flush() error {
return rfw.fd.Sync()
}
func (rfw *RotatingFileWriter) Close() error {
rfw.mu.Lock()
if rfw.cancelReleaseReopen != nil {
rfw.cancelReleaseReopen()
rfw.cancelReleaseReopen = nil
}
rfw.mu.Unlock()
return rfw.fd.Close()
}
func (rfw *RotatingFileWriter) open(filename string) error {
fd, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o660)
if err != nil {
return err
}
rfw.fd = fd
finfo, err := fd.Stat()
if err != nil {
return err
}
rfw.currentSize = finfo.Size()
rfw.openDate = finfo.ModTime().Day()
return nil
}
func (rfw *RotatingFileWriter) ReleaseReopen() error {
return errors.Join(
rfw.fd.Close(),
rfw.open(rfw.fd.Name()),
)
}
// DoRotate the log file creating a backup like xx.2013-01-01.2
func (rfw *RotatingFileWriter) DoRotate() error {
if !rfw.options.Rotate {
return nil
}
rfw.mu.Lock()
defer rfw.mu.Unlock()
prefix := fmt.Sprintf("%s.%s.", rfw.fd.Name(), time.Now().Format("2006-01-02"))
var err error
fname := ""
for i := 1; err == nil && i <= 999; i++ {
fname = prefix + fmt.Sprintf("%03d", i)
_, err = os.Lstat(fname)
if rfw.options.Compress && err != nil {
_, err = os.Lstat(fname + ".gz")
}
}
// return error if the last file checked still existed
if err == nil {
return fmt.Errorf("cannot find free file to rename %s", rfw.fd.Name())
}
fd := rfw.fd
if err := fd.Close(); err != nil { // close file before rename
return err
}
if err := util.Rename(fd.Name(), fname); err != nil {
return err
}
if rfw.options.Compress {
go func() {
err := compressOldFile(fname, rfw.options.CompressionLevel)
if err != nil {
errorf("DoRotate: %v", err)
}
}()
}
if err := rfw.open(fd.Name()); err != nil {
return err
}
go deleteOldFiles(
filepath.Dir(fd.Name()),
filepath.Base(fd.Name()),
time.Now().AddDate(0, 0, -rfw.options.KeepDays),
)
return nil
}
func compressOldFile(fname string, compressionLevel int) error {
reader, err := os.Open(fname)
if err != nil {
return fmt.Errorf("compressOldFile: failed to open existing file %s: %w", fname, err)
}
defer reader.Close()
buffer := bufio.NewReader(reader)
fnameGz := fname + ".gz"
fw, err := os.OpenFile(fnameGz, os.O_WRONLY|os.O_CREATE, 0o660)
if err != nil {
return fmt.Errorf("compressOldFile: failed to open new file %s: %w", fnameGz, err)
}
defer fw.Close()
zw, err := gzip.NewWriterLevel(fw, compressionLevel)
if err != nil {
return fmt.Errorf("compressOldFile: failed to create gzip writer: %w", err)
}
defer zw.Close()
_, err = buffer.WriteTo(zw)
if err != nil {
_ = zw.Close()
_ = fw.Close()
_ = util.Remove(fname + ".gz")
return fmt.Errorf("compressOldFile: failed to write to gz file: %w", err)
}
_ = reader.Close()
err = util.Remove(fname)
if err != nil {
return fmt.Errorf("compressOldFile: failed to delete old file: %w", err)
}
return nil
}
func deleteOldFiles(dir, prefix string, removeBefore time.Time) {
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) (returnErr error) {
defer func() {
if r := recover(); r != nil {
returnErr = fmt.Errorf("unable to delete old file '%s', error: %+v", path, r)
}
}()
if err != nil {
return err
}
if d.IsDir() {
return nil
}
info, err := d.Info()
if err != nil {
return err
}
if info.ModTime().Before(removeBefore) {
if strings.HasPrefix(filepath.Base(path), prefix) {
return util.Remove(path)
}
}
return nil
})
if err != nil {
errorf("deleteOldFiles: failed to delete old file: %v", err)
}
}