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

admin.go 3.5 kB

Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "os"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/storage"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "github.com/unknwon/com"
  13. )
  14. //NoticeType describes the notice type
  15. type NoticeType int
  16. const (
  17. //NoticeRepository type
  18. NoticeRepository NoticeType = iota + 1
  19. // NoticeTask type
  20. NoticeTask
  21. )
  22. // Notice represents a system notice for admin.
  23. type Notice struct {
  24. ID int64 `xorm:"pk autoincr"`
  25. Type NoticeType
  26. Description string `xorm:"TEXT"`
  27. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  28. }
  29. // TrStr returns a translation format string.
  30. func (n *Notice) TrStr() string {
  31. return "admin.notices.type_" + com.ToStr(n.Type)
  32. }
  33. // CreateNotice creates new system notice.
  34. func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
  35. return createNotice(x, tp, desc, args...)
  36. }
  37. func createNotice(e Engine, tp NoticeType, desc string, args ...interface{}) error {
  38. if len(args) > 0 {
  39. desc = fmt.Sprintf(desc, args...)
  40. }
  41. n := &Notice{
  42. Type: tp,
  43. Description: desc,
  44. }
  45. _, err := e.Insert(n)
  46. return err
  47. }
  48. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  49. func CreateRepositoryNotice(desc string, args ...interface{}) error {
  50. return createNotice(x, NoticeRepository, desc, args...)
  51. }
  52. // RemoveAllWithNotice removes all directories in given path and
  53. // creates a system notice when error occurs.
  54. func RemoveAllWithNotice(title, path string) {
  55. removeAllWithNotice(x, title, path)
  56. }
  57. // RemoveStorageWithNotice removes a file from the storage and
  58. // creates a system notice when error occurs.
  59. func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) {
  60. if err := bucket.Delete(path); err != nil {
  61. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  62. log.Warn(title+" [%s]: %v", path, err)
  63. if err = createNotice(x, NoticeRepository, desc); err != nil {
  64. log.Error("CreateRepositoryNotice: %v", err)
  65. }
  66. }
  67. }
  68. func removeAllWithNotice(e Engine, title, path string) {
  69. if err := os.RemoveAll(path); err != nil {
  70. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  71. log.Warn(title+" [%s]: %v", path, err)
  72. if err = createNotice(e, NoticeRepository, desc); err != nil {
  73. log.Error("CreateRepositoryNotice: %v", err)
  74. }
  75. }
  76. }
  77. // CountNotices returns number of notices.
  78. func CountNotices() int64 {
  79. count, _ := x.Count(new(Notice))
  80. return count
  81. }
  82. // Notices returns notices in given page.
  83. func Notices(page, pageSize int) ([]*Notice, error) {
  84. notices := make([]*Notice, 0, pageSize)
  85. return notices, x.
  86. Limit(pageSize, (page-1)*pageSize).
  87. Desc("id").
  88. Find(&notices)
  89. }
  90. // DeleteNotice deletes a system notice by given ID.
  91. func DeleteNotice(id int64) error {
  92. _, err := x.ID(id).Delete(new(Notice))
  93. return err
  94. }
  95. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  96. func DeleteNotices(start, end int64) error {
  97. sess := x.Where("id >= ?", start)
  98. if end > 0 {
  99. sess.And("id <= ?", end)
  100. }
  101. _, err := sess.Delete(new(Notice))
  102. return err
  103. }
  104. // DeleteNoticesByIDs deletes notices by given IDs.
  105. func DeleteNoticesByIDs(ids []int64) error {
  106. if len(ids) == 0 {
  107. return nil
  108. }
  109. _, err := x.
  110. In("id", ids).
  111. Delete(new(Notice))
  112. return err
  113. }