Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/125 Reviewed-by: avadesian <xuchx@pcl.ac.cn>tags/v1.21.12.1
@@ -67,19 +67,43 @@ func (l *Logger) Log(skip int, level Level, format string, v ...interface{}) err | |||||
caller = fn.Name() + "()" | caller = fn.Name() + "()" | ||||
} | } | ||||
} | } | ||||
msg := format | |||||
if len(v) > 0 { | |||||
msg = ColorSprintf(format, v...) | |||||
} | |||||
stack := "" | stack := "" | ||||
if l.GetStacktraceLevel() <= level { | if l.GetStacktraceLevel() <= level { | ||||
stack = Stack(skip + 1) | stack = Stack(skip + 1) | ||||
} | } | ||||
return l.SendLog(level, caller, strings.TrimPrefix(filename, prefix), line, msg, stack) | |||||
msg := format | |||||
if len(v) > 0 { | |||||
switch v[len(v)-1].(type) { | |||||
case string: | |||||
if !strings.Contains(v[len(v)-1].(string), "-") { | |||||
//has no msgID | |||||
msg = ColorSprintf(format, v...) | |||||
return l.SendLog(level, caller, strings.TrimPrefix(filename, prefix), line, msg, "", stack) | |||||
} else { | |||||
if len(v) > 1 { | |||||
args := make([]interface{}, len(v)-1) | |||||
for i := 0; i < len(v)-1; i++ { | |||||
args[i] = v[i] | |||||
} | |||||
msg = ColorSprintf(format, args...) | |||||
} | |||||
return l.SendLog(level, caller, strings.TrimPrefix(filename, prefix), line, msg, v[len(v)-1].(string), stack) | |||||
} | |||||
default: | |||||
//has no msgID | |||||
msg = ColorSprintf(format, v...) | |||||
return l.SendLog(level, caller, strings.TrimPrefix(filename, prefix), line, msg, "", stack) | |||||
} | |||||
} else { | |||||
//has no msgID | |||||
return l.SendLog(level, caller, strings.TrimPrefix(filename, prefix), line, msg, "", stack) | |||||
} | |||||
} | } | ||||
// SendLog sends a log event at the provided level with the information given | // SendLog sends a log event at the provided level with the information given | ||||
func (l *Logger) SendLog(level Level, caller, filename string, line int, msg string, stack string) error { | |||||
func (l *Logger) SendLog(level Level, caller, filename string, line int, msg, msgID, stack string) error { | |||||
if l.GetLevel() > level { | if l.GetLevel() > level { | ||||
return nil | return nil | ||||
} | } | ||||
@@ -88,7 +112,7 @@ func (l *Logger) SendLog(level Level, caller, filename string, line int, msg str | |||||
caller: caller, | caller: caller, | ||||
filename: filename, | filename: filename, | ||||
line: line, | line: line, | ||||
msg: msg, | |||||
msg: msg + "[" + msgID + "]", | |||||
time: time.Now(), | time: time.Now(), | ||||
stacktrace: stack, | stacktrace: stack, | ||||
} | } | ||||
@@ -28,14 +28,14 @@ const ( | |||||
func BlockChainIndex(ctx *context.Context) { | func BlockChainIndex(ctx *context.Context) { | ||||
repo := ctx.Repo.Repository | repo := ctx.Repo.Repository | ||||
if repo.ContractAddress == "" || ctx.User.PublicKey == "" { | if repo.ContractAddress == "" || ctx.User.PublicKey == "" { | ||||
log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", repo.RepoID, ctx.User.ID) | |||||
log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", repo.RepoID, ctx.User.ID, ctx.Data["msgID"]) | |||||
ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) | ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) | ||||
return | return | ||||
} | } | ||||
res, err := blockchain.GetBalance(repo.ContractAddress, ctx.User.PublicKey) | res, err := blockchain.GetBalance(repo.ContractAddress, ctx.User.PublicKey) | ||||
if err != nil { | if err != nil { | ||||
log.Error("GetBalance(%s) failed:%v", ctx.User.PublicKey, err) | |||||
log.Error("GetBalance(%s) failed:%s", ctx.User.PublicKey, err, ctx.Data["msgID"]) | |||||
ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) | ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) | ||||
return | return | ||||
} | } | ||||
@@ -52,7 +52,7 @@ func HandleBlockChainInitNotify(ctx *context.Context) { | |||||
repo, err := models.GetRepositoryByID(req.RepoId) | repo, err := models.GetRepositoryByID(req.RepoId) | ||||
if err != nil { | if err != nil { | ||||
log.Error("GetRepositoryByID failed:", err.Error()) | |||||
log.Error("GetRepositoryByID failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "internal error", | "message": "internal error", | ||||
@@ -61,7 +61,7 @@ func HandleBlockChainInitNotify(ctx *context.Context) { | |||||
} | } | ||||
if repo.BlockChainStatus == models.RepoBlockChainSuccess && len(repo.ContractAddress) != 0 { | if repo.BlockChainStatus == models.RepoBlockChainSuccess && len(repo.ContractAddress) != 0 { | ||||
log.Error("the repo has been RepoBlockChainSuccess:", req.RepoId) | |||||
log.Error("the repo has been RepoBlockChainSuccess:%d", req.RepoId, ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "the repo has been RepoBlockChainSuccess", | "message": "the repo has been RepoBlockChainSuccess", | ||||
@@ -73,7 +73,7 @@ func HandleBlockChainInitNotify(ctx *context.Context) { | |||||
repo.ContractAddress = req.ContractAddress | repo.ContractAddress = req.ContractAddress | ||||
if err = models.UpdateRepositoryCols(repo, "block_chain_status", "contract_address"); err != nil { | if err = models.UpdateRepositoryCols(repo, "block_chain_status", "contract_address"); err != nil { | ||||
log.Error("UpdateRepositoryCols failed:", err.Error()) | |||||
log.Error("UpdateRepositoryCols failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "internal error", | "message": "internal error", | ||||
@@ -91,7 +91,7 @@ func HandleBlockChainCommitNotify(ctx *context.Context) { | |||||
var req BlockChainCommitNotify | var req BlockChainCommitNotify | ||||
data, _ := ctx.Req.Body().Bytes() | data, _ := ctx.Req.Body().Bytes() | ||||
if err := json.Unmarshal(data, &req); err != nil { | if err := json.Unmarshal(data, &req); err != nil { | ||||
log.Error("json.Unmarshal failed:", err.Error()) | |||||
log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "response data error", | "message": "response data error", | ||||
@@ -101,7 +101,7 @@ func HandleBlockChainCommitNotify(ctx *context.Context) { | |||||
blockChain, err := models.GetBlockChainByCommitID(req.CommitID) | blockChain, err := models.GetBlockChainByCommitID(req.CommitID) | ||||
if err != nil { | if err != nil { | ||||
log.Error("GetRepositoryByID failed:", err.Error()) | |||||
log.Error("GetRepositoryByID failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "internal error", | "message": "internal error", | ||||
@@ -110,7 +110,7 @@ func HandleBlockChainCommitNotify(ctx *context.Context) { | |||||
} | } | ||||
if blockChain.Status == models.BlockChainCommitSuccess { | if blockChain.Status == models.BlockChainCommitSuccess { | ||||
log.Error("the commit has been BlockChainCommitReady:", blockChain.RepoID) | |||||
log.Error("the commit has been BlockChainCommitReady:%s", blockChain.RepoID, ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "the commit has been BlockChainCommitReady", | "message": "the commit has been BlockChainCommitReady", | ||||
@@ -122,7 +122,7 @@ func HandleBlockChainCommitNotify(ctx *context.Context) { | |||||
blockChain.TransactionHash = req.TransactionHash | blockChain.TransactionHash = req.TransactionHash | ||||
if err = models.UpdateBlockChainCols(blockChain, "status", "transaction_hash"); err != nil { | if err = models.UpdateBlockChainCols(blockChain, "status", "transaction_hash"); err != nil { | ||||
log.Error("UpdateBlockChainCols failed:", err.Error()) | |||||
log.Error("UpdateBlockChainCols failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"code": "-1", | "code": "-1", | ||||
"message": "internal error", | "message": "internal error", | ||||
@@ -96,7 +96,7 @@ func CloudBrainNew(ctx *context.Context) { | |||||
result, err := cloudbrain.GetImages() | result, err := cloudbrain.GetImages() | ||||
if err != nil { | if err != nil { | ||||
ctx.Data["error"] = err.Error() | ctx.Data["error"] = err.Error() | ||||
log.Error("cloudbrain.GetImages failed:", err.Error()) | |||||
log.Error("cloudbrain.GetImages failed:", err.Error(), ctx.Data["msgID"]) | |||||
} | } | ||||
for i, payload := range result.Payload.ImageInfo { | for i, payload := range result.Payload.ImageInfo { | ||||
@@ -112,7 +112,7 @@ func CloudBrainNew(ctx *context.Context) { | |||||
resultPublic, err := cloudbrain.GetPublicImages() | resultPublic, err := cloudbrain.GetPublicImages() | ||||
if err != nil { | if err != nil { | ||||
ctx.Data["error"] = err.Error() | ctx.Data["error"] = err.Error() | ||||
log.Error("cloudbrain.GetPublicImages failed:", err.Error()) | |||||
log.Error("cloudbrain.GetPublicImages failed:", err.Error(), ctx.Data["msgID"]) | |||||
} | } | ||||
for i, payload := range resultPublic.Payload.ImageInfo { | for i, payload := range resultPublic.Payload.ImageInfo { | ||||
@@ -164,7 +164,7 @@ func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) { | |||||
codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath | codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath | ||||
if jobType != string(models.JobTypeBenchmark) && jobType != string(models.JobTypeDebug) && jobType != string(models.JobTypeSnn4imagenet) { | if jobType != string(models.JobTypeBenchmark) && jobType != string(models.JobTypeDebug) && jobType != string(models.JobTypeSnn4imagenet) { | ||||
log.Error("jobtype error:", jobType) | |||||
log.Error("jobtype error:", jobType, ctx.Data["msgID"]) | |||||
ctx.RenderWithErr("jobtype error", tplCloudBrainNew, &form) | ctx.RenderWithErr("jobtype error", tplCloudBrainNew, &form) | ||||
return | return | ||||
} | } | ||||
@@ -267,7 +267,7 @@ func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrain | |||||
ImageTag: form.Tag, | ImageTag: form.Tag, | ||||
}) | }) | ||||
if err != nil { | if err != nil { | ||||
log.Error("CommitImage(%s) failed:", task.JobName, err.Error()) | |||||
log.Error("CommitImage(%s) failed:%v", task.JobName, err.Error(), ctx.Data["msgID"]) | |||||
ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
"result_code": "-1", | "result_code": "-1", | ||||
"error_msg": "CommitImage failed", | "error_msg": "CommitImage failed", | ||||
@@ -290,14 +290,14 @@ func CloudBrainStop(ctx *context.Context) { | |||||
} | } | ||||
if task.Status == string(models.JobStopped) { | if task.Status == string(models.JobStopped) { | ||||
log.Error("the job(%s) has been stopped", task.JobName) | |||||
log.Error("the job(%s) has been stopped", task.JobName, ctx.Data["msgID"]) | |||||
ctx.ServerError("the job has been stopped", errors.New("the job has been stopped")) | ctx.ServerError("the job has been stopped", errors.New("the job has been stopped")) | ||||
return | return | ||||
} | } | ||||
err = cloudbrain.StopJob(jobID) | err = cloudbrain.StopJob(jobID) | ||||
if err != nil { | if err != nil { | ||||
log.Error("StopJob(%s) failed:%v", task.JobName, err.Error()) | |||||
log.Error("StopJob(%s) failed:%v", task.JobName, err.Error(), ctx.Data["msgID"]) | |||||
ctx.ServerError("StopJob failed", err) | ctx.ServerError("StopJob failed", err) | ||||
return | return | ||||
} | } | ||||
@@ -321,7 +321,7 @@ func CloudBrainDel(ctx *context.Context) { | |||||
} | } | ||||
if task.Status != string(models.JobStopped) { | if task.Status != string(models.JobStopped) { | ||||
log.Error("the job(%s) has not been stopped", task.JobName) | |||||
log.Error("the job(%s) has not been stopped", task.JobName, ctx.Data["msgID"]) | |||||
ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped")) | ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped")) | ||||
return | return | ||||
} | } | ||||
@@ -343,7 +343,7 @@ func CloudBrainShowModels(ctx *context.Context) { | |||||
dirArray := strings.Split(parentDir, "/") | dirArray := strings.Split(parentDir, "/") | ||||
task, err := models.GetCloudbrainByJobID(jobID) | task, err := models.GetCloudbrainByJobID(jobID) | ||||
if err != nil { | if err != nil { | ||||
log.Error("no such job!") | |||||
log.Error("no such job!", ctx.Data["msgID"]) | |||||
ctx.ServerError("no such job:", err) | ctx.ServerError("no such job:", err) | ||||
return | return | ||||
} | } | ||||
@@ -351,7 +351,7 @@ func CloudBrainShowModels(ctx *context.Context) { | |||||
//get dirs | //get dirs | ||||
dirs, err := getModelDirs(task.JobName, parentDir) | dirs, err := getModelDirs(task.JobName, parentDir) | ||||
if err != nil { | if err != nil { | ||||
log.Error("getModelDirs failed:", err.Error()) | |||||
log.Error("getModelDirs failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.ServerError("getModelDirs failed:", err) | ctx.ServerError("getModelDirs failed:", err) | ||||
return | return | ||||
} | } | ||||
@@ -359,7 +359,7 @@ func CloudBrainShowModels(ctx *context.Context) { | |||||
var fileInfos []FileInfo | var fileInfos []FileInfo | ||||
err = json.Unmarshal([]byte(dirs), &fileInfos) | err = json.Unmarshal([]byte(dirs), &fileInfos) | ||||
if err != nil { | if err != nil { | ||||
log.Error("json.Unmarshal failed:", err.Error()) | |||||
log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.ServerError("json.Unmarshal failed:", err) | ctx.ServerError("json.Unmarshal failed:", err) | ||||
return | return | ||||
} | } | ||||
@@ -390,7 +390,7 @@ func CloudBrainDownloadModel(ctx *context.Context) { | |||||
filePath := "jobs/" +jobName + "/model/" + parentDir | filePath := "jobs/" +jobName + "/model/" + parentDir | ||||
url, err := storage.Attachments.PresignedGetURL(filePath, fileName) | url, err := storage.Attachments.PresignedGetURL(filePath, fileName) | ||||
if err != nil { | if err != nil { | ||||
log.Error("PresignedGetURL failed: %v", err.Error()) | |||||
log.Error("PresignedGetURL failed: %v", err.Error(), ctx.Data["msgID"]) | |||||
ctx.ServerError("PresignedGetURL", err) | ctx.ServerError("PresignedGetURL", err) | ||||
return | return | ||||
} | } | ||||
@@ -411,7 +411,7 @@ func GetRate(ctx *context.Context) { | |||||
} else if job.JobType == string(models.JobTypeSnn4imagenet) { | } else if job.JobType == string(models.JobTypeSnn4imagenet) { | ||||
ctx.Redirect(setting.Snn4imagenetServerHost) | ctx.Redirect(setting.Snn4imagenetServerHost) | ||||
} else { | } else { | ||||
log.Error("JobType error:", job.JobType) | |||||
log.Error("JobType error:%s", job.JobType, ctx.Data["msgID"]) | |||||
} | } | ||||
} | } | ||||
@@ -49,6 +49,7 @@ import ( | |||||
"gitea.com/macaron/session" | "gitea.com/macaron/session" | ||||
"gitea.com/macaron/toolbox" | "gitea.com/macaron/toolbox" | ||||
"github.com/prometheus/client_golang/prometheus" | "github.com/prometheus/client_golang/prometheus" | ||||
gouuid "github.com/satori/go.uuid" | |||||
"github.com/tstranex/u2f" | "github.com/tstranex/u2f" | ||||
) | ) | ||||
@@ -85,7 +86,7 @@ func setupAccessLogger(m *macaron.Macaron) { | |||||
log.Error("Could not set up macaron access logger: %v", err.Error()) | log.Error("Could not set up macaron access logger: %v", err.Error()) | ||||
} | } | ||||
err = logger.SendLog(log.INFO, "", "", 0, buf.String(), "") | |||||
err = logger.SendLog(log.INFO, "", "", 0, buf.String(), ctx.Data["msgID"].(string), "") | |||||
if err != nil { | if err != nil { | ||||
log.Error("Could not set up macaron access logger: %v", err.Error()) | log.Error("Could not set up macaron access logger: %v", err.Error()) | ||||
} | } | ||||
@@ -107,6 +108,24 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) { | |||||
} | } | ||||
} | } | ||||
// SetLogMsgID set msgID in Context | |||||
func SetLogMsgID() func(ctx *macaron.Context) { | |||||
return func(ctx *macaron.Context) { | |||||
start := time.Now() | |||||
uuid := gouuid.NewV4().String() | |||||
ctx.Data["MsgID"] = uuid | |||||
log.Info("Started %s %s for %s", log.ColoredMethod(ctx.Req.Method), ctx.Req.URL.RequestURI(), ctx.RemoteAddr(), ctx.Data["MsgID"]) | |||||
rw := ctx.Resp.(macaron.ResponseWriter) | |||||
ctx.Next() | |||||
status := rw.Status() | |||||
log.Info("Completed %s %s %v %s in %v", log.ColoredMethod(ctx.Req.Method), ctx.Req.URL.RequestURI(), log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(rw.Status())), log.ColoredTime(time.Since(start)), ctx.Data["MsgID"]) | |||||
} | |||||
} | |||||
// NewMacaron initializes Macaron instance. | // NewMacaron initializes Macaron instance. | ||||
func NewMacaron() *macaron.Macaron { | func NewMacaron() *macaron.Macaron { | ||||
gob.Register(&u2f.Challenge{}) | gob.Register(&u2f.Challenge{}) | ||||
@@ -125,6 +144,7 @@ func NewMacaron() *macaron.Macaron { | |||||
m.Use(macaron.Logger()) | m.Use(macaron.Logger()) | ||||
} | } | ||||
} | } | ||||
m.Use(SetLogMsgID()) | |||||
// Access Logger is similar to Router Log but more configurable and by default is more like the NCSA Common Log format | // Access Logger is similar to Router Log but more configurable and by default is more like the NCSA Common Log format | ||||
if setting.EnableAccessLog { | if setting.EnableAccessLog { | ||||
setupAccessLogger(m) | setupAccessLogger(m) | ||||