-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
12 Zinx Logger
刘丹冰 edited this page May 11, 2023
·
2 revisions
Zinx provides a default log format for output, and the default configuration is in ./conf/zinx.json
to configure the log output path and file name.
{
"Name":"zinx server Demo",
"Host":"127.0.0.1",
"TCPPort":8999,
"MaxConn":3,
"WorkerPoolSize":10,
"LogDir": "./mylog",
"LogFile":"zinx.log",
"LogIsolationLevel":0
}
Parameter Description:
Attribute | Description |
---|---|
LogDir | The folder where logs are stored. Default: "./log" |
LogFile | The name of the log file. Default: "" -- If no log file is set, the print information will be printed to stderr |
LogIsolationLevel | Log isolation level. 0: All open, 1: Disable debug, 2: Disable debug/info, 3: Disable debug/info/warn -- Default: All open |
Zinx provides an interface for developers to set up a custom log module. If developers want the Zinx log to meet the requirements of their own modules, they can implement the abstract ILogger of Zinx, define a log object, and then set it through Zinx's SetLogger().
type ILogger interface {
// Log interface without context
InfoF(format string, v ...interface{})
ErrorF(format string, v ...interface{})
DebugF(format string, v ...interface{})
// Log interface with context
InfoFX(ctx context.Context, format string, v ...interface{})
ErrorFX(ctx context.Context, format string, v ...interface{})
DebugFX(ctx context.Context, format string, v ...interface{})
}
package main
import (
"context"
"fmt"
)
// Custom log implementation
// You can reset the log printing method of the Zinx internal engine using your own business's logging method
// This example uses fmt.Println as an example
type MyLogger struct{}
// Log interface without context
func (l *MyLogger) InfoF(format string, v ...interface{}) {
fmt.Printf(format, v...)
}
func (l *MyLogger) ErrorF(format string, v ...interface{}) {
fmt.Printf(format, v...)
}
func (l *MyLogger) DebugF(format string, v ...interface{}) {
fmt.Printf(format, v...)
}
// Log interface with context
func (l *MyLogger) InfoFX(ctx context.Context, format string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(format, v...)
}
func (l *MyLogger) ErrorFX(ctx context.Context, format string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(format, v...)
}
func (l *MyLogger) DebugFX(ctx context.Context, format string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(format, v...)
}
package main
import (
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
)
type TestRouter struct {
znet.BaseRouter
}
// Handle -
func (t *TestRouter) Handle(req ziface.IRequest) {
zlog.Ins().InfoF("Call Handle")
if err := req.GetConnection().SendMsg(0, []byte("test2")); err != nil {
zlog.Ins().ErrorF("err: %v", err)
}
}
func main() {
s := znet.NewServer()
s.AddRouter(1, &TestRouter{})
// Set the logger
zlog.SetLogger(new(MyLogger))
s.Serve()
}