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.

92 lines
1.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 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 log is a wrapper of logs for short calling name.
  5. package log
  6. import (
  7. "fmt"
  8. "os"
  9. "path"
  10. "github.com/gogits/logs"
  11. )
  12. var (
  13. loggers []*logs.BeeLogger
  14. GitLogger *logs.BeeLogger
  15. )
  16. func init() {
  17. NewLogger(0, "console", `{"level": 0}`)
  18. }
  19. func NewLogger(bufLen int64, mode, config string) {
  20. logger := logs.NewLogger(bufLen)
  21. isExist := false
  22. for _, l := range loggers {
  23. if l.Adapter == mode {
  24. isExist = true
  25. l = logger
  26. }
  27. }
  28. if !isExist {
  29. loggers = append(loggers, logger)
  30. }
  31. logger.SetLogFuncCallDepth(3)
  32. if err := logger.SetLogger(mode, config); err != nil {
  33. Fatal("Fail to set logger(%s): %v", mode, err)
  34. }
  35. }
  36. func NewGitLogger(logPath string) {
  37. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  38. GitLogger = logs.NewLogger(0)
  39. GitLogger.SetLogger("file", fmt.Sprintf(`{"level":0,"filename":"%s","rotate":false}`, logPath))
  40. }
  41. func Trace(format string, v ...interface{}) {
  42. for _, logger := range loggers {
  43. logger.Trace(format, v...)
  44. }
  45. }
  46. func Debug(format string, v ...interface{}) {
  47. for _, logger := range loggers {
  48. logger.Debug(format, v...)
  49. }
  50. }
  51. func Info(format string, v ...interface{}) {
  52. for _, logger := range loggers {
  53. logger.Info(format, v...)
  54. }
  55. }
  56. func Error(format string, v ...interface{}) {
  57. for _, logger := range loggers {
  58. logger.Error(format, v...)
  59. }
  60. }
  61. func Warn(format string, v ...interface{}) {
  62. for _, logger := range loggers {
  63. logger.Warn(format, v...)
  64. }
  65. }
  66. func Critical(format string, v ...interface{}) {
  67. for _, logger := range loggers {
  68. logger.Critical(format, v...)
  69. }
  70. }
  71. func Fatal(format string, v ...interface{}) {
  72. Error(format, v...)
  73. for _, l := range loggers {
  74. l.Close()
  75. }
  76. os.Exit(2)
  77. }