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.

217 lines
6.8 KiB

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
5 years ago
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
5 years ago
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
5 years ago
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
5 years ago
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
5 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "bytes"
  7. "fmt"
  8. "html/template"
  9. "path"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/mailer"
  13. "code.gitea.io/gitea/modules/markup"
  14. "code.gitea.io/gitea/modules/markup/markdown"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/timeutil"
  17. "gopkg.in/gomail.v2"
  18. )
  19. const (
  20. mailAuthActivate base.TplName = "auth/activate"
  21. mailAuthActivateEmail base.TplName = "auth/activate_email"
  22. mailAuthResetPassword base.TplName = "auth/reset_passwd"
  23. mailAuthRegisterNotify base.TplName = "auth/register_notify"
  24. mailIssueComment base.TplName = "issue/comment"
  25. mailIssueMention base.TplName = "issue/mention"
  26. mailNotifyCollaborator base.TplName = "notify/collaborator"
  27. )
  28. var templates *template.Template
  29. // InitMailRender initializes the mail renderer
  30. func InitMailRender(tmpls *template.Template) {
  31. templates = tmpls
  32. }
  33. // SendTestMail sends a test mail
  34. func SendTestMail(email string) error {
  35. return gomail.Send(mailer.Sender, mailer.NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
  36. }
  37. // SendUserMail sends a mail to the user
  38. func SendUserMail(language string, u *User, tpl base.TplName, code, subject, info string) {
  39. data := map[string]interface{}{
  40. "DisplayName": u.DisplayName(),
  41. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
  42. "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
  43. "Code": code,
  44. }
  45. var content bytes.Buffer
  46. if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  47. log.Error("Template: %v", err)
  48. return
  49. }
  50. msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
  51. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  52. mailer.SendAsync(msg)
  53. }
  54. // Locale represents an interface to translation
  55. type Locale interface {
  56. Language() string
  57. Tr(string, ...interface{}) string
  58. }
  59. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  60. func SendActivateAccountMail(locale Locale, u *User) {
  61. SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
  62. }
  63. // SendResetPasswordMail sends a password reset mail to the user
  64. func SendResetPasswordMail(locale Locale, u *User) {
  65. SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
  66. }
  67. // SendActivateEmailMail sends confirmation email to confirm new email address
  68. func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
  69. data := map[string]interface{}{
  70. "DisplayName": u.DisplayName(),
  71. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
  72. "Code": u.GenerateEmailActivateCode(email.Email),
  73. "Email": email.Email,
  74. }
  75. var content bytes.Buffer
  76. if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  77. log.Error("Template: %v", err)
  78. return
  79. }
  80. msg := mailer.NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
  81. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  82. mailer.SendAsync(msg)
  83. }
  84. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  85. func SendRegisterNotifyMail(locale Locale, u *User) {
  86. data := map[string]interface{}{
  87. "DisplayName": u.DisplayName(),
  88. "Username": u.Name,
  89. }
  90. var content bytes.Buffer
  91. if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  92. log.Error("Template: %v", err)
  93. return
  94. }
  95. msg := mailer.NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
  96. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  97. mailer.SendAsync(msg)
  98. }
  99. // SendCollaboratorMail sends mail notification to new collaborator.
  100. func SendCollaboratorMail(u, doer *User, repo *Repository) {
  101. repoName := path.Join(repo.Owner.Name, repo.Name)
  102. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  103. data := map[string]interface{}{
  104. "Subject": subject,
  105. "RepoName": repoName,
  106. "Link": repo.HTMLURL(),
  107. }
  108. var content bytes.Buffer
  109. if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
  110. log.Error("Template: %v", err)
  111. return
  112. }
  113. msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
  114. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  115. mailer.SendAsync(msg)
  116. }
  117. func composeTplData(subject, body, link string) map[string]interface{} {
  118. data := make(map[string]interface{}, 10)
  119. data["Subject"] = subject
  120. data["Body"] = body
  121. data["Link"] = link
  122. return data
  123. }
  124. func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
  125. var subject string
  126. if comment != nil {
  127. subject = "Re: " + issue.mailSubject()
  128. } else {
  129. subject = issue.mailSubject()
  130. }
  131. err := issue.LoadRepo()
  132. if err != nil {
  133. log.Error("LoadRepo: %v", err)
  134. }
  135. body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  136. var data = make(map[string]interface{}, 10)
  137. if comment != nil {
  138. data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
  139. } else {
  140. data = composeTplData(subject, body, issue.HTMLURL())
  141. }
  142. data["Doer"] = doer
  143. var mailBody bytes.Buffer
  144. if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
  145. log.Error("Template: %v", err)
  146. }
  147. msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
  148. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  149. // Set Message-ID on first message so replies know what to reference
  150. if comment == nil {
  151. msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">")
  152. } else {
  153. msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">")
  154. msg.SetHeader("References", "<"+issue.ReplyReference()+">")
  155. }
  156. return msg
  157. }
  158. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  159. func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
  160. if len(tos) == 0 {
  161. return
  162. }
  163. mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
  164. }
  165. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  166. func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
  167. if len(tos) == 0 {
  168. return
  169. }
  170. mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
  171. }