Skip to content

Commit

Permalink
add log storage
Browse files Browse the repository at this point in the history
  • Loading branch information
MacBookAirM2 committed May 10, 2023
1 parent a770e24 commit 575d7ee
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 120 deletions.
10 changes: 6 additions & 4 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,15 @@ recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
```json
{
"Name":"zinx v-0.10 demoApp",
"Host":"127.0.0.1",
"TCPPort":7777,
"Host":"0.0.0.0",
"TCPPort":9090,
"MaxConn":3,
"WorkerPoolSize":10,
"LogDir": "./mylog",
"LogFile":"zinx.log",
"LogIsolationLevel": 0
"LogFile":"app.log",
"LogSaveDays":15,
"LogCons": true,
"LogIsolationLevel":0
}
```

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
```json
{
"Name":"zinx v-0.10 demoApp",
"Host":"127.0.0.1",
"TCPPort":7777,
"Host":"0.0.0.0",
"TCPPort":9090,
"MaxConn":3,
"WorkerPoolSize":10,
"LogDir": "./mylog",
"LogFile":"zinx.log",
"LogFile":"app.log",
"LogSaveDays":15,
"LogCons": true,
"LogIsolationLevel":0
}
```
Expand Down
12 changes: 12 additions & 0 deletions conf/zinx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Name":"zinx v-0.10 demoApp",
"Host":"0.0.0.0",
"TCPPort":9090,
"MaxConn":3,
"WorkerPoolSize":10,
"LogDir": "./mylog",
"LogFile":"app.log",
"LogSaveDays":15,
"LogCons": true,
"LogIsolationLevel":0
}
1 change: 1 addition & 0 deletions examples/zinx_decoder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func DoConnectionLost(conn ziface.IConnection) {
}

func main() {
//zlog.SetLogFile("./logs", "app.log")
s := znet.NewServer()

s.SetOnConnStart(DoConnectionBegin)
Expand Down
3 changes: 2 additions & 1 deletion examples/zinx_logger/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
"time"
)
Expand Down Expand Up @@ -43,6 +44,6 @@ func (t *TestRouter) PostHandle(req ziface.IRequest) {
func main() {
s := znet.NewServer()
s.AddRouter(1, &TestRouter{})
//zlog.SetLogger(new(MyLogger))
zlog.SetLogger(new(MyLogger))
s.Serve()
}
23 changes: 17 additions & 6 deletions zconf/globalobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const (
)

/*
Store all global parameters related to the Zinx framework for use by other modules.
Some parameters can also be configured by the user based on the zinx.json file.
(存储一切有关Zinx框架的全局参数,供其他模块使用
一些参数也可以通过 用户根据 zinx.json来配置)
Store all global parameters related to the Zinx framework for use by other modules.
Some parameters can also be configured by the user based on the zinx.json file.
(存储一切有关Zinx框架的全局参数,供其他模块使用
一些参数也可以通过 用户根据 zinx.json来配置)
*/
type Config struct {
/*
Expand Down Expand Up @@ -66,6 +66,10 @@ type Config struct {
// (日志文件名称 默认"" --如果没有设置日志文件,打印信息将打印至stderr)
LogFile string

LogSaveDays int // 日志最大保留天数
LogFileSize int64 // 日志单个日志最大容量 默认 64MB,单位:字节,记得一定要换算成MB(1024 * 1024)
LogCons bool // 日志标准输出 默认 false

// The level of log isolation. The values can be 0 (all open), 1 (debug off), 2 (debug/info off), 3 (debug/info/warn off), and so on.
//日志隔离级别 -- 0:全开 1:关debug 2:关debug/info 3:关debug/info/warn ...
LogIsolationLevel int
Expand All @@ -85,7 +89,7 @@ type Config struct {
}

/*
Define a global object.(定义一个全局的对象)
Define a global object.(定义一个全局的对象)
*/
var GlobalObject *Config

Expand Down Expand Up @@ -154,14 +158,21 @@ func (g *Config) HeartbeatMaxDuration() time.Duration {
func (g *Config) InitLogConfig() {
if g.LogFile != "" {
zlog.SetLogFile(g.LogDir, g.LogFile)
zlog.SetCons(g.LogCons)
}
if g.LogSaveDays > 0 {
zlog.SetMaxAge(g.LogSaveDays)
}
if g.LogFileSize > 0 {
zlog.SetMaxSize(g.LogFileSize)
}
if g.LogIsolationLevel > zlog.LogDebug {
zlog.SetLogLevel(g.LogIsolationLevel)
}
}

/*
init, set default value
init, set default value
*/
func init() {
pwd, err := os.Getwd()
Expand Down
131 changes: 37 additions & 94 deletions zlog/logger_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package zlog
import (
"bytes"
"fmt"
"io"
"github.com/aceld/zinx/zutils"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -75,17 +75,9 @@ type ZinxLoggerCore struct {
// log tag bit (日志标记位)
flag int

// the file descriptor for log output
// (日志输出的文件描述符)
out io.Writer

// the output buffer (输出的缓冲区)
buf bytes.Buffer

// the output file currently bound to the log
// (当前日志绑定的输出文件)
file *os.File

// log isolation level
// (日志隔离级别)
isolationLevel int
Expand All @@ -94,21 +86,7 @@ type ZinxLoggerCore struct {
// (获取日志文件名和代码上述的runtime.Call 的函数调用层数)
calldDepth int

// log file name
// (日志文件名称)
fileName string

// log file directory
// (日志文件目录)
fileDir string

// last write date
// (上次写入日期)
lastWriteDate int

// file swap lock
// (文件交换锁)
fsLock sync.Mutex
fw *zutils.Writer

onLogHook func([]byte)
}
Expand All @@ -120,11 +98,11 @@ out: The file io for standard output
prefix: The prefix of the log
flag: The flag of the log header information
*/
func NewZinxLog(out io.Writer, prefix string, flag int) *ZinxLoggerCore {
func NewZinxLog(prefix string, flag int) *ZinxLoggerCore {

// By default, debug is turned on, the depth is 2, and the ZinxLogger object calling the log print method can call up to two levels to reach the output function
// (默认 debug打开, calledDepth深度为2,ZinxLogger对象调用日志打印方法最多调用两层到达output函数)
zlog := &ZinxLoggerCore{out: out, prefix: prefix, flag: flag, file: nil, isolationLevel: 0, calldDepth: 2}
zlog := &ZinxLoggerCore{prefix: prefix, flag: flag, isolationLevel: 0, calldDepth: 2}

// Set the log object's resource cleanup destructor method (this is not necessary, as go's Gc will automatically collect, but for the sake of neatness)
// (设置log对象 回收资源 析构方法(不设置也可以,go的Gc会自动回收,强迫症没办法))
Expand Down Expand Up @@ -245,23 +223,19 @@ func (log *ZinxLoggerCore) OutPut(level int, s string) error {
log.buf.WriteByte('\n')
}

log.updateOutputFile()

var err error
if log.file == nil {
if log.fw == nil {
// if log file is not set, output to console
_, _ = os.Stderr.Write(log.buf.Bytes())
} else {
// write the filled buffer to IO output
_, err = log.out.Write(log.buf.Bytes())
_, err = log.fw.Write(log.buf.Bytes())
}

if log.onLogHook != nil {
log.onLogHook(log.buf.Bytes())
}

return err

}

func (log *ZinxLoggerCore) verifyLogIsolation(logLevel int) bool {
Expand Down Expand Up @@ -407,66 +381,48 @@ func (log *ZinxLoggerCore) SetPrefix(prefix string) {
// SetLogFile sets the log file output
// (设置日志文件输出)
func (log *ZinxLoggerCore) SetLogFile(fileDir string, fileName string) {
log.fileDir = fileDir
log.fileName = fileName
}

// Close the file associated with the log
// (关闭日志绑定的文件)
func (log *ZinxLoggerCore) closeFile() {
if log.file != nil {
_ = log.file.Close()
log.file = nil
log.out = os.Stderr
if log.fw != nil {
log.fw.Close()
}
log.fw = zutils.New(filepath.Join(fileDir, fileName))
}

// update the output file for the log
// (更新文件输出)
func (log *ZinxLoggerCore) updateOutputFile() {

var file *os.File

yearDay := time.Now().YearDay()

if log.lastWriteDate == yearDay && log.file != nil {
// SetMaxAge 最大保留天数
func (log *ZinxLoggerCore) SetMaxAge(ma int) {
if log.fw == nil {
return
}
log.mu.Lock()
defer log.mu.Unlock()
log.fw.SetMaxAge(ma)
}

log.fsLock.Lock()
defer log.fsLock.Unlock()

if log.lastWriteDate == yearDay && log.file != nil {
// SetMaxSize 单个日志最大容量 单位:字节
func (log *ZinxLoggerCore) SetMaxSize(ms int64) {
if log.fw == nil {
return
}
log.mu.Lock()
defer log.mu.Unlock()
log.fw.SetMaxSize(ms)
}

log.lastWriteDate = yearDay

// create the log directory
_ = mkdirLog(log.fileDir)

// define the log file name = log file name . date suffix . log
// supported file name formats:
// 1. "zinx.log"
// 2. "zinx"
// 3. "zinx.zinx.zinx.log"
realFileNameSlice := strings.Split(log.fileName, ".log")
realFileName := realFileNameSlice[0]
newDailyFile := log.fileDir + "/" + realFileName + "." + time.Now().Format("20060102") + ".log"

if log.checkFileExist(newDailyFile) {
file, _ = os.OpenFile(newDailyFile, os.O_APPEND|os.O_RDWR, 0644) // rw-r--r--
} else {
file, _ = os.OpenFile(newDailyFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
// SetCons 同时输出控制台
func (log *ZinxLoggerCore) SetCons(b bool) {
if log.fw == nil {
return
}
log.mu.Lock()
defer log.mu.Unlock()
log.fw.SetCons(b)
}

if log.file != nil {
log.closeFile()
// Close the file associated with the log
// (关闭日志绑定的文件)
func (log *ZinxLoggerCore) closeFile() {
if log.fw != nil {
log.fw.Close()
}

log.file = file
log.out = file

}

func (log *ZinxLoggerCore) SetLogLevel(logLevel int) {
Expand All @@ -481,19 +437,6 @@ func (log *ZinxLoggerCore) checkFileExist(filename string) bool {
return exist
}

func mkdirLog(dir string) (e error) {
_, er := os.Stat(dir)
b := er == nil || os.IsExist(er)
if !b {
if err := os.MkdirAll(dir, 0775); err != nil {
if os.IsPermission(err) {
e = err
}
}
}
return
}

// Convert an integer to a fixed-length string, where the width of the string should be greater than 0
// Ensure that the buffer has sufficient capacity
// (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的
Expand Down
19 changes: 16 additions & 3 deletions zlog/stdzlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ package zlog
zlog.Ins().InfoF()等方法
*/

import "os"

// StdZinxLog creates a global log
var StdZinxLog = NewZinxLog(os.Stderr, "", BitDefault)
var StdZinxLog = NewZinxLog("", BitDefault)

// Flags gets the flags of StdZinxLog
func Flags() int {
Expand All @@ -51,6 +49,21 @@ func SetLogFile(fileDir string, fileName string) {
StdZinxLog.SetLogFile(fileDir, fileName)
}

// SetMaxAge 最大保留天数
func SetMaxAge(ma int) {
StdZinxLog.SetMaxAge(ma)
}

// SetMaxSize 单个日志最大容量 单位:字节
func SetMaxSize(ms int64) {
StdZinxLog.SetMaxSize(ms)
}

// SetCons 同时输出控制台
func SetCons(b bool) {
StdZinxLog.SetCons(b)
}

// SetLogLevel sets the log level of StdZinxLog
func SetLogLevel(logLevel int) {
StdZinxLog.SetLogLevel(logLevel)
Expand Down
Loading

0 comments on commit 575d7ee

Please sign in to comment.