Skip to content

Commit

Permalink
feat : rotate logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dadang committed Nov 30, 2023
1 parent d677640 commit 109f427
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# go-rotate-log
# go-rotate-logs
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/sutantodadang/go-rotate-logs

go 1.21.4
128 changes: 128 additions & 0 deletions rotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package gorotatelogs

import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)

type Config struct {
Directory string

Filename string

MaxSize int // Megabyte MB

BackupName string

UsingTime bool // if true it will add "-" + FormatTime

FormatTime string // if no format it will default using rfc3399
}

type RotateLogsWriter struct {
config Config

mut sync.Mutex

file *os.File
}

func New(config Config) *RotateLogsWriter {

w := &RotateLogsWriter{config: config}

err := w.Rotate()
if err != nil {
return nil
}

return w

}

// write func to satisfy io.writer interface
func (r *RotateLogsWriter) Write(output []byte) (int, error) {

r.mut.Lock()

defer r.mut.Unlock()

return r.file.Write(output)

}

func (r *RotateLogsWriter) Rotate() (err error) {

r.mut.Lock()

defer r.mut.Unlock()

if r.file != nil {

err = r.file.Close()

r.file = nil

if err != nil {
return
}

}

err = os.MkdirAll(r.config.Directory, os.ModePerm)
if err != nil {
return
}

str := strings.Split(r.config.Filename, ".log")

if r.config.UsingTime {

if r.config.FormatTime == "" {
r.config.FormatTime = time.RFC3339
}

r.config.Filename = str[0] + "-" + time.Now().Format(r.config.FormatTime) + ".log"

}

pathFile := filepath.Join(r.config.Directory, r.config.Filename)

dir, err := os.ReadDir(r.config.Filename)
if err != nil {
return
}

info, err := os.Stat(pathFile)
if err == nil {

// if file size over maxsize rename to backup and create new file
if (info.Size() / 1000000) > int64(r.config.MaxSize) {

newStr := strings.Split(r.config.Filename, ".log")

i := strconv.Itoa(len(dir))

err = os.Rename(pathFile, filepath.Join(r.config.Directory, newStr[0]+"-"+r.config.BackupName+"-"+i+".log"))
if err != nil {
return
}

}

} else {
return
}

r.file, err = os.OpenFile(pathFile, os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
return
}

return

}

0 comments on commit 109f427

Please sign in to comment.