conn.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logs
  15. import (
  16. "encoding/json"
  17. "io"
  18. "net"
  19. "time"
  20. )
  21. // connWriter implements LoggerInterface.
  22. // it writes messages in keep-live tcp connection.
  23. type connWriter struct {
  24. lg *logWriter
  25. innerWriter io.WriteCloser
  26. ReconnectOnMsg bool `json:"reconnectOnMsg"`
  27. Reconnect bool `json:"reconnect"`
  28. Net string `json:"net"`
  29. Addr string `json:"addr"`
  30. Level int `json:"level"`
  31. }
  32. // NewConn create new ConnWrite returning as LoggerInterface.
  33. func NewConn() Logger {
  34. conn := new(connWriter)
  35. conn.Level = LevelTrace
  36. return conn
  37. }
  38. // Init init connection writer with json config.
  39. // json config only need key "level".
  40. func (c *connWriter) Init(jsonConfig string) error {
  41. return json.Unmarshal([]byte(jsonConfig), c)
  42. }
  43. // WriteMsg write message in connection.
  44. // if connection is down, try to re-connect.
  45. func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
  46. if level > c.Level {
  47. return nil
  48. }
  49. if c.needToConnectOnMsg() {
  50. err := c.connect()
  51. if err != nil {
  52. return err
  53. }
  54. }
  55. if c.ReconnectOnMsg {
  56. defer c.innerWriter.Close()
  57. }
  58. c.lg.println(when, msg)
  59. return nil
  60. }
  61. // Flush implementing method. empty.
  62. func (c *connWriter) Flush() {
  63. }
  64. // Destroy destroy connection writer and close tcp listener.
  65. func (c *connWriter) Destroy() {
  66. if c.innerWriter != nil {
  67. c.innerWriter.Close()
  68. }
  69. }
  70. func (c *connWriter) connect() error {
  71. if c.innerWriter != nil {
  72. c.innerWriter.Close()
  73. c.innerWriter = nil
  74. }
  75. conn, err := net.Dial(c.Net, c.Addr)
  76. if err != nil {
  77. return err
  78. }
  79. if tcpConn, ok := conn.(*net.TCPConn); ok {
  80. tcpConn.SetKeepAlive(true)
  81. }
  82. c.innerWriter = conn
  83. c.lg = newLogWriter(conn)
  84. return nil
  85. }
  86. func (c *connWriter) needToConnectOnMsg() bool {
  87. if c.Reconnect {
  88. c.Reconnect = false
  89. return true
  90. }
  91. if c.innerWriter == nil {
  92. return true
  93. }
  94. return c.ReconnectOnMsg
  95. }
  96. func init() {
  97. Register(AdapterConn, NewConn)
  98. }