Skip to content

Commit

Permalink
fix readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gkzy committed Nov 24, 2022
1 parent 29e2bb5 commit ab0aba8
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 4 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,73 @@ func main() {

---
## 2. 同时写日志到文件

### 2.1 使用 `logx``FileWriter`

```go
package main

import (
"io"
"os"
"time"

"github.com/zituocn/logx"
)

func init() {
logx.SetWriter(io.MultiWriter(
os.Stdout,
logx.NewFileWriter(logx.FileOptions{
StorageType: logx.StorageTypeDay,
MaxDay: 7,
Dir: "./logs",
Prefix: "web",
}))).SetColor(false)
}

func main() {
logx.Infof("这是一个字串: %s", time.Now())
logx.Debugf("这是一个对象: %#v", time.Now())
}
```
**代码说明**

1. `logx.SetWriter` 时,使用了` io.MultiWriter`
2. 把日志输出到了`os.Stdout``logx.FileWriter`
3. 设置不显示颜色 `SetColor(false)`


### 2.2 自己设计一个io.Writer


可以自己设计一个实现了 `io.Writer` ,由 `logx.SetWriter` 传入,可以是文件或网络。


```go
package main

import (
"github.com/zituocn/logx"
"io"
"os"
"time"
)

func init() {
myWriter, err := os.OpenFile("./my_log_file.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
panic(err)
}
logx.SetWriter(io.MultiWriter(
os.Stdout,
myWriter,
)).SetColor(false)
}

func main() {
logx.Infof("这是一个字串: %s", time.Now())
logx.Debugf("这是一个对象: %#v", time.Now())
}

```
80 changes: 76 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package logx

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
)

const (
nextSecond = 3600
)

type StorageType int

const (
Expand Down Expand Up @@ -58,7 +63,7 @@ type FileOptions struct {
Prefix string

// date 日期
data string
date string
}

// FileWriter 文件存储实现
Expand All @@ -75,20 +80,22 @@ func NewFileWriter(opts ...FileOptions) *FileWriter {
FileOptions: opt,
mu: &sync.Mutex{},
}
w.initFile()
go w.clearLogFile()
go w.startTimer()
return w
}

func (w *FileWriter) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
w.initFile()
return w.file.Write(p)
}

func (w *FileWriter) initFile() {
now := time.Now()
date := now.Format(w.StorageType.getFileFormat())
if w.data != date && w.file != nil {
if w.date != date && w.file != nil {
_ = w.file.Close()
w.file = nil
}
Expand All @@ -104,8 +111,73 @@ func (w *FileWriter) initFile() {
panic(errO)
}
w.file = file
w.data = date
w.date = date
}
}

func (w *FileWriter) startTimer() {
now := time.Now()
nextTime := now.Add(nextSecond * time.Second)
second := time.Duration(nextTime.Sub(now).Seconds())
w.timer(second)
}

func (w *FileWriter) timer(second time.Duration) {
timer := time.NewTicker(second * time.Second)
for {
select {
case <-timer.C:
{
w.clearLogFile()
nextTimer := time.NewTicker(nextSecond * time.Second)
for {
select {
case <-nextTimer.C:
w.startTimer()
return
}
}
}
}
}
}

func (w *FileWriter) clearLogFile() {
now := time.Now()
files := getDirFiles(w.Dir)
for _, item := range files {
modTime := item.ModTime
flag := modTime.Add(time.Hour * 24 * time.Duration(w.MaxDay-1)).Before(now)
if flag {
_ = os.Remove(w.Dir + item.Name)
}
}
}

// FileInfo file info
type FileInfo struct {
Name string
ModTime time.Time
Size int64
}

// getDirFiles return log files
func getDirFiles(path string) (files []*FileInfo) {
dir, err := ioutil.ReadDir(path)
if err != nil {
return
}
files = make([]*FileInfo, 0)
for _, fi := range dir {
if !fi.IsDir() {
files = append(files, &FileInfo{
Name: fi.Name(),
ModTime: fi.ModTime(),
Size: fi.Size(),
})
}
}
return
}

func prepareFileWriterOption(opts []FileOptions) FileOptions {
Expand Down

0 comments on commit ab0aba8

Please sign in to comment.