logs.go 721 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package common
  2. import (
  3. "github.com/astaxie/beego/logs"
  4. "time"
  5. )
  6. const MaxMsgLen = 5000
  7. var logMsgs string
  8. func init() {
  9. logs.Register("store", func() logs.Logger {
  10. return new(StoreMsg)
  11. })
  12. }
  13. func GetLogMsg() string {
  14. return logMsgs
  15. }
  16. type StoreMsg struct {
  17. }
  18. func (lg *StoreMsg) Init(config string) error {
  19. return nil
  20. }
  21. func (lg *StoreMsg) WriteMsg(when time.Time, msg string, level int) error {
  22. m := when.Format("2006-01-02 15:04:05") + " " + msg + "\r\n"
  23. if len(logMsgs) > MaxMsgLen {
  24. start := MaxMsgLen - len(m)
  25. if start <= 0 {
  26. start = MaxMsgLen
  27. }
  28. logMsgs = logMsgs[start:]
  29. }
  30. logMsgs += m
  31. return nil
  32. }
  33. func (lg *StoreMsg) Destroy() {
  34. return
  35. }
  36. func (lg *StoreMsg) Flush() {
  37. return
  38. }