Fix bug related to log

This commit is contained in:
Unknown 2014-04-06 13:00:20 -04:00
parent d3a987eded
commit 2846ff7d31
7 changed files with 46 additions and 57 deletions

View file

@ -12,6 +12,8 @@ github.com/nfnt/resize =
github.com/lunny/xorm = github.com/lunny/xorm =
github.com/go-sql-driver/mysql = github.com/go-sql-driver/mysql =
github.com/lib/pq = github.com/lib/pq =
github.com/qiniu/log =
code.google.com/p/goauth2 =
github.com/gogits/logs = github.com/gogits/logs =
github.com/gogits/binding = github.com/gogits/binding =
github.com/gogits/git = github.com/gogits/git =
@ -19,7 +21,6 @@ github.com/gogits/gfm =
github.com/gogits/cache = github.com/gogits/cache =
github.com/gogits/session = github.com/gogits/session =
github.com/gogits/webdav = github.com/gogits/webdav =
code.google.com/p/goauth2 =
[res] [res]
include = templates|public|conf include = templates|public|conf

View file

@ -13,6 +13,8 @@
"others": [ "others": [
"modules", "modules",
"$GOPATH/src/github.com/gogits/binding", "$GOPATH/src/github.com/gogits/binding",
"$GOPATH/src/github.com/gogits/webdav",
"$GOPATH/src/github.com/gogits/logs",
"$GOPATH/src/github.com/gogits/git", "$GOPATH/src/github.com/gogits/git",
"$GOPATH/src/github.com/gogits/gfm" "$GOPATH/src/github.com/gogits/gfm"
] ]

View file

@ -14,6 +14,7 @@ import (
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/Unknwon/goconfig" "github.com/Unknwon/goconfig"
qlog "github.com/qiniu/log"
"github.com/gogits/cache" "github.com/gogits/cache"
"github.com/gogits/session" "github.com/gogits/session"
@ -105,16 +106,14 @@ func newLogService() {
LogMode = Cfg.MustValue("log", "MODE", "console") LogMode = Cfg.MustValue("log", "MODE", "console")
modeSec := "log." + LogMode modeSec := "log." + LogMode
if _, err := Cfg.GetSection(modeSec); err != nil { if _, err := Cfg.GetSection(modeSec); err != nil {
fmt.Printf("Unknown log mode: %s\n", LogMode) qlog.Fatalf("Unknown log mode: %s\n", LogMode)
os.Exit(2)
} }
// Log level. // Log level.
levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace") levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace")
level, ok := logLevels[levelName] level, ok := logLevels[levelName]
if !ok { if !ok {
fmt.Printf("Unknown log level: %s\n", levelName) qlog.Fatalf("Unknown log level: %s\n", levelName)
os.Exit(2)
} }
// Generate log configuration. // Generate log configuration.
@ -164,16 +163,14 @@ func newCacheService() {
case "redis", "memcache": case "redis", "memcache":
CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST")) CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST"))
default: default:
fmt.Printf("Unknown cache adapter: %s\n", CacheAdapter) qlog.Fatalf("Unknown cache adapter: %s\n", CacheAdapter)
os.Exit(2)
} }
var err error var err error
Cache, err = cache.NewCache(CacheAdapter, CacheConfig) Cache, err = cache.NewCache(CacheAdapter, CacheConfig)
if err != nil { if err != nil {
fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n", qlog.Fatalf("Init cache system failed, adapter: %s, config: %s, %v\n",
CacheAdapter, CacheConfig, err) CacheAdapter, CacheConfig, err)
os.Exit(2)
} }
log.Info("Cache Service Enabled") log.Info("Cache Service Enabled")
@ -199,9 +196,8 @@ func newSessionService() {
var err error var err error
SessionManager, err = session.NewManager(SessionProvider, *SessionConfig) SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
if err != nil { if err != nil {
fmt.Printf("Init session system failed, provider: %s, %v\n", qlog.Fatalf("Init session system failed, provider: %s, %v\n",
SessionProvider, err) SessionProvider, err)
os.Exit(2)
} }
log.Info("Session Service Enabled") log.Info("Session Service Enabled")
@ -246,23 +242,20 @@ func NewConfigContext() {
//var err error //var err error
workDir, err := ExecDir() workDir, err := ExecDir()
if err != nil { if err != nil {
fmt.Printf("Fail to get work directory: %s\n", err) qlog.Fatalf("Fail to get work directory: %s\n", err)
os.Exit(2)
} }
cfgPath := filepath.Join(workDir, "conf/app.ini") cfgPath := filepath.Join(workDir, "conf/app.ini")
Cfg, err = goconfig.LoadConfigFile(cfgPath) Cfg, err = goconfig.LoadConfigFile(cfgPath)
if err != nil { if err != nil {
fmt.Printf("Cannot load config file(%s): %v\n", cfgPath, err) qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err)
os.Exit(2)
} }
Cfg.BlockMode = false Cfg.BlockMode = false
cfgPath = filepath.Join(workDir, "custom/conf/app.ini") cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
if com.IsFile(cfgPath) { if com.IsFile(cfgPath) {
if err = Cfg.AppendFiles(cfgPath); err != nil { if err = Cfg.AppendFiles(cfgPath); err != nil {
fmt.Printf("Cannot load config file(%s): %v\n", cfgPath, err) qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err)
os.Exit(2)
} }
} }
@ -281,8 +274,7 @@ func NewConfigContext() {
} }
// Does not check run user when the install lock is off. // Does not check run user when the install lock is off.
if InstallLock && RunUser != curUser { if InstallLock && RunUser != curUser {
fmt.Printf("Expect user(%s) but current user is: %s\n", RunUser, curUser) qlog.Fatalf("Expect user(%s) but current user is: %s\n", RunUser, curUser)
os.Exit(2)
} }
LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS") LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS")
@ -294,14 +286,14 @@ func NewConfigContext() {
// Determine and create root git reposiroty path. // Determine and create root git reposiroty path.
homeDir, err := com.HomeDir() homeDir, err := com.HomeDir()
if err != nil { if err != nil {
fmt.Printf("Fail to get home directory): %v\n", err) qlog.Fatalf("Fail to get home directory): %v\n", err)
os.Exit(2)
} }
RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "git/gogs-repositories")) RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "git/gogs-repositories"))
if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
fmt.Printf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err) qlog.Fatalf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err)
os.Exit(2)
} }
log.Info("%s %s", AppName, AppVer)
} }
func NewServices() { func NewServices() {

View file

@ -21,8 +21,6 @@ func init() {
func NewLogger(bufLen int64, mode, config string) { func NewLogger(bufLen int64, mode, config string) {
Mode, Config = mode, config Mode, Config = mode, config
logger = logs.NewLogger(bufLen) logger = logs.NewLogger(bufLen)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(4)
logger.SetLogger(mode, config) logger.SetLogger(mode, config)
} }

View file

@ -6,13 +6,13 @@ package routers
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"strings" "strings"
"github.com/Unknwon/goconfig" "github.com/Unknwon/goconfig"
"github.com/go-martini/martini" "github.com/go-martini/martini"
"github.com/lunny/xorm" "github.com/lunny/xorm"
qlog "github.com/qiniu/log"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
@ -43,8 +43,7 @@ func GlobalInit() {
if base.InstallLock { if base.InstallLock {
if err := models.NewEngine(); err != nil { if err := models.NewEngine(); err != nil {
fmt.Println(err) qlog.Fatal(err)
os.Exit(2)
} }
models.HasEngine = true models.HasEngine = true

View file

@ -6,7 +6,6 @@ package main
import ( import (
"container/list" "container/list"
"fmt"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -14,11 +13,11 @@ import (
"strings" "strings"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
qlog "github.com/qiniu/log"
"github.com/gogits/git" "github.com/gogits/git"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
//"github.com/qiniu/log"
) )
var CmdUpdate = cli.Command{ var CmdUpdate = cli.Command{
@ -31,11 +30,15 @@ gogs serv provide access auth for repositories`,
} }
func newUpdateLogger(execDir string) { func newUpdateLogger(execDir string) {
level := "0"
logPath := execDir + "/log/update.log" logPath := execDir + "/log/update.log"
os.MkdirAll(path.Dir(logPath), os.ModePerm) os.MkdirAll(path.Dir(logPath), os.ModePerm)
log.NewLogger(0, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, logPath)) f, err := os.Open(logPath)
log.Trace("start logging...") if err != nil {
qlog.Fatal(err)
}
qlog.SetOutput(f)
qlog.Info("Start logging update...")
} }
// for command: ./gogs update // for command: ./gogs update
@ -54,14 +57,12 @@ func runUpdate(c *cli.Context) {
args := c.Args() args := c.Args()
if len(args) != 3 { if len(args) != 3 {
log.Error("received less 3 parameters") qlog.Fatal("received less 3 parameters")
return
} }
refName := args[0] refName := args[0]
if refName == "" { if refName == "" {
log.Error("refName is empty, shouldn't use") qlog.Fatal("refName is empty, shouldn't use")
return
} }
oldCommitId := args[1] oldCommitId := args[1]
newCommitId := args[2] newCommitId := args[2]
@ -69,8 +70,7 @@ func runUpdate(c *cli.Context) {
isNew := strings.HasPrefix(oldCommitId, "0000000") isNew := strings.HasPrefix(oldCommitId, "0000000")
if isNew && if isNew &&
strings.HasPrefix(newCommitId, "0000000") { strings.HasPrefix(newCommitId, "0000000") {
log.Error("old rev and new rev both 000000") qlog.Fatal("old rev and new rev both 000000")
return
} }
userName := os.Getenv("userName") userName := os.Getenv("userName")
@ -86,19 +86,18 @@ func runUpdate(c *cli.Context) {
repo, err := git.OpenRepository(f) repo, err := git.OpenRepository(f)
if err != nil { if err != nil {
log.Error("runUpdate.Open repoId: %v", err) qlog.Fatalf("runUpdate.Open repoId: %v", err)
return
} }
newOid, err := git.NewOidFromString(newCommitId) newOid, err := git.NewOidFromString(newCommitId)
if err != nil { if err != nil {
log.Error("runUpdate.Ref repoId: %v", err) qlog.Fatalf("runUpdate.Ref repoId: %v", err)
return return
} }
newCommit, err := repo.LookupCommit(newOid) newCommit, err := repo.LookupCommit(newOid)
if err != nil { if err != nil {
log.Error("runUpdate.Ref repoId: %v", err) qlog.Fatalf("runUpdate.Ref repoId: %v", err)
return return
} }
@ -107,38 +106,38 @@ func runUpdate(c *cli.Context) {
if isNew { if isNew {
l, err = repo.CommitsBefore(newCommit.Id()) l, err = repo.CommitsBefore(newCommit.Id())
if err != nil { if err != nil {
log.Error("Find CommitsBefore erro:", err) qlog.Fatalf("Find CommitsBefore erro:", err)
return return
} }
} else { } else {
oldOid, err := git.NewOidFromString(oldCommitId) oldOid, err := git.NewOidFromString(oldCommitId)
if err != nil { if err != nil {
log.Error("runUpdate.Ref repoId: %v", err) qlog.Fatalf("runUpdate.Ref repoId: %v", err)
return return
} }
oldCommit, err := repo.LookupCommit(oldOid) oldCommit, err := repo.LookupCommit(oldOid)
if err != nil { if err != nil {
log.Error("runUpdate.Ref repoId: %v", err) qlog.Fatalf("runUpdate.Ref repoId: %v", err)
return return
} }
l = repo.CommitsBetween(newCommit, oldCommit) l = repo.CommitsBetween(newCommit, oldCommit)
} }
if err != nil { if err != nil {
log.Error("runUpdate.Commit repoId: %v", err) qlog.Fatalf("runUpdate.Commit repoId: %v", err)
return return
} }
sUserId, err := strconv.Atoi(userId) sUserId, err := strconv.Atoi(userId)
if err != nil { if err != nil {
log.Error("runUpdate.Parse userId: %v", err) qlog.Fatalf("runUpdate.Parse userId: %v", err)
return return
} }
repos, err := models.GetRepositoryByName(int64(sUserId), repoName) repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
if err != nil { if err != nil {
log.Error("runUpdate.GetRepositoryByName userId: %v", err) qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err)
return return
} }
@ -163,6 +162,6 @@ func runUpdate(c *cli.Context) {
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()}) //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
if err = models.CommitRepoAction(int64(sUserId), userName, actEmail, if err = models.CommitRepoAction(int64(sUserId), userName, actEmail,
repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil { repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
log.Error("runUpdate.models.CommitRepoAction: %v", err) qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
} }
} }

8
web.go
View file

@ -11,6 +11,7 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/go-martini/martini" "github.com/go-martini/martini"
qlog "github.com/qiniu/log"
"github.com/gogits/binding" "github.com/gogits/binding"
@ -50,9 +51,7 @@ func newMartini() *martini.ClassicMartini {
} }
func runWeb(*cli.Context) { func runWeb(*cli.Context) {
fmt.Println("Server is running...")
routers.GlobalInit() routers.GlobalInit()
log.Info("%s %s", base.AppName, base.AppVer)
m := newMartini() m := newMartini()
@ -177,14 +176,13 @@ func runWeb(*cli.Context) {
if protocol == "http" { if protocol == "http" {
log.Info("Listen: http://%s", listenAddr) log.Info("Listen: http://%s", listenAddr)
if err := http.ListenAndServe(listenAddr, m); err != nil { if err := http.ListenAndServe(listenAddr, m); err != nil {
fmt.Println(err.Error()) qlog.Error(err.Error())
//log.Critical(err.Error()) // not working now
} }
} else if protocol == "https" { } else if protocol == "https" {
log.Info("Listen: https://%s", listenAddr) log.Info("Listen: https://%s", listenAddr)
if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"), if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil { base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
fmt.Println(err.Error()) qlog.Error(err.Error())
} }
} }
} }