From 0c8debc6f961d41a9640941c754772dac0f9e507 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 20 Nov 2018 19:58:26 +0800 Subject: [PATCH] add log.LOGGER --- src/contexts/ctx/ctx.go | 14 ++++++++------ src/contexts/log/log.go | 25 +++++++++++++++++++++++++ src/contexts/web/web.go | 37 ++++++++++++++++++++----------------- usr/librarys/code.js | 2 +- usr/template/code/code.tmpl | 4 ++-- 5 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index a5909b62..a15c5136 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -17,6 +17,10 @@ import ( "time" ) +type LOGGER interface { + LOG(*Message, string, string) +} + func Right(arg interface{}) bool { switch str := arg.(type) { case nil: @@ -600,13 +604,11 @@ func (m *Message) Copy(msg *Message, meta string, arg ...string) *Message { return m } func (m *Message) Log(action string, str string, arg ...interface{}) *Message { - l := m.Sess("log", !m.Confs("compact_log")) - if l == nil || m.Detail(0) == "log" || m.Detail(0) == "write" || m.Options("silent") { - return m + if l := m.Sess("log", false); l != nil { + if log, ok := l.target.Server.(LOGGER); ok { + log.LOG(m, action, fmt.Sprintf(str, arg...)) + } } - - l.Optionv("msg", m) - l.Cmd("log", action, fmt.Sprintf(str, arg...)) return m } diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index 03e43095..bc6fc020 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -13,6 +13,31 @@ type LOG struct { *ctx.Context } +func (log *LOG) LOG(msg *ctx.Message, action string, str string) { + m := log.Context.Message() + + if m.Confs("silent", action) { + return + } + if m.Confs("module", fmt.Sprintf("%s.%s", msg.Target().Name, action)) { + return + } + + color := 0 + if m.Confs("flag_color") && m.Confs("color", action) { + color = m.Confi("color", action) + } + + date := time.Now().Format(m.Conf("flag_time")) + action = fmt.Sprintf("%d %s(%s->%s)", msg.Code(), action, msg.Source().Name, msg.Target().Name) + + if color > 0 { + log.out.WriteString(fmt.Sprintf("%s\033[%dm%s %s\033[0m\n", date, color, action, str)) + } else { + log.out.WriteString(fmt.Sprintf("%s%s %s\n", date, action, str)) + } +} + func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{} c.Configs = map[string]*ctx.Config{} diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 8bdb6e54..2a23fc0e 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -148,26 +148,28 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { if index { m.Log("info", "").Log("info", "%v %s %s", r.RemoteAddr, r.Method, r.URL) + wd, _ := os.Getwd() + m.Log("info", "wd: %s", wd) } - if index && m.Confs("logheaders") { - for k, v := range r.Header { - m.Log("info", "%s: %v", k, v) - } - m.Log("info", "") - } - - if r.URL.Path == "/" && m.Confs("root_index") { - r.URL.Path = m.Conf("root_index") - } - + // if index && m.Confs("logheaders") { + // for k, v := range r.Header { + // m.Log("info", "%s: %v", k, v) + // } + // m.Log("info", "") + // } + // + // if r.URL.Path == "/" && m.Confs("root_index") { + // r.URL.Path = m.Conf("root_index") + // } + // web.ServeMux.ServeHTTP(w, r) - if index && m.Confs("logheaders") { - for k, v := range w.Header() { - m.Log("info", "%s: %v", k, v) - } - m.Log("info", "") - } + // if index && m.Confs("logheaders") { + // for k, v := range w.Header() { + // m.Log("info", "%s: %v", k, v) + // } + // m.Log("info", "") + // } } func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { @@ -272,6 +274,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", "cert": &ctx.Config{Name: "cert", Value: "etc/cert.pem", Help: "路由数量"}, "key": &ctx.Config{Name: "key", Value: "etc/key.pem", Help: "路由数量"}, + "library_dir": &ctx.Config{Name: "library_dir", Value: "usr/librarys", Help: "模板路径"}, "template_dir": &ctx.Config{Name: "template_dir", Value: "usr/template", Help: "模板路径"}, "template_debug": &ctx.Config{Name: "template_debug", Value: "true", Help: "模板调试"}, "componet_context": &ctx.Config{Name: "component_context", Value: "nfs", Help: "默认模块"}, diff --git a/usr/librarys/code.js b/usr/librarys/code.js index 8a677883..601229b6 100644 --- a/usr/librarys/code.js +++ b/usr/librarys/code.js @@ -484,7 +484,7 @@ function init_download(event) { if ((dir.endsWith(".sh") || dir.endsWith(".shy") || dir.endsWith(".py")) && !show) { var command = document.querySelector("form.option.command") var cmd = command["cmd"] - cmd.value = "run "+ dir + cmd.value = "run "+ dir.split("/").pop() cmd.focus() if (run) { check_option(command) diff --git a/usr/template/code/code.tmpl b/usr/template/code/code.tmpl index 4ef9962f..ae022139 100644 --- a/usr/template/code/code.tmpl +++ b/usr/template/code/code.tmpl @@ -98,8 +98,6 @@ height:2em; } - - {{end}} @@ -207,5 +205,7 @@ {{end}} {{define "tail"}} + + {{end}}