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.

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