1
0
forked from x/ContextOS

add log.LOGGER

This commit is contained in:
shaoying 2018-11-20 19:58:26 +08:00
parent 5f7e75d9d8
commit 0c8debc6f9
5 changed files with 56 additions and 26 deletions

View File

@ -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
}

View File

@ -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{}

View File

@ -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: "默认模块"},

View File

@ -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)

View File

@ -98,8 +98,6 @@
height:2em;
}
</style>
<script src="/librarys/context.js"></script>
<script src="/librarys/code.js"></script>
</head>
<body onkeyup="return onaction(event, 'keymap')">
{{end}}
@ -207,5 +205,7 @@
{{end}}
{{define "tail"}}
<script src="/librarys/context.js"></script>
<script src="/librarys/code.js"></script>
</body>
{{end}}