From 8bfa7ae7453e56191aa6f7de8067d5b6bb5a4a8b Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 20 Jun 2014 01:14:54 -0400 Subject: [PATCH] Remove qiniu/log --- cmd/serve.go | 58 ++++++++++++++------------------------------ cmd/update.go | 20 +++------------ models/action.go | 3 +-- models/publickey.go | 7 +++--- models/update.go | 23 +++++++++--------- modules/log/log.go | 11 ++++++++- routers/install.go | 1 + routers/repo/http.go | 16 ++++++------ 8 files changed, 56 insertions(+), 83 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index a21500c775..62e290d82a 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -13,9 +13,9 @@ import ( "strings" "github.com/codegangsta/cli" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" ) @@ -27,26 +27,13 @@ var CmdServ = cli.Command{ Flags: []cli.Flag{}, } -func newLogger(logPath string) { - os.MkdirAll(path.Dir(logPath), os.ModePerm) - - f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) - if err != nil { - qlog.Fatal("Fail to open log file(%s): %v", logPath, err) - } - - qlog.SetOutput(f) - qlog.Info("Start logging serv...") -} - func setup(logPath string) { - workDir, _ := setting.WorkDir() - newLogger(path.Join(workDir, logPath)) - setting.NewConfigContext() + log.NewGitLogger(path.Join(setting.LogRootPath, logPath)) models.LoadModelsConfig() if models.UseSQLite3 { + workDir, _ := setting.WorkDir() os.Chdir(workDir) } @@ -87,27 +74,27 @@ func In(b string, sl map[string]int) bool { } func runServ(k *cli.Context) { - setup(path.Join(setting.LogRootPath, "serv.log")) + setup("serv.log") keys := strings.Split(os.Args[2], "-") if len(keys) != 2 { println("Gogs: auth file format error") - qlog.Fatal("Invalid auth file format: %s", os.Args[2]) + log.GitLogger.Fatal("Invalid auth file format: %s", os.Args[2]) } keyId, err := strconv.ParseInt(keys[1], 10, 64) if err != nil { println("Gogs: auth file format error") - qlog.Fatalf("Invalid auth file format: %v", err) + log.GitLogger.Fatal("Invalid auth file format: %v", err) } user, err := models.GetUserByKeyId(keyId) if err != nil { if err == models.ErrUserNotKeyOwner { println("Gogs: you are not the owner of SSH key") - qlog.Fatalf("Invalid owner of SSH key: %d", keyId) + log.GitLogger.Fatal("Invalid owner of SSH key: %d", keyId) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get user by key ID(%d): %v", keyId, err) + log.GitLogger.Fatal("Fail to get user by key ID(%d): %v", keyId, err) } cmd := os.Getenv("SSH_ORIGINAL_COMMAND") @@ -121,7 +108,7 @@ func runServ(k *cli.Context) { rr := strings.SplitN(repoPath, "/", 2) if len(rr) != 2 { println("Gogs: unavailable repository", args) - qlog.Fatalf("Unavailable repository: %v", args) + log.GitLogger.Fatal("Unavailable repository: %v", args) } repoUserName := rr[0] repoName := strings.TrimSuffix(rr[1], ".git") @@ -133,10 +120,10 @@ func runServ(k *cli.Context) { if err != nil { if err == models.ErrUserNotExist { println("Gogs: given repository owner are not registered") - qlog.Fatalf("Unregistered owner: %s", repoUserName) + log.GitLogger.Fatal("Unregistered owner: %s", repoUserName) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get repository owner(%s): %v", repoUserName, err) + log.GitLogger.Fatal("Fail to get repository owner(%s): %v", repoUserName, err) } // Access check. @@ -145,20 +132,20 @@ func runServ(k *cli.Context) { has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) if err != nil { println("Gogs: internal error:", err) - qlog.Fatal("Fail to check write access:", err) + log.GitLogger.Fatal("Fail to check write access:", err) } else if !has { println("You have no right to write this repository") - qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath) + log.GitLogger.Fatal("User %s has no right to write repository %s", user.Name, repoPath) } case isRead: repo, err := models.GetRepositoryByName(repoUser.Id, repoName) if err != nil { if err == models.ErrRepoNotExist { println("Gogs: given repository does not exist") - qlog.Fatalf("Repository does not exist: %s/%s", repoUser.Name, repoName) + log.GitLogger.Fatal("Repository does not exist: %s/%s", repoUser.Name, repoName) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get repository: %v", err) + log.GitLogger.Fatal("Fail to get repository: %v", err) } if !repo.IsPrivate { @@ -168,10 +155,10 @@ func runServ(k *cli.Context) { has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) if err != nil { println("Gogs: internal error:", err) - qlog.Fatal("Fail to check read access:", err) + log.GitLogger.Fatal("Fail to check read access:", err) } else if !has { println("You have no right to access this repository") - qlog.Fatalf("User %s has no right to read repository %s", user.Name, repoPath) + log.GitLogger.Fatal("User %s has no right to read repository %s", user.Name, repoPath) } default: println("Unknown command") @@ -188,15 +175,6 @@ func runServ(k *cli.Context) { err = gitcmd.Run() if err != nil { println("Gogs: internal error:", err) - qlog.Fatalf("Fail to execute git command: %v", err) + log.GitLogger.Fatal("Fail to execute git command: %v", err) } - - //refName := os.Getenv("refName") - //oldCommitId := os.Getenv("oldCommitId") - //newCommitId := os.Getenv("newCommitId") - - //qlog.Error("get envs:", refName, oldCommitId, newCommitId) - - // update - //models.Update(refName, oldCommitId, newCommitId, repoUserName, repoName, user.Id) } diff --git a/cmd/update.go b/cmd/update.go index b8686a3e6e..d0e0acded9 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -6,14 +6,12 @@ package cmd import ( "os" - "path" "strconv" "github.com/codegangsta/cli" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/modules/log" ) var CmdUpdate = cli.Command{ @@ -24,33 +22,23 @@ var CmdUpdate = cli.Command{ Flags: []cli.Flag{}, } -func updateEnv(refName, oldCommitId, newCommitId string) { - os.Setenv("refName", refName) - os.Setenv("oldCommitId", oldCommitId) - os.Setenv("newCommitId", newCommitId) - qlog.Info("set envs:", refName, oldCommitId, newCommitId) -} - func runUpdate(c *cli.Context) { cmd := os.Getenv("SSH_ORIGINAL_COMMAND") if cmd == "" { return } - setup(path.Join(setting.LogRootPath, "update.log")) + setup("update.log") args := c.Args() if len(args) != 3 { - qlog.Fatal("received less 3 parameters") + log.GitLogger.Fatal("received less 3 parameters") } else if args[0] == "" { - qlog.Fatal("refName is empty, shouldn't use") + log.GitLogger.Fatal("refName is empty, shouldn't use") } - //updateEnv(args[0], args[1], args[2]) - userName := os.Getenv("userName") userId, _ := strconv.ParseInt(os.Getenv("userId"), 10, 64) - //repoId := os.Getenv("repoId") repoUserName := os.Getenv("repoUserName") repoName := os.Getenv("repoName") diff --git a/models/action.go b/models/action.go index e39b04a1ad..2846823386 100644 --- a/models/action.go +++ b/models/action.go @@ -12,7 +12,6 @@ import ( "time" "github.com/gogits/git" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" @@ -115,7 +114,7 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, return errors.New("action.CommitRepoAction(NotifyWatchers): " + err.Error()) } - qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName) + //qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName) // New push event hook. if err := repo.GetOwner(); err != nil { diff --git a/models/publickey.go b/models/publickey.go index 72b45c5b89..b0021a6880 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -19,7 +19,6 @@ import ( "time" "github.com/Unknwon/com" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/process" @@ -55,7 +54,7 @@ func exePath() (string, error) { func homeDir() string { home, err := com.HomeDir() if err != nil { - qlog.Fatalln(err) + log.Fatal("Fail to get home directory: %v", err) } return home } @@ -64,13 +63,13 @@ func init() { var err error if appPath, err = exePath(); err != nil { - qlog.Fatalf("publickey.init(fail to get app path): %v\n", err) + log.Fatal("publickey.init(fail to get app path): %v\n", err) } // Determine and create .ssh path. SshPath = filepath.Join(homeDir(), ".ssh") if err = os.MkdirAll(SshPath, os.ModePerm); err != nil { - qlog.Fatalf("publickey.init(fail to create SshPath(%s)): %v\n", SshPath, err) + log.Fatal("publickey.init(fail to create SshPath(%s)): %v\n", SshPath, err) } } diff --git a/models/update.go b/models/update.go index b7242cde85..5675a94cf4 100644 --- a/models/update.go +++ b/models/update.go @@ -9,18 +9,17 @@ import ( "os/exec" "strings" - qlog "github.com/qiniu/log" - "github.com/gogits/git" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" ) func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) { isNew := strings.HasPrefix(oldCommitId, "0000000") if isNew && strings.HasPrefix(newCommitId, "0000000") { - qlog.Fatal("old rev and new rev both 000000") + log.GitLogger.Fatal("old rev and new rev both 000000") } f := RepoPath(repoUserName, repoName) @@ -31,18 +30,18 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName isDel := strings.HasPrefix(newCommitId, "0000000") if isDel { - qlog.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId) + log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId) return } repo, err := git.OpenRepository(f) if err != nil { - qlog.Fatalf("runUpdate.Open repoId: %v", err) + log.GitLogger.Fatal("runUpdate.Open repoId: %v", err) } newCommit, err := repo.GetCommit(newCommitId) if err != nil { - qlog.Fatalf("runUpdate GetCommit of newCommitId: %v", err) + log.GitLogger.Fatal("runUpdate GetCommit of newCommitId: %v", err) return } @@ -51,28 +50,28 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName if isNew { l, err = newCommit.CommitsBefore() if err != nil { - qlog.Fatalf("Find CommitsBefore erro: %v", err) + log.GitLogger.Fatal("Find CommitsBefore erro: %v", err) } } else { l, err = newCommit.CommitsBeforeUntil(oldCommitId) if err != nil { - qlog.Fatalf("Find CommitsBeforeUntil erro: %v", err) + log.GitLogger.Fatal("Find CommitsBeforeUntil erro: %v", err) return } } if err != nil { - qlog.Fatalf("runUpdate.Commit repoId: %v", err) + log.GitLogger.Fatal("runUpdate.Commit repoId: %v", err) } ru, err := GetUserByName(repoUserName) if err != nil { - qlog.Fatalf("runUpdate.GetUserByName: %v", err) + log.GitLogger.Fatal("runUpdate.GetUserByName: %v", err) } repos, err := GetRepositoryByName(ru.Id, repoName) if err != nil { - qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err) + log.GitLogger.Fatal("runUpdate.GetRepositoryByName userId: %v", err) } commits := make([]*base.PushCommit, 0) @@ -96,6 +95,6 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()}) if err = CommitRepoAction(userId, ru.Id, userName, actEmail, repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil { - qlog.Fatalf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err) + log.GitLogger.Fatal("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err) } } diff --git a/modules/log/log.go b/modules/log/log.go index a62baad16e..24f0442d1e 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -6,13 +6,16 @@ package log import ( + "fmt" "os" + "path" "github.com/gogits/logs" ) var ( - loggers []*logs.BeeLogger + loggers []*logs.BeeLogger + GitLogger *logs.BeeLogger ) func init() { @@ -38,6 +41,12 @@ func NewLogger(bufLen int64, mode, config string) { } } +func NewGitLogger(logPath string) { + os.MkdirAll(path.Dir(logPath), os.ModePerm) + GitLogger = logs.NewLogger(0) + GitLogger.SetLogger("file", fmt.Sprintf(`{"level":0,"filename":"%s","rotate":false}`, logPath)) +} + func Trace(format string, v ...interface{}) { for _, logger := range loggers { logger.Trace(format, v...) diff --git a/routers/install.go b/routers/install.go index eb0fc199cf..c2eee88a5a 100644 --- a/routers/install.go +++ b/routers/install.go @@ -60,6 +60,7 @@ func GlobalInit() { models.HasEngine = true cron.NewCronContext() + log.NewGitLogger(path.Join(setting.LogRootPath, "http.log")) } if models.EnableSQLite3 { log.Info("SQLite3 Enabled") diff --git a/routers/repo/http.go b/routers/repo/http.go index c5856d603c..5915e8761e 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "log" "net/http" "os" "os/exec" @@ -22,6 +21,7 @@ import ( "github.com/go-martini/martini" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" ) @@ -190,7 +190,7 @@ var routes = []route{ // Request handling function func HttpBackend(config *Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - //log.Printf("%s %s %s %s", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) + //log.GitLogger.Printf("%s %s %s %s", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) for _, route := range routes { if m := route.cr.FindStringSubmatch(r.URL.Path); m != nil { if route.method != r.Method { @@ -202,7 +202,7 @@ func HttpBackend(config *Config) http.HandlerFunc { dir, err := getGitDir(config, m[1]) if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) renderNotFound(w) return } @@ -246,19 +246,19 @@ func serviceRpc(rpc string, hr handler) { cmd.Dir = dir in, err := cmd.StdinPipe() if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) return } stdout, err := cmd.StdoutPipe() if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) return } err = cmd.Start() if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) return } @@ -345,7 +345,7 @@ func getGitDir(config *Config, fPath string) (string, error) { cwd, err := os.Getwd() if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) return "", err } @@ -422,7 +422,7 @@ func gitCommand(gitBinPath, dir string, args ...string) []byte { out, err := command.Output() if err != nil { - log.Print(err) + log.GitLogger.Error(err.Error()) } return out