You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_writer_conn.go 2.0KB

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>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package log
  4. import (
  5. "io"
  6. "net"
  7. )
  8. type WriterConnOption struct {
  9. Addr string
  10. Protocol string
  11. Reconnect bool
  12. ReconnectOnMsg bool
  13. }
  14. type eventWriterConn struct {
  15. *EventWriterBaseImpl
  16. connWriter connWriter
  17. }
  18. var _ EventWriter = (*eventWriterConn)(nil)
  19. func NewEventWriterConn(writerName string, writerMode WriterMode) EventWriter {
  20. w := &eventWriterConn{EventWriterBaseImpl: NewEventWriterBase(writerName, "conn", writerMode)}
  21. opt := writerMode.WriterOption.(WriterConnOption)
  22. w.connWriter = connWriter{
  23. ReconnectOnMsg: opt.ReconnectOnMsg,
  24. Reconnect: opt.Reconnect,
  25. Net: opt.Protocol,
  26. Addr: opt.Addr,
  27. }
  28. w.OutputWriteCloser = &w.connWriter
  29. return w
  30. }
  31. func init() {
  32. RegisterEventWriter("conn", NewEventWriterConn)
  33. }
  34. // below is copied from old code
  35. type connWriter struct {
  36. innerWriter io.WriteCloser
  37. ReconnectOnMsg bool
  38. Reconnect bool
  39. Net string `json:"net"`
  40. Addr string `json:"addr"`
  41. }
  42. var _ io.WriteCloser = (*connWriter)(nil)
  43. // Close the inner writer
  44. func (i *connWriter) Close() error {
  45. if i.innerWriter != nil {
  46. return i.innerWriter.Close()
  47. }
  48. return nil
  49. }
  50. // Write the data to the connection
  51. func (i *connWriter) Write(p []byte) (int, error) {
  52. if i.neededConnectOnMsg() {
  53. if err := i.connect(); err != nil {
  54. return 0, err
  55. }
  56. }
  57. if i.ReconnectOnMsg {
  58. defer i.innerWriter.Close()
  59. }
  60. return i.innerWriter.Write(p)
  61. }
  62. func (i *connWriter) neededConnectOnMsg() bool {
  63. if i.Reconnect {
  64. i.Reconnect = false
  65. return true
  66. }
  67. if i.innerWriter == nil {
  68. return true
  69. }
  70. return i.ReconnectOnMsg
  71. }
  72. func (i *connWriter) connect() error {
  73. if i.innerWriter != nil {
  74. _ = i.innerWriter.Close()
  75. i.innerWriter = nil
  76. }
  77. conn, err := net.Dial(i.Net, i.Addr)
  78. if err != nil {
  79. return err
  80. }
  81. if tcpConn, ok := conn.(*net.TCPConn); ok {
  82. err = tcpConn.SetKeepAlive(true)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. i.innerWriter = conn
  88. return nil
  89. }