Skip to content

Commit

Permalink
working with the log file is improved. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
microup authored Aug 11, 2023
1 parent cdf629b commit 8ae059a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ proxy:

logger:
dirLog: ./logs/
fileSize: 10000000
fileSizeInMb: 10
apiShowRecords: 50
2 changes: 1 addition & 1 deletion internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Log struct {
// directory where log files are stored
DirLog string `yaml:"dirLog"`
// maximum size of a log file, in megabytes
FileSizeMB uint64 `yaml:"fileSizeInMb"`
FileSizeMB float64 `yaml:"fileSizeInMb"`
// number of records to show when
APIShowRecords uint64 `yaml:"apiShowRecords"`
}
2 changes: 2 additions & 0 deletions internal/types/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
const (
// LengthByte is the length of a byte.
LengthByte = 1024
//
LengthKilobytesInBytes = 1024
// PowX is the power of 10.
PowX = 10
// RoundOne is the value of 0.5.
Expand Down
13 changes: 8 additions & 5 deletions internal/vlog/log_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
func (v *VLog) newFileLog(newFileName string, isNewFileLog bool) error {
if isNewFileLog {
v.MapLastLogRecords = make([]string, 0)

err := v.Close()

if err != nil {
return err
}
Expand All @@ -34,7 +34,7 @@ func (v *VLog) newFileLog(newFileName string, isNewFileLog bool) error {

v.fileLog, err = v.open(newFileName)
if v.fileLog == nil || err != nil {
return err
return fmt.Errorf("%w", err)
}

_, err = v.fileLog.WriteString(v.headerCSV + "\n")
Expand Down Expand Up @@ -110,18 +110,21 @@ func (v *VLog) checkToCreateNewLogFile() error {
if fileInfo == nil {
err = v.newFileLog("", false)
if err != nil {
return err
return fmt.Errorf("%w", err)
}

return nil
}

if uint64(fileInfo.Size()) > v.cfg.FileSizeMB {
fileSizeBytes := fileInfo.Size()
fileSizeMB := float64(fileSizeBytes) / (types.LengthKilobytesInBytes * types.LengthKilobytesInBytes)

if fileSizeMB > v.cfg.FileSizeMB {
oldFileCSV := v.fileLog.Name()

err = v.newFileLog("", false)
if err != nil {
return err
return fmt.Errorf("%w", err)
}

err = core.ArchiveFile(oldFileCSV, fmt.Sprintf("_%s.zip", types.LogFileExtension))
Expand Down
2 changes: 1 addition & 1 deletion internal/vlog/vlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func New(cfg *config.Log) *VLog {
func (v *VLog) Init() error {
err := v.newFileLog("", true)
if err != nil {
return err
return fmt.Errorf("%w", err)
}

return nil
Expand Down

0 comments on commit 8ae059a

Please sign in to comment.