diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-22 06:35:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-21 22:35:11 +0000 |
commit | 4647660776436f0a83129b4ceb8426b1fb0599bb (patch) | |
tree | 465ae0fc7cb7c4b0b30f6600a701c240cdf2a3a6 /modules/log/conn.go | |
parent | 65dff8e36459a7852c40ebb4732a598ddb9bf915 (diff) | |
download | gitea-4647660776436f0a83129b4ceb8426b1fb0599bb.tar.gz gitea-4647660776436f0a83129b4ceb8426b1fb0599bb.zip |
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>
Diffstat (limited to 'modules/log/conn.go')
-rw-r--r-- | modules/log/conn.go | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/modules/log/conn.go b/modules/log/conn.go deleted file mode 100644 index b21a744037..0000000000 --- a/modules/log/conn.go +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Copyright 2019 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package log - -import ( - "fmt" - "io" - "net" - - "code.gitea.io/gitea/modules/json" -) - -type connWriter struct { - innerWriter io.WriteCloser - ReconnectOnMsg bool `json:"reconnectOnMsg"` - Reconnect bool `json:"reconnect"` - Net string `json:"net"` - Addr string `json:"addr"` -} - -// Close the inner writer -func (i *connWriter) Close() error { - if i.innerWriter != nil { - return i.innerWriter.Close() - } - return nil -} - -// Write the data to the connection -func (i *connWriter) Write(p []byte) (int, error) { - if i.neededConnectOnMsg() { - if err := i.connect(); err != nil { - return 0, err - } - } - - if i.ReconnectOnMsg { - defer i.innerWriter.Close() - } - - return i.innerWriter.Write(p) -} - -func (i *connWriter) neededConnectOnMsg() bool { - if i.Reconnect { - i.Reconnect = false - return true - } - - if i.innerWriter == nil { - return true - } - - return i.ReconnectOnMsg -} - -func (i *connWriter) connect() error { - if i.innerWriter != nil { - i.innerWriter.Close() - i.innerWriter = nil - } - - conn, err := net.Dial(i.Net, i.Addr) - if err != nil { - return err - } - - if tcpConn, ok := conn.(*net.TCPConn); ok { - err = tcpConn.SetKeepAlive(true) - if err != nil { - return err - } - } - - i.innerWriter = conn - return nil -} - -func (i *connWriter) releaseReopen() error { - if i.innerWriter != nil { - return i.connect() - } - return nil -} - -// ConnLogger implements LoggerProvider. -// it writes messages in keep-live tcp connection. -type ConnLogger struct { - WriterLogger - ReconnectOnMsg bool `json:"reconnectOnMsg"` - Reconnect bool `json:"reconnect"` - Net string `json:"net"` - Addr string `json:"addr"` -} - -// NewConn creates new ConnLogger returning as LoggerProvider. -func NewConn() LoggerProvider { - conn := new(ConnLogger) - conn.Level = TRACE - return conn -} - -// Init inits connection writer with json config. -// json config only need key "level". -func (log *ConnLogger) Init(jsonconfig string) error { - err := json.Unmarshal([]byte(jsonconfig), log) - if err != nil { - return fmt.Errorf("Unable to parse JSON: %w", err) - } - log.NewWriterLogger(&connWriter{ - ReconnectOnMsg: log.ReconnectOnMsg, - Reconnect: log.Reconnect, - Net: log.Net, - Addr: log.Addr, - }, log.Level) - return nil -} - -// Flush does nothing for this implementation -func (log *ConnLogger) Flush() { -} - -// GetName returns the default name for this implementation -func (log *ConnLogger) GetName() string { - return "conn" -} - -// ReleaseReopen causes the ConnLogger to reconnect to the server -func (log *ConnLogger) ReleaseReopen() error { - return log.out.(*connWriter).releaseReopen() -} - -func init() { - Register("conn", NewConn) -} |