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.

main.go 5.3 kB

11 years ago
5 years ago
11 years ago
11 years ago
11 years ago
11 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
6 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "time"
  13. "code.gitea.io/gitea/cmd"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. // register supported doc types
  17. _ "code.gitea.io/gitea/modules/markup/csv"
  18. _ "code.gitea.io/gitea/modules/markup/markdown"
  19. _ "code.gitea.io/gitea/modules/markup/orgmode"
  20. _ "code.gitea.io/gitea/modules/timer"
  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 = time.Now().String()
  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. cmd.CmdMigrateStorage,
  64. }
  65. // Now adjust these commands to add our global configuration options
  66. // First calculate the default paths and set the AppHelpTemplates in this context
  67. setting.SetCustomPathAndConf("", "", "")
  68. setAppHelpTemplates()
  69. // default configuration flags
  70. defaultFlags := []cli.Flag{
  71. cli.StringFlag{
  72. Name: "custom-path, C",
  73. Value: setting.CustomPath,
  74. Usage: "Custom path file path",
  75. },
  76. cli.StringFlag{
  77. Name: "config, c",
  78. Value: setting.CustomConf,
  79. Usage: "Custom configuration file path",
  80. },
  81. cli.VersionFlag,
  82. cli.StringFlag{
  83. Name: "work-path, w",
  84. Value: setting.AppWorkPath,
  85. Usage: "Set the gitea working path",
  86. },
  87. }
  88. // Set the default to be equivalent to cmdWeb and add the default flags
  89. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  90. app.Flags = append(app.Flags, defaultFlags...)
  91. app.Action = cmd.CmdWeb.Action
  92. // Add functions to set these paths and these flags to the commands
  93. app.Before = establishCustomPath
  94. for i := range app.Commands {
  95. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  96. }
  97. err := app.Run(os.Args)
  98. if err != nil {
  99. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  100. }
  101. }
  102. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  103. command.Flags = append(command.Flags, defaultFlags...)
  104. command.Before = establishCustomPath
  105. for i := range command.Subcommands {
  106. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  107. }
  108. }
  109. func establishCustomPath(ctx *cli.Context) error {
  110. var providedCustom string
  111. var providedConf string
  112. var providedWorkPath string
  113. currentCtx := ctx
  114. for {
  115. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  116. break
  117. }
  118. if currentCtx == nil {
  119. break
  120. }
  121. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  122. providedCustom = currentCtx.String("custom-path")
  123. }
  124. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  125. providedConf = currentCtx.String("config")
  126. }
  127. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  128. providedWorkPath = currentCtx.String("work-path")
  129. }
  130. currentCtx = currentCtx.Parent()
  131. }
  132. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  133. setAppHelpTemplates()
  134. if ctx.IsSet("version") {
  135. cli.ShowVersion(ctx)
  136. os.Exit(0)
  137. }
  138. return nil
  139. }
  140. func setAppHelpTemplates() {
  141. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  142. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  143. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  144. }
  145. func adjustHelpTemplate(originalTemplate string) string {
  146. overrided := ""
  147. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  148. overrided = "(GITEA_CUSTOM)"
  149. }
  150. return fmt.Sprintf(`%s
  151. DEFAULT CONFIGURATION:
  152. CustomPath: %s %s
  153. CustomConf: %s
  154. AppPath: %s
  155. AppWorkPath: %s
  156. `, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  157. }
  158. func formatBuiltWith() string {
  159. var version = runtime.Version()
  160. if len(MakeVersion) > 0 {
  161. version = MakeVersion + ", " + runtime.Version()
  162. }
  163. if len(Tags) == 0 {
  164. return " built with " + version
  165. }
  166. return " built with " + version + " : " + strings.Replace(Tags, " ", ", ", -1)
  167. }