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.

179 lines
4.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 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
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 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. // Gitea (git with a cup of tea) is a painless self-hosted Git Service.
  6. package main // import "code.gitea.io/gitea"
  7. import (
  8. "fmt"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "code.gitea.io/gitea/cmd"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. // register supported doc types
  16. _ "code.gitea.io/gitea/modules/markup/csv"
  17. _ "code.gitea.io/gitea/modules/markup/markdown"
  18. _ "code.gitea.io/gitea/modules/markup/orgmode"
  19. "github.com/urfave/cli"
  20. )
  21. var (
  22. // Version holds the current Gitea version
  23. Version = "1.9.0-dev"
  24. // Tags holds the build tags used
  25. Tags = ""
  26. // MakeVersion holds the current Make version if built with make
  27. MakeVersion = ""
  28. originalAppHelpTemplate = ""
  29. originalCommandHelpTemplate = ""
  30. originalSubcommandHelpTemplate = ""
  31. )
  32. func init() {
  33. setting.AppVer = Version
  34. setting.AppBuiltWith = formatBuiltWith(Tags)
  35. // Grab the original help templates
  36. originalAppHelpTemplate = cli.AppHelpTemplate
  37. originalCommandHelpTemplate = cli.CommandHelpTemplate
  38. originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
  39. }
  40. func main() {
  41. app := cli.NewApp()
  42. app.Name = "Gitea"
  43. app.Usage = "A painless self-hosted Git service"
  44. app.Description = `By default, gitea will start serving using the webserver with no
  45. arguments - which can alternatively be run by running the subcommand web.`
  46. app.Version = Version + formatBuiltWith(Tags)
  47. app.Commands = []cli.Command{
  48. cmd.CmdWeb,
  49. cmd.CmdServ,
  50. cmd.CmdHook,
  51. cmd.CmdDump,
  52. cmd.CmdCert,
  53. cmd.CmdAdmin,
  54. cmd.CmdGenerate,
  55. cmd.CmdMigrate,
  56. cmd.CmdKeys,
  57. }
  58. // Now adjust these commands to add our global configuration options
  59. // First calculate the default paths and set the AppHelpTemplates in this context
  60. setting.SetCustomPathAndConf("", "")
  61. setAppHelpTemplates()
  62. // default configuration flags
  63. defaultFlags := []cli.Flag{
  64. cli.StringFlag{
  65. Name: "custom-path, C",
  66. Value: setting.CustomPath,
  67. Usage: "Custom path file path",
  68. },
  69. cli.StringFlag{
  70. Name: "config, c",
  71. Value: setting.CustomConf,
  72. Usage: "Custom configuration file path",
  73. },
  74. cli.VersionFlag,
  75. }
  76. // Set the default to be equivalent to cmdWeb and add the default flags
  77. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  78. app.Flags = append(app.Flags, defaultFlags...)
  79. app.Action = cmd.CmdWeb.Action
  80. // Add functions to set these paths and these flags to the commands
  81. app.Before = establishCustomPath
  82. for i := range app.Commands {
  83. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  84. }
  85. err := app.Run(os.Args)
  86. if err != nil {
  87. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  88. }
  89. }
  90. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  91. command.Flags = append(command.Flags, defaultFlags...)
  92. command.Before = establishCustomPath
  93. for i := range command.Subcommands {
  94. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  95. }
  96. }
  97. func establishCustomPath(ctx *cli.Context) error {
  98. var providedCustom string
  99. var providedConf string
  100. currentCtx := ctx
  101. for {
  102. if len(providedCustom) != 0 && len(providedConf) != 0 {
  103. break
  104. }
  105. if currentCtx == nil {
  106. break
  107. }
  108. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  109. providedCustom = currentCtx.String("custom-path")
  110. }
  111. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  112. providedConf = currentCtx.String("config")
  113. }
  114. currentCtx = currentCtx.Parent()
  115. }
  116. setting.SetCustomPathAndConf(providedCustom, providedConf)
  117. setAppHelpTemplates()
  118. if ctx.IsSet("version") {
  119. cli.ShowVersion(ctx)
  120. os.Exit(0)
  121. }
  122. return nil
  123. }
  124. func setAppHelpTemplates() {
  125. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  126. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  127. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  128. }
  129. func adjustHelpTemplate(originalTemplate string) string {
  130. overrided := ""
  131. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  132. overrided = "(GITEA_CUSTOM)"
  133. }
  134. return fmt.Sprintf(`%s
  135. DEFAULT CONFIGURATION:
  136. CustomPath: %s %s
  137. CustomConf: %s
  138. AppPath: %s
  139. AppWorkPath: %s
  140. `, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  141. }
  142. func formatBuiltWith(makeTags string) string {
  143. var version = runtime.Version()
  144. if len(MakeVersion) > 0 {
  145. version = MakeVersion + ", " + runtime.Version()
  146. }
  147. if len(Tags) == 0 {
  148. return " built with " + version
  149. }
  150. return " built with " + version + " : " + strings.Replace(Tags, " ", ", ", -1)
  151. }