diff --git a/common/runtime.go b/common/runtime.go index fe9436b..18f3ea0 100644 --- a/common/runtime.go +++ b/common/runtime.go @@ -1,9 +1,10 @@ package common import ( - "log" "net/http" "runtime" + + "github.com/astaxie/beego/logs" ) func LogPanic(r interface{}) { @@ -17,8 +18,8 @@ func LogPanic(r interface{}) { stacktrace := make([]byte, size) stacktrace = stacktrace[:runtime.Stack(stacktrace, false)] if _, ok := r.(string); ok { - log.Printf("Observed a panic: %s\n%s", r, stacktrace) + logs.Warn("Observed a panic: %s\n%s", r, stacktrace) } else { - log.Printf("Observed a panic: %#v (%v)\n%s", r, r, stacktrace) + logs.Warn("Observed a panic: %#v (%v)\n%s", r, r, stacktrace) } } diff --git a/conf/app.conf b/conf/app.conf index 7b75e43..4913e20 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -5,5 +5,6 @@ runmode = dev autorender = false copyrequestbody = true logspath = "./logs/" +loglevel = "info" uploadspath = "./uploads/" sqlitedbfilepath = "./tasks.db" diff --git a/controllers/task.go b/controllers/task.go index 2f87230..07fd050 100644 --- a/controllers/task.go +++ b/controllers/task.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/astaxie/beego" + "github.com/astaxie/beego/logs" "github.com/vesoft-inc/nebula-http-gateway/service/importer" "github.com/vesoft-inc/nebula-importer/pkg/config" @@ -47,7 +48,7 @@ func (this *TaskController) Import() { if err != nil { // task err: import task not start err handle task.TaskStatus = importer.StatusAborted.String() - beego.Error(fmt.Sprintf("Failed to start a import task: `%s`, task result: `%v`", taskID, err)) + logs.Error(fmt.Sprintf("Failed to start a import task: `%s`, task result: `%v`", taskID, err)) res.Code = -1 res.Message = err.Error() diff --git a/main.go b/main.go index 4ac4901..02a0e37 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "syscall" "time" + "github.com/astaxie/beego/logs" _ "github.com/vesoft-inc/nebula-http-gateway/routers" "github.com/astaxie/beego" @@ -29,7 +30,7 @@ func main() { /* logger config */ - logsPath := beego.AppConfig.String("logspath") + logsPath := beego.AppConfig.DefaultString("logspath", "./logs/") absLogsPath, _ := filepath.Abs(logsPath) _, err := common.CreateFileWithPerm(absLogsPath+"/", "0720") @@ -37,16 +38,37 @@ func main() { log.Fatalf("create file %s with error: %s", absLogsPath, err.Error()) } + logFileName := beego.AppConfig.DefaultString("appname", "nebula-http-gateway") + logFileName += ".log" + logFilePath := filepath.Join( absLogsPath, - "test.log", + logFileName, ) - beego.SetLogger("file", fmt.Sprintf(`{"filename":"%s","MaxSize":104857600,"perm":"0620"}`, logFilePath)) - beego.BeeLogger.DelLogger("console") - beego.SetLogFuncCall(true) - beego.BeeLogger.SetLogFuncCallDepth(3) - // beego.SetLevel(beego.LevelInformational) - beego.SetLevel(beego.LevelDebug) + + logLevelString := beego.AppConfig.String("logLevel") + logLevel := logs.LevelWarning + switch logLevelString { + case "error": + logLevel = logs.LevelError + case "warning", "warn": + logLevel = logs.LevelWarning + case "notice": + logLevel = logs.LevelNotice + case "informational", "info": + logLevel = logs.LevelInformational + case "debug": + logLevel = logs.LevelDebug + } + + logs.SetLogger("file", fmt.Sprintf(`{"filename":"%s","MaxSize":104857600,"perm":"0620"}`, logFilePath)) + logs.GetBeeLogger().DelLogger("console") + logs.SetLogFuncCall(true) + logs.SetLogFuncCallDepth(3) + logs.SetLevel(logLevel) + defer func(){ + logs.GetBeeLogger().Flush() + }() /* importer file uploads config @@ -69,14 +91,14 @@ func main() { go func() { <-quit - beego.Info("server is shutting down") + logs.Info("server is shutting down") ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() beego.BeeApp.Server.SetKeepAlivesEnabled(false) if err := beego.BeeApp.Server.Shutdown(ctx); err != nil { - beego.Error(err.Error()) + logs.Error(err.Error()) } close(done) @@ -85,5 +107,5 @@ func main() { beego.Run() <-done - beego.Info("server closed") + logs.Info("server closed") } diff --git a/service/dao/dao.go b/service/dao/dao.go index 949462d..6a7e7f4 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -2,8 +2,8 @@ package dao import ( "errors" - "log" + "github.com/astaxie/beego/logs" "github.com/vesoft-inc/nebula-http-gateway/common" "github.com/vesoft-inc/nebula-http-gateway/service/pool" @@ -328,7 +328,7 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) { } if !resp.IsSucceed() { - log.Printf("ErrorCode: %v, ErrorMsg: %s", resp.GetErrorCode(), resp.GetErrorMsg()) + logs.Info("ErrorCode: %v, ErrorMsg: %s", resp.GetErrorCode(), resp.GetErrorMsg()) return result, errors.New(string(resp.GetErrorMsg())) } if !resp.IsEmpty() { diff --git a/service/importer/importer.go b/service/importer/importer.go index 2003f50..29adeb0 100644 --- a/service/importer/importer.go +++ b/service/importer/importer.go @@ -7,6 +7,7 @@ import ( "time" "github.com/astaxie/beego" + "github.com/astaxie/beego/logs" "github.com/vesoft-inc/nebula-importer/pkg/config" importerErrors "github.com/vesoft-inc/nebula-importer/pkg/errors" ) @@ -28,7 +29,7 @@ type ActionResult struct { func Import(taskID string, configPath string, configBody *config.YAMLConfig) (err error) { - beego.Debug(fmt.Sprintf("Start a import task: `%s`", taskID)) + logs.Debug(fmt.Sprintf("Start a import task: `%s`", taskID)) var conf *config.YAMLConfig @@ -41,7 +42,7 @@ func Import(taskID string, configPath string, configBody *config.YAMLConfig) (er ) if err != nil { - beego.Error(err.(importerErrors.ImporterError)) + logs.Error(err.(importerErrors.ImporterError)) return err.(importerErrors.ImporterError) } } else { @@ -73,21 +74,21 @@ func Import(taskID string, configPath string, configBody *config.YAMLConfig) (er result.ErrorResult.ErrorCode = err.ErrCode result.ErrorResult.ErrorMsg = err.ErrMsg.Error() - beego.Error(fmt.Sprintf("Failed to finish a import task: `%s`, task result: `%v`", taskID, result)) + logs.Error(fmt.Sprintf("Failed to finish a import task: `%s`, task result: `%v`", taskID, result)) } else { task.TaskStatus = StatusFinished.String() result.FailedRows = task.GetRunner().NumFailed GetTaskMgr().DelTask(taskID) - beego.Debug(fmt.Sprintf("Success to finish a import task: `%s`, task result: `%v`", taskID, result)) + logs.Debug(fmt.Sprintf("Success to finish a import task: `%s`, task result: `%v`", taskID, result)) } }() return nil } func ImportAction(taskID string, taskAction TaskAction) (result ActionResult, err error) { - beego.Debug(fmt.Sprintf("Start a import task action: `%s` for task: `%s`", taskAction.String(), taskID)) + logs.Debug(fmt.Sprintf("Start a import task action: `%s` for task: `%s`", taskAction.String(), taskID)) result = ActionResult{} @@ -104,7 +105,7 @@ func ImportAction(taskID string, taskAction TaskAction) (result ActionResult, er err = errors.New("unknown task action") } - beego.Debug(fmt.Sprintf("The import task action: `%s` for task: `%s` finished, action result: `%v`", taskAction.String(), taskID, result)) + logs.Debug(fmt.Sprintf("The import task action: `%s` for task: `%s` finished, action result: `%v`", taskAction.String(), taskID, result)) return result, err } diff --git a/service/importer/taskmgr.go b/service/importer/taskmgr.go index 74a112e..ca5ceda 100644 --- a/service/importer/taskmgr.go +++ b/service/importer/taskmgr.go @@ -9,6 +9,7 @@ import ( "sync" "github.com/astaxie/beego" + "github.com/astaxie/beego/logs" _ "github.com/mattn/go-sqlite3" "github.com/vesoft-inc/nebula-importer/pkg/cmd" ) @@ -154,7 +155,7 @@ func initDB() { _db, err := sql.Open("sqlite3", dbFilePath) if err != nil { - beego.Emergency(err.Error()) + logs.Emergency(err.Error()) log.Panic(err) } @@ -167,7 +168,7 @@ func initDB() { _, err = GetTaskMgr().db.Exec(sqlStmt) if err != nil { - beego.Emergency(fmt.Sprintf("%q: %s\n", err, sqlStmt)) + logs.Emergency(fmt.Sprintf("%q: %s\n", err, sqlStmt)) log.Panicf("%q: %s\n", err, sqlStmt) } diff --git a/service/logger/adapter.go b/service/logger/adapter.go index fd6767b..8bc1dd6 100644 --- a/service/logger/adapter.go +++ b/service/logger/adapter.go @@ -3,23 +3,23 @@ package logger import ( "fmt" - "github.com/astaxie/beego" + "github.com/astaxie/beego/logs" ) type HttpGatewayLogger struct{} func (l HttpGatewayLogger) Info(msg string) { - beego.Info(fmt.Sprintf("[nebula-clients] %s", msg)) + logs.Info(fmt.Sprintf("[nebula-clients] %s", msg)) } func (l HttpGatewayLogger) Warn(msg string) { - beego.Warn(fmt.Sprintf("[nebula-clients] %s", msg)) + logs.Warn(fmt.Sprintf("[nebula-clients] %s", msg)) } func (l HttpGatewayLogger) Error(msg string) { - beego.Error(fmt.Sprintf("[nebula-clients] %s", msg)) + logs.Error(fmt.Sprintf("[nebula-clients] %s", msg)) } func (l HttpGatewayLogger) Fatal(msg string) { - beego.Emergency(fmt.Sprintf("[nebula-clients] %s", msg)) + logs.Emergency(fmt.Sprintf("[nebula-clients] %s", msg)) }