mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-26 01:04:06 +08:00
opt log
This commit is contained in:
parent
013b38fb1b
commit
0862bdd761
@ -4,5 +4,5 @@ var version = struct {
|
|||||||
host string
|
host string
|
||||||
self int
|
self int
|
||||||
}{
|
}{
|
||||||
"2019-07-27 12:43:56", "ZYB-20190522USI", 276,
|
"2019-07-27 13:41:52", "ZYB-20190522USI", 282,
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,42 @@ package ctx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"toolkit"
|
"toolkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (m *Message) Log(action string, str string, arg ...interface{}) *Message {
|
||||||
|
if action == "error" {
|
||||||
|
kit.Log("error", fmt.Sprintf("chain: %s", m.Format("chain")))
|
||||||
|
kit.Log("error", fmt.Sprintf("%s %s %s", m.Format(), action, fmt.Sprintf(str, arg...)))
|
||||||
|
kit.Log("error", fmt.Sprintf("stack: %s", m.Format("stack")))
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Options("log.disable") {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
if l := m.Sess("log", false); l != nil {
|
||||||
|
if log, ok := l.target.Server.(LOGGER); ok {
|
||||||
|
if action == "error" {
|
||||||
|
log.Log(m, "error", "chain: %s", m.Format("chain"))
|
||||||
|
}
|
||||||
|
if log.Log(m, action, str, arg...); action == "error" {
|
||||||
|
log.Log(m, "error", "stack: %s", m.Format("stack"))
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, str, arg...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) Register(s *Context, x Server, args ...interface{}) *Context {
|
func (c *Context) Register(s *Context, x Server, args ...interface{}) *Context {
|
||||||
name, force := s.Name, false
|
name, force := s.Name, false
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
|
@ -2,7 +2,6 @@ package ctx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -504,34 +503,6 @@ func (m *Message) Gdb(arg ...interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (m *Message) Log(action string, str string, arg ...interface{}) *Message {
|
|
||||||
if m.Options("log.disable") {
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
if l := m.Sess("log", false); l != nil {
|
|
||||||
if log, ok := l.target.Server.(LOGGER); ok {
|
|
||||||
if action == "error" {
|
|
||||||
log.Log(m, "error", "chain: %s", m.Format("chain"))
|
|
||||||
}
|
|
||||||
log.Log(m, action, str, arg...)
|
|
||||||
if action == "error" {
|
|
||||||
log.Log(m, "error", "stack: %s", m.Format("stack"))
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Printf(str, arg...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if action == "error" {
|
|
||||||
kit.Log("error", fmt.Sprintf("chain: %s", m.Format("chain")))
|
|
||||||
kit.Log("error", fmt.Sprintf("%s %s %s", m.Format(), action, fmt.Sprintf(str, arg...)))
|
|
||||||
kit.Log("error", fmt.Sprintf("stack: %s", m.Format("stack")))
|
|
||||||
}
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
func (m *Message) Show(str string, args ...interface{}) *Message {
|
func (m *Message) Show(str string, args ...interface{}) *Message {
|
||||||
res := fmt.Sprintf(str, args...)
|
res := fmt.Sprintf(str, args...)
|
||||||
|
|
||||||
|
@ -15,6 +15,12 @@ type LOG struct {
|
|||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (log *LOG) Log(msg *ctx.Message, action string, str string, arg ...interface{}) {
|
||||||
|
if log.queue != nil {
|
||||||
|
log.queue <- map[string]interface{}{"action": action, "str": str, "arg": arg, "msg": msg}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (log *LOG) Value(msg *ctx.Message, arg ...interface{}) []string {
|
func (log *LOG) Value(msg *ctx.Message, arg ...interface{}) []string {
|
||||||
args := append(kit.Trans(arg...))
|
args := append(kit.Trans(arg...))
|
||||||
|
|
||||||
@ -30,39 +36,33 @@ func (log *LOG) Value(msg *ctx.Message, arg ...interface{}) []string {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (log *LOG) Log(msg *ctx.Message, action string, str string, arg ...interface{}) {
|
|
||||||
if log.queue != nil {
|
|
||||||
log.queue <- map[string]interface{}{
|
|
||||||
"action": action,
|
|
||||||
"str": str,
|
|
||||||
"arg": arg,
|
|
||||||
"msg": msg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
c.Caches = map[string]*ctx.Cache{}
|
return &LOG{Context: c}
|
||||||
c.Configs = map[string]*ctx.Config{}
|
|
||||||
|
|
||||||
s := new(LOG)
|
|
||||||
s.Context = c
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
return log
|
return log
|
||||||
}
|
}
|
||||||
func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
|
func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
|
||||||
|
// 创建文件
|
||||||
log.file = map[string]*os.File{}
|
log.file = map[string]*os.File{}
|
||||||
|
m.Assert(os.MkdirAll(m.Conf("logdir"), 0770))
|
||||||
|
m.Confm("output", "file", func(key string, value string) {
|
||||||
|
switch value {
|
||||||
|
case "":
|
||||||
|
case "stderr":
|
||||||
|
log.file[key] = os.Stderr
|
||||||
|
case "stdout":
|
||||||
|
log.file[key] = os.Stdout
|
||||||
|
default:
|
||||||
|
if f, e := os.Create(path.Join(m.Conf("logdir"), value)); m.Assert(e) {
|
||||||
|
log.file[key] = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
os.MkdirAll(m.Conf("logdir"), 0770)
|
// 创建队列
|
||||||
kit.Log("error", "make log dir %s", m.Conf("logdir"))
|
log.queue = make(chan map[string]interface{}, m.Confi("logbuf"))
|
||||||
|
|
||||||
log.queue = make(chan map[string]interface{}, 1024)
|
|
||||||
// for _, v := range []string{"error", "bench", "debug"} {
|
|
||||||
// log.Log(m, v, "hello world\n")
|
|
||||||
// log.Log(m, v, "hello world")
|
|
||||||
// }
|
|
||||||
m.Cap("stream", m.Conf("output", []string{"bench", "value", "file"}))
|
m.Cap("stream", m.Conf("output", []string{"bench", "value", "file"}))
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -70,8 +70,8 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
case l := <-log.queue:
|
case l := <-log.queue:
|
||||||
m.Capi("nlog", 1)
|
m.Capi("nlog", 1)
|
||||||
msg := l["msg"].(*ctx.Message)
|
msg := l["msg"].(*ctx.Message)
|
||||||
|
|
||||||
args := kit.Trans(l["arg"].([]interface{})...)
|
args := kit.Trans(l["arg"].([]interface{})...)
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
for _, v := range []string{kit.Format(l["action"]), "bench"} {
|
for _, v := range []string{kit.Format(l["action"]), "bench"} {
|
||||||
for i := len(args); i >= 0; i-- {
|
for i := len(args); i >= 0; i-- {
|
||||||
@ -79,29 +79,24 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
if !kit.Right(value) {
|
if !kit.Right(value) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
p := kit.Chains(log.Configs["output"].Value, []string{"file", value[0]})
|
|
||||||
if p == "" {
|
if value[0] == "debug" && !m.Options("log.debug") {
|
||||||
continue
|
break loop
|
||||||
}
|
}
|
||||||
|
|
||||||
file, ok := os.Stdout, true
|
// 日志文件
|
||||||
if p != "stdout" {
|
file := os.Stderr
|
||||||
name := path.Join(m.Conf("logdir"), p)
|
if f, ok := log.file[value[0]]; ok {
|
||||||
file, ok = log.file[name]
|
file = f
|
||||||
if !ok {
|
} else {
|
||||||
if f, e := os.Create(name); e == nil {
|
break loop
|
||||||
file, log.file[name] = f, f
|
|
||||||
kit.Log("error", "%s log file %s", "open", name)
|
|
||||||
} else {
|
|
||||||
kit.Log("error", "%s log file %s %s", "open", name, e)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 日志格式
|
||||||
font := m.Conf("output", []string{"font", kit.Select("", value, 1)})
|
font := m.Conf("output", []string{"font", kit.Select("", value, 1)})
|
||||||
meta := msg.Format(m.Confv("output", []string{"meta", kit.Select("short", value, 2)}).([]interface{})...)
|
meta := msg.Format(m.Confv("output", []string{"meta", kit.Select("short", value, 2)}).([]interface{})...)
|
||||||
|
|
||||||
|
// 输出日志
|
||||||
fmt.Fprintln(file, fmt.Sprintf("%d %s %s%s %s%s", m.Capi("nout", 1), meta, font,
|
fmt.Fprintln(file, fmt.Sprintf("%d %s %s%s %s%s", m.Capi("nout", 1), meta, font,
|
||||||
kit.Format(l["action"]), fmt.Sprintf(kit.Format(l["str"]), l["arg"].([]interface{})...),
|
kit.Format(l["action"]), fmt.Sprintf(kit.Format(l["str"]), l["arg"].([]interface{})...),
|
||||||
kit.Select("", "\033[0m", font != "")))
|
kit.Select("", "\033[0m", font != "")))
|
||||||
@ -110,13 +105,9 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
func (log *LOG) Close(m *ctx.Message, arg ...string) bool {
|
func (log *LOG) Close(m *ctx.Message, arg ...string) bool {
|
||||||
switch log.Context {
|
|
||||||
case m.Target():
|
|
||||||
case m.Source():
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,18 +117,19 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
|
|||||||
"nout": &ctx.Cache{Name: "nout", Value: "0", Help: "日志输出数量"},
|
"nout": &ctx.Cache{Name: "nout", Value: "0", Help: "日志输出数量"},
|
||||||
},
|
},
|
||||||
Configs: map[string]*ctx.Config{
|
Configs: map[string]*ctx.Config{
|
||||||
"logdir": &ctx.Config{Name: "logdir", Value: "var/log", Help: ""},
|
"logbuf": &ctx.Config{Name: "logbuf", Value: "1024", Help: "日志队列长度"},
|
||||||
|
"logdir": &ctx.Config{Name: "logdir", Value: "var/log", Help: "日志目录"},
|
||||||
|
|
||||||
"output": &ctx.Config{Name: "output", Value: map[string]interface{}{
|
"output": &ctx.Config{Name: "output", Value: map[string]interface{}{
|
||||||
"file": map[string]interface{}{
|
"file": map[string]interface{}{
|
||||||
"bench": "bench.log",
|
|
||||||
"debug": "debug.log",
|
"debug": "debug.log",
|
||||||
"error": "error.log",
|
"bench": "bench.log",
|
||||||
"right": "right.log",
|
"right": "right.log",
|
||||||
|
"error": "error.log",
|
||||||
},
|
},
|
||||||
"font": map[string]interface{}{
|
"font": map[string]interface{}{
|
||||||
"red": "\033[31m",
|
"red": "\033[31m",
|
||||||
"green": "\033[32m",
|
"green": "\033[32m",
|
||||||
"blue": "\033[34m",
|
|
||||||
"yellow": "\033[33m",
|
"yellow": "\033[33m",
|
||||||
},
|
},
|
||||||
"meta": map[string]interface{}{
|
"meta": map[string]interface{}{
|
||||||
@ -145,22 +137,24 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
|
|||||||
"long": []interface{}{"time", "ship"},
|
"long": []interface{}{"time", "ship"},
|
||||||
},
|
},
|
||||||
|
|
||||||
"error": map[string]interface{}{"value": []interface{}{"error", "red"}},
|
|
||||||
"trace": map[string]interface{}{"value": []interface{}{"error", "red"}},
|
|
||||||
"debug": map[string]interface{}{"value": []interface{}{"debug"}},
|
"debug": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
"search": map[string]interface{}{"value": []interface{}{"debug"}},
|
"search": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
"call": map[string]interface{}{"value": []interface{}{"debug"}},
|
"call": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
"back": map[string]interface{}{"value": []interface{}{"debug"}},
|
"back": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
|
"send": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
"send": map[string]interface{}{"value": []interface{}{"debug"}},
|
"recv": map[string]interface{}{"value": []interface{}{"debug"}},
|
||||||
"recv": map[string]interface{}{"value": []interface{}{"debug"}},
|
|
||||||
|
|
||||||
"bench": map[string]interface{}{"value": []interface{}{"bench"}},
|
"bench": map[string]interface{}{"value": []interface{}{"bench"}},
|
||||||
"begin": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
"begin": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
||||||
"start": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
"start": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
||||||
"close": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
"close": map[string]interface{}{"value": []interface{}{"bench", "red"}},
|
||||||
"warn": map[string]interface{}{"value": []interface{}{"bench", "yellow"}},
|
|
||||||
"stack": map[string]interface{}{"value": []interface{}{"bench", "yellow"}},
|
"stack": map[string]interface{}{"value": []interface{}{"bench", "yellow"}},
|
||||||
|
"warn": map[string]interface{}{"value": []interface{}{"bench", "yellow"}},
|
||||||
|
|
||||||
|
"right": map[string]interface{}{"value": []interface{}{"right"}},
|
||||||
|
|
||||||
|
"error": map[string]interface{}{"value": []interface{}{"error", "red"}},
|
||||||
|
"trace": map[string]interface{}{"value": []interface{}{"error", "red"}},
|
||||||
|
|
||||||
"cmd": map[string]interface{}{"value": []interface{}{"bench", "green"},
|
"cmd": map[string]interface{}{"value": []interface{}{"bench", "green"},
|
||||||
"lex": map[string]interface{}{"value": []interface{}{"debug", "green"}},
|
"lex": map[string]interface{}{"value": []interface{}{"debug", "green"}},
|
||||||
@ -177,11 +171,10 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
|
|||||||
"rsa": map[string]interface{}{"value": []interface{}{"debug", "red"}},
|
"rsa": map[string]interface{}{"value": []interface{}{"debug", "red"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"right": map[string]interface{}{"value": []interface{}{"right"}},
|
|
||||||
}, Help: "日志输出配置"},
|
}, Help: "日志输出配置"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"_init": &ctx.Command{Name: "_init", Help: "启动日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
"_init": &ctx.Command{Name: "_init", Help: "初始化", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
m.Target().Start(m)
|
m.Target().Start(m)
|
||||||
return
|
return
|
||||||
}},
|
}},
|
||||||
@ -195,7 +188,5 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log := &LOG{}
|
ctx.Index.Register(Index, &LOG{Context: Index})
|
||||||
log.Context = Index
|
|
||||||
ctx.Index.Register(Index, log)
|
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,6 @@ import (
|
|||||||
|
|
||||||
var DisableLog = false
|
var DisableLog = false
|
||||||
|
|
||||||
func Pwd() string {
|
|
||||||
wd, _ := os.Getwd()
|
|
||||||
return wd
|
|
||||||
}
|
|
||||||
func Env(key string) {
|
|
||||||
os.Getenv(key)
|
|
||||||
}
|
|
||||||
func Log(action string, str string, args ...interface{}) {
|
func Log(action string, str string, args ...interface{}) {
|
||||||
if DisableLog {
|
if DisableLog {
|
||||||
return
|
return
|
||||||
@ -31,6 +24,14 @@ func Log(action string, str string, args ...interface{}) {
|
|||||||
}
|
}
|
||||||
fmt.Fprintf(os.Stderr, "%s: %s\n", action, str)
|
fmt.Fprintf(os.Stderr, "%s: %s\n", action, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Pwd() string {
|
||||||
|
wd, _ := os.Getwd()
|
||||||
|
return wd
|
||||||
|
}
|
||||||
|
func Env(key string) {
|
||||||
|
os.Getenv(key)
|
||||||
|
}
|
||||||
func Errorf(str string, args ...interface{}) {
|
func Errorf(str string, args ...interface{}) {
|
||||||
Log("error", str, args...)
|
Log("error", str, args...)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user