规范日志输出

This commit is contained in:
Max 2022-02-09 09:31:36 +08:00
parent 34f4382c19
commit 497ebcaae7
8 changed files with 21 additions and 148 deletions

View file

@ -1,77 +0,0 @@
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/fatih/color"
jsoniter "github.com/json-iterator/go"
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
)
var importRenew bool
var importChunk int
var importCmd = &cobra.Command{
Use: "import",
Short: "导入数据(alpha)",
Long: `导入CSV/Excel数据`,
Run: func(cmd *cobra.Command, args []string) {
Boot()
if len(args) < 3 {
fmt.Fprintln(os.Stderr, "参数错误", args)
fmt.Println("xiang import <数据模型> <文件名称> <导入配置>")
os.Exit(1)
}
name := args[0]
filename := args[1]
configfile := args[2]
// 检查数据文件
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Println(color.RedString("数据文件不存在: %s", filename))
os.Exit(1)
}
if _, err := os.Stat(configfile); os.IsNotExist(err) {
fmt.Println(color.RedString("配置文件不存在: %s", configfile))
os.Exit(1)
}
content, err := ioutil.ReadFile(configfile)
if err != nil {
fmt.Println(color.RedString("读取配置文件失败: %s", err))
os.Exit(1)
}
impt := gou.NewImport(importChunk)
err = jsoniter.Unmarshal(content, impt)
if err != nil {
fmt.Println(color.RedString("解析配置文件=失败: %s", err))
os.Exit(1)
}
// 加载数据模型
engine.Load(config.Conf)
if importRenew {
gou.Select(name).Migrate(true)
}
resp := impt.InsertCSV(filename, name)
for _, err := range *resp.Errors {
fmt.Println(color.RedString("%s line: %d", err.Message, err.Line))
}
fmt.Println(color.WhiteString("成功: %d, 失败: %d, 总共: %d", resp.Success, resp.Failure, resp.Total))
},
}
func init() {
importCmd.PersistentFlags().BoolVarP(&importRenew, "renew", "r", false, "清空数据模型原有数据")
importCmd.PersistentFlags().IntVarP(&importChunk, "chunk", "c", 1000, "每次处理数量(数值越大,处理越快,内存占用越大)")
}

View file

@ -11,12 +11,12 @@ import (
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/importer/from"
"github.com/yaoapp/yao/importer/xlsx"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/xfs"
"github.com/yaoapp/yao/xlog"
)
// Importers 导入器
@ -163,12 +163,12 @@ func (imp *Importer) DataClean(data [][]interface{}, bindings []*Binding) ([]str
func DataValidate(row []interface{}, value interface{}, rule string) ([]interface{}, bool) {
process, err := gou.ProcessOf(rule, value, row)
if err != nil {
xlog.Printf("DataValidate: %s %s", rule, err.Error())
log.With(log.F{"rule": rule, "row": row}).Error("DataValidate: %s", err.Error())
return row, true
}
res, err := process.Exec()
if err != nil {
xlog.Printf("DataValidate: %s %s", rule, err.Error())
log.With(log.F{"rule": rule, "row": row}).Error("DataValidate: %s", err.Error())
return row, true
}
@ -397,14 +397,14 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) map[string]int {
process, err := gou.ProcessOf(imp.Process, columns, data)
if err != nil {
failed = failed + length
xlog.Printf("导入失败 %d %s ", line, err.Error())
log.With(log.F{"line": line}).Error("导入失败: %s", err.Error())
return
}
response, err := process.Exec()
if err != nil {
failed = failed + length
xlog.Printf("导入失败 %d %s ", line, err.Error())
log.With(log.F{"line": line}).Error("导入失败: %s", err.Error())
return
}
@ -422,7 +422,7 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) map[string]int {
return
}
xlog.Printf("导入处理器未返回失败结果 %#v %d %d", response, line, length)
log.With(log.F{"line": line, "response": response, "length": length}).Error("导入处理器未返回失败结果")
})
return map[string]int{
"total": total,

View file

@ -6,8 +6,8 @@ import (
"github.com/xuri/excelize/v2"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/importer/from"
"github.com/yaoapp/yao/xlog"
)
// Xlsx xlsx file
@ -54,7 +54,7 @@ func Open(filename string) *Xlsx {
// Close 关闭文件句柄
func (xlsx *Xlsx) Close() error {
if err := xlsx.File.Close(); err != nil {
xlog.Println(err.Error())
log.Error("Close file error: %s", err.Error())
return err
}
return nil
@ -122,7 +122,7 @@ func (xlsx *Xlsx) readLine(line int, axises []string) ([]interface{}, bool) {
axis := positionToAxis(line, c)
value, err = xlsx.File.GetCellValue(xlsx.SheetName, axis)
if err != nil {
xlog.Printf("读取数据出错 %s %s %s", xlsx.SheetName, axis, err.Error())
log.With(log.F{"SheetName": xlsx.SheetName, "axis": axis}).Error("读取数据出错 %s", err.Error())
value = ""
}
}
@ -160,7 +160,7 @@ func (xlsx *Xlsx) Columns() []from.Column {
}
cellType, err := xlsx.File.GetCellType(xlsx.SheetName, axis)
if err != nil {
xlog.Printf("读取数据类型失败 %s", err.Error())
log.With(log.F{"SheetName": xlsx.SheetName, "axis": axis}).Error("读取数据类型失败 %s", err.Error())
}
columns = append(columns, from.Column{
Name: cell,

View file

@ -6,9 +6,9 @@ import (
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/helper"
"github.com/yaoapp/yao/xlog"
)
// Guards 服务中间件
@ -27,15 +27,13 @@ func bearerJWT(c *gin.Context) {
}
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
if config.Conf.Mode == "debug" {
xlog.Printf("JWT: %s Secret: %s", tokenString, config.Conf.JWTSecret)
}
log.Debug("JWT: %s Secret: %s", tokenString, config.Conf.JWTSecret)
token, err := jwt.ParseWithClaims(tokenString, &helper.JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(config.Conf.JWTSecret), nil
})
if err != nil {
xlog.Printf("JWT ParseWithClaims Error: %s", err)
log.Error("JWT ParseWithClaims Error: %s", err)
c.JSON(403, gin.H{"code": 403, "message": fmt.Sprintf("登录已过期或令牌失效(%s)", err)})
c.Abort()
return

View file

@ -10,9 +10,9 @@ import (
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/kun/utils"
"github.com/yaoapp/yao/xlog"
)
// IsAllow 鉴权处理程序
@ -118,20 +118,17 @@ func (api API) MergeDefaultQueryParam(param gou.QueryParam, i int, sid string) g
// GetQueryParam 解析参数
func GetQueryParam(v interface{}, sid string) gou.QueryParam {
fmt.Println("\n==== GetQueryParam ===== SID:", sid)
log.With(log.F{"sid": sid}).Trace("GetQueryParam Entry")
data := map[string]interface{}{}
if sid != "" {
var err error
ss := session.Global().ID(sid)
data, err = ss.Dump()
utils.Dump(data)
log.With(log.F{"data": data}).Trace("GetQueryParam Session Data")
if err != nil {
xlog.Printf("读取会话信息出错 %s", err.Error())
log.Error("读取会话信息出错 %s", err.Error())
}
}
fmt.Println("==== GetQueryParam========================")
fmt.Println("")
v = share.Bind(v, maps.Of(data).Dot())
param, ok := gou.AnyToQueryParam(v)
if !ok {

View file

@ -11,10 +11,8 @@ import (
"github.com/yaoapp/gou/helper"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/xlog"
)
// Tables 已载入模型
@ -182,7 +180,7 @@ func (table *Table) Before(process string, processArgs []interface{}, sid string
response, err := gou.NewProcess(process, args...).WithSID(sid).Exec()
if err != nil {
xlog.Println("Hook执行失败: ", err.Error(), maps.StrAny{"process": process, "args": args})
log.With(log.F{"process": process, "args": args}).Warn("Hook执行失败: ", err.Error())
return processArgs
}
@ -191,7 +189,7 @@ func (table *Table) Before(process string, processArgs []interface{}, sid string
return res
}
xlog.Println("Hook执行失败: 无效的处理器", maps.StrAny{"process": process, "response": response})
log.With(log.F{"process": process, "args": args}).Warn("Hook执行失败: 无效的处理器")
return processArgs
}
@ -203,7 +201,7 @@ func (table *Table) After(process string, data interface{}, args []interface{},
args = append([]interface{}{data}, args...)
response, err := gou.NewProcess(process, args...).WithSID(sid).Exec()
if err != nil {
xlog.Println("Hook执行失败: ", err.Error(), maps.StrAny{"process": process, "args": args})
log.With(log.F{"process": process, "args": args}).Warn("Hook执行失败: ", err.Error())
return data
}
return response

View file

@ -16,7 +16,6 @@ import (
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/helper"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/xlog"
)
// Process
@ -64,9 +63,7 @@ func LoadWorkFlow(source []byte, name string) (*WorkFlow, error) {
workflow := WorkFlow{Name: name, Source: source}
err := jsoniter.Unmarshal(source, &workflow)
if err != nil {
xlog.Println(name)
xlog.Println(err.Error())
xlog.Println(string(source))
log.With(log.F{"name": name, "source": source}).Error("LoadWorkFlow: %s", err.Error())
return nil, err
}

View file

@ -1,40 +0,0 @@
package xlog
import (
"fmt"
"log"
jsoniter "github.com/json-iterator/go"
)
// XLog 日志接口
type XLog interface{}
// These functions write to the standard logger.
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
log.Output(2, fmt.Sprint(v...))
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
log.Output(2, fmt.Sprintf(format, v...))
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
content, err := jsoniter.Marshal(v)
if err != nil {
log.Output(2, fmt.Sprintln(v...))
}
log.Output(2, string(content))
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}