Skip to content

Commit

Permalink
adjustment log (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
veezhang authored Nov 11, 2021
1 parent 662da6b commit cce0022
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 30 deletions.
7 changes: 4 additions & 3 deletions common/runtime.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package common

import (
"log"
"net/http"
"runtime"

"github.com/astaxie/beego/logs"
)

func LogPanic(r interface{}) {
Expand All @@ -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)
}
}
1 change: 1 addition & 0 deletions conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ runmode = dev
autorender = false
copyrequestbody = true
logspath = "./logs/"
loglevel = "info"
uploadspath = "./uploads/"
sqlitedbfilepath = "./tasks.db"
3 changes: 2 additions & 1 deletion controllers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
Expand Down
44 changes: 33 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"syscall"
"time"

"github.com/astaxie/beego/logs"
_ "github.com/vesoft-inc/nebula-http-gateway/routers"

"github.com/astaxie/beego"
Expand All @@ -29,24 +30,45 @@ func main() {
/*
logger config
*/
logsPath := beego.AppConfig.String("logspath")
logsPath := beego.AppConfig.DefaultString("logspath", "./logs/")
absLogsPath, _ := filepath.Abs(logsPath)
_, err := common.CreateFileWithPerm(absLogsPath+"/", "0720")

if err != nil && os.IsNotExist(err) {
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
Expand All @@ -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)
Expand All @@ -85,5 +107,5 @@ func main() {
beego.Run()

<-done
beego.Info("server closed")
logs.Info("server closed")
}
4 changes: 2 additions & 2 deletions service/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 7 additions & 6 deletions service/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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{}

Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions service/importer/taskmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
10 changes: 5 additions & 5 deletions service/logger/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit cce0022

Please sign in to comment.