Skip to content

Commit 575d7ee

Browse files
author
MacBookAirM2
committed
add log storage
1 parent a770e24 commit 575d7ee

File tree

9 files changed

+110
-120
lines changed

9 files changed

+110
-120
lines changed

README-CN.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,15 @@ recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
264264
```json
265265
{
266266
"Name":"zinx v-0.10 demoApp",
267-
"Host":"127.0.0.1",
268-
"TCPPort":7777,
267+
"Host":"0.0.0.0",
268+
"TCPPort":9090,
269269
"MaxConn":3,
270270
"WorkerPoolSize":10,
271271
"LogDir": "./mylog",
272-
"LogFile":"zinx.log",
273-
"LogIsolationLevel": 0
272+
"LogFile":"app.log",
273+
"LogSaveDays":15,
274+
"LogCons": true,
275+
"LogIsolationLevel":0
274276
}
275277
```
276278

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,14 @@ recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
261261
```json
262262
{
263263
"Name":"zinx v-0.10 demoApp",
264-
"Host":"127.0.0.1",
265-
"TCPPort":7777,
264+
"Host":"0.0.0.0",
265+
"TCPPort":9090,
266266
"MaxConn":3,
267267
"WorkerPoolSize":10,
268268
"LogDir": "./mylog",
269-
"LogFile":"zinx.log",
269+
"LogFile":"app.log",
270+
"LogSaveDays":15,
271+
"LogCons": true,
270272
"LogIsolationLevel":0
271273
}
272274
```

conf/zinx.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Name":"zinx v-0.10 demoApp",
3+
"Host":"0.0.0.0",
4+
"TCPPort":9090,
5+
"MaxConn":3,
6+
"WorkerPoolSize":10,
7+
"LogDir": "./mylog",
8+
"LogFile":"app.log",
9+
"LogSaveDays":15,
10+
"LogCons": true,
11+
"LogIsolationLevel":0
12+
}

examples/zinx_decoder/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func DoConnectionLost(conn ziface.IConnection) {
1717
}
1818

1919
func main() {
20+
//zlog.SetLogFile("./logs", "app.log")
2021
s := znet.NewServer()
2122

2223
s.SetOnConnStart(DoConnectionBegin)

examples/zinx_logger/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/aceld/zinx/ziface"
6+
"github.com/aceld/zinx/zlog"
67
"github.com/aceld/zinx/znet"
78
"time"
89
)
@@ -43,6 +44,6 @@ func (t *TestRouter) PostHandle(req ziface.IRequest) {
4344
func main() {
4445
s := znet.NewServer()
4546
s.AddRouter(1, &TestRouter{})
46-
//zlog.SetLogger(new(MyLogger))
47+
zlog.SetLogger(new(MyLogger))
4748
s.Serve()
4849
}

zconf/globalobj.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const (
2424
)
2525

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

69+
LogSaveDays int // 日志最大保留天数
70+
LogFileSize int64 // 日志单个日志最大容量 默认 64MB,单位:字节,记得一定要换算成MB(1024 * 1024)
71+
LogCons bool // 日志标准输出 默认 false
72+
6973
// 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.
7074
//日志隔离级别 -- 0:全开 1:关debug 2:关debug/info 3:关debug/info/warn ...
7175
LogIsolationLevel int
@@ -85,7 +89,7 @@ type Config struct {
8589
}
8690

8791
/*
88-
Define a global object.(定义一个全局的对象)
92+
Define a global object.(定义一个全局的对象)
8993
*/
9094
var GlobalObject *Config
9195

@@ -154,14 +158,21 @@ func (g *Config) HeartbeatMaxDuration() time.Duration {
154158
func (g *Config) InitLogConfig() {
155159
if g.LogFile != "" {
156160
zlog.SetLogFile(g.LogDir, g.LogFile)
161+
zlog.SetCons(g.LogCons)
162+
}
163+
if g.LogSaveDays > 0 {
164+
zlog.SetMaxAge(g.LogSaveDays)
165+
}
166+
if g.LogFileSize > 0 {
167+
zlog.SetMaxSize(g.LogFileSize)
157168
}
158169
if g.LogIsolationLevel > zlog.LogDebug {
159170
zlog.SetLogLevel(g.LogIsolationLevel)
160171
}
161172
}
162173

163174
/*
164-
init, set default value
175+
init, set default value
165176
*/
166177
func init() {
167178
pwd, err := os.Getwd()

zlog/logger_core.go

+37-94
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package zlog
1818
import (
1919
"bytes"
2020
"fmt"
21-
"io"
21+
"github.com/aceld/zinx/zutils"
2222
"os"
23+
"path/filepath"
2324
"runtime"
24-
"strings"
2525
"sync"
2626
"time"
2727
)
@@ -75,17 +75,9 @@ type ZinxLoggerCore struct {
7575
// log tag bit (日志标记位)
7676
flag int
7777

78-
// the file descriptor for log output
79-
// (日志输出的文件描述符)
80-
out io.Writer
81-
8278
// the output buffer (输出的缓冲区)
8379
buf bytes.Buffer
8480

85-
// the output file currently bound to the log
86-
// (当前日志绑定的输出文件)
87-
file *os.File
88-
8981
// log isolation level
9082
// (日志隔离级别)
9183
isolationLevel int
@@ -94,21 +86,7 @@ type ZinxLoggerCore struct {
9486
// (获取日志文件名和代码上述的runtime.Call 的函数调用层数)
9587
calldDepth int
9688

97-
// log file name
98-
// (日志文件名称)
99-
fileName string
100-
101-
// log file directory
102-
// (日志文件目录)
103-
fileDir string
104-
105-
// last write date
106-
// (上次写入日期)
107-
lastWriteDate int
108-
109-
// file swap lock
110-
// (文件交换锁)
111-
fsLock sync.Mutex
89+
fw *zutils.Writer
11290

11391
onLogHook func([]byte)
11492
}
@@ -120,11 +98,11 @@ out: The file io for standard output
12098
prefix: The prefix of the log
12199
flag: The flag of the log header information
122100
*/
123-
func NewZinxLog(out io.Writer, prefix string, flag int) *ZinxLoggerCore {
101+
func NewZinxLog(prefix string, flag int) *ZinxLoggerCore {
124102

125103
// 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
126104
// (默认 debug打开, calledDepth深度为2,ZinxLogger对象调用日志打印方法最多调用两层到达output函数)
127-
zlog := &ZinxLoggerCore{out: out, prefix: prefix, flag: flag, file: nil, isolationLevel: 0, calldDepth: 2}
105+
zlog := &ZinxLoggerCore{prefix: prefix, flag: flag, isolationLevel: 0, calldDepth: 2}
128106

129107
// 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)
130108
// (设置log对象 回收资源 析构方法(不设置也可以,go的Gc会自动回收,强迫症没办法))
@@ -245,23 +223,19 @@ func (log *ZinxLoggerCore) OutPut(level int, s string) error {
245223
log.buf.WriteByte('\n')
246224
}
247225

248-
log.updateOutputFile()
249-
250226
var err error
251-
if log.file == nil {
227+
if log.fw == nil {
252228
// if log file is not set, output to console
253229
_, _ = os.Stderr.Write(log.buf.Bytes())
254230
} else {
255231
// write the filled buffer to IO output
256-
_, err = log.out.Write(log.buf.Bytes())
232+
_, err = log.fw.Write(log.buf.Bytes())
257233
}
258234

259235
if log.onLogHook != nil {
260236
log.onLogHook(log.buf.Bytes())
261237
}
262-
263238
return err
264-
265239
}
266240

267241
func (log *ZinxLoggerCore) verifyLogIsolation(logLevel int) bool {
@@ -407,66 +381,48 @@ func (log *ZinxLoggerCore) SetPrefix(prefix string) {
407381
// SetLogFile sets the log file output
408382
// (设置日志文件输出)
409383
func (log *ZinxLoggerCore) SetLogFile(fileDir string, fileName string) {
410-
log.fileDir = fileDir
411-
log.fileName = fileName
412-
}
413-
414-
// Close the file associated with the log
415-
// (关闭日志绑定的文件)
416-
func (log *ZinxLoggerCore) closeFile() {
417-
if log.file != nil {
418-
_ = log.file.Close()
419-
log.file = nil
420-
log.out = os.Stderr
384+
if log.fw != nil {
385+
log.fw.Close()
421386
}
387+
log.fw = zutils.New(filepath.Join(fileDir, fileName))
422388
}
423389

424-
// update the output file for the log
425-
// (更新文件输出)
426-
func (log *ZinxLoggerCore) updateOutputFile() {
427-
428-
var file *os.File
429-
430-
yearDay := time.Now().YearDay()
431-
432-
if log.lastWriteDate == yearDay && log.file != nil {
390+
// SetMaxAge 最大保留天数
391+
func (log *ZinxLoggerCore) SetMaxAge(ma int) {
392+
if log.fw == nil {
433393
return
434394
}
395+
log.mu.Lock()
396+
defer log.mu.Unlock()
397+
log.fw.SetMaxAge(ma)
398+
}
435399

436-
log.fsLock.Lock()
437-
defer log.fsLock.Unlock()
438-
439-
if log.lastWriteDate == yearDay && log.file != nil {
400+
// SetMaxSize 单个日志最大容量 单位:字节
401+
func (log *ZinxLoggerCore) SetMaxSize(ms int64) {
402+
if log.fw == nil {
440403
return
441404
}
405+
log.mu.Lock()
406+
defer log.mu.Unlock()
407+
log.fw.SetMaxSize(ms)
408+
}
442409

443-
log.lastWriteDate = yearDay
444-
445-
// create the log directory
446-
_ = mkdirLog(log.fileDir)
447-
448-
// define the log file name = log file name . date suffix . log
449-
// supported file name formats:
450-
// 1. "zinx.log"
451-
// 2. "zinx"
452-
// 3. "zinx.zinx.zinx.log"
453-
realFileNameSlice := strings.Split(log.fileName, ".log")
454-
realFileName := realFileNameSlice[0]
455-
newDailyFile := log.fileDir + "/" + realFileName + "." + time.Now().Format("20060102") + ".log"
456-
457-
if log.checkFileExist(newDailyFile) {
458-
file, _ = os.OpenFile(newDailyFile, os.O_APPEND|os.O_RDWR, 0644) // rw-r--r--
459-
} else {
460-
file, _ = os.OpenFile(newDailyFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
410+
// SetCons 同时输出控制台
411+
func (log *ZinxLoggerCore) SetCons(b bool) {
412+
if log.fw == nil {
413+
return
461414
}
415+
log.mu.Lock()
416+
defer log.mu.Unlock()
417+
log.fw.SetCons(b)
418+
}
462419

463-
if log.file != nil {
464-
log.closeFile()
420+
// Close the file associated with the log
421+
// (关闭日志绑定的文件)
422+
func (log *ZinxLoggerCore) closeFile() {
423+
if log.fw != nil {
424+
log.fw.Close()
465425
}
466-
467-
log.file = file
468-
log.out = file
469-
470426
}
471427

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

484-
func mkdirLog(dir string) (e error) {
485-
_, er := os.Stat(dir)
486-
b := er == nil || os.IsExist(er)
487-
if !b {
488-
if err := os.MkdirAll(dir, 0775); err != nil {
489-
if os.IsPermission(err) {
490-
e = err
491-
}
492-
}
493-
}
494-
return
495-
}
496-
497440
// Convert an integer to a fixed-length string, where the width of the string should be greater than 0
498441
// Ensure that the buffer has sufficient capacity
499442
// (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的

zlog/stdzlog.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ package zlog
2121
zlog.Ins().InfoF()等方法
2222
*/
2323

24-
import "os"
25-
2624
// StdZinxLog creates a global log
27-
var StdZinxLog = NewZinxLog(os.Stderr, "", BitDefault)
25+
var StdZinxLog = NewZinxLog("", BitDefault)
2826

2927
// Flags gets the flags of StdZinxLog
3028
func Flags() int {
@@ -51,6 +49,21 @@ func SetLogFile(fileDir string, fileName string) {
5149
StdZinxLog.SetLogFile(fileDir, fileName)
5250
}
5351

52+
// SetMaxAge 最大保留天数
53+
func SetMaxAge(ma int) {
54+
StdZinxLog.SetMaxAge(ma)
55+
}
56+
57+
// SetMaxSize 单个日志最大容量 单位:字节
58+
func SetMaxSize(ms int64) {
59+
StdZinxLog.SetMaxSize(ms)
60+
}
61+
62+
// SetCons 同时输出控制台
63+
func SetCons(b bool) {
64+
StdZinxLog.SetCons(b)
65+
}
66+
5467
// SetLogLevel sets the log level of StdZinxLog
5568
func SetLogLevel(logLevel int) {
5669
StdZinxLog.SetLogLevel(logLevel)

0 commit comments

Comments
 (0)