Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with the log file is improved. #45

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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