mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
* logger: reduce go routine generation * logger: shift most of processing and prep work to the worker pool, add pool for fields because each log we are pushing the struct to the heap, has better segregation now and includes a buffer in scope instead of relying on a pool * logger: shift fmt package calls to worker pool * logger: conform tests to new design * linter: fix issues * Update log/logger_test.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update log/logger_test.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * UN-GLORIOUS: nits * logger: Handle config variable * logger: NITERINOS BY GLORIOUS CODE * logger: revert * glorious: nits * Panic at the disco: fix * Panic at the disco: fix * logger: make sure logger closed and job channel emptied on start up error * fix tests * logger: reduce globals * logger: finished reduces globals, reduce workers to one too keep everything in line. * logger: remove comments * logger/exhchange: linter issues * db/test: fix linter * logger: add tests shift wait before unlock * logger: consolidate worker code; fix linter issue and make sure we can sustain writing for external testing. * logger: fix race and warn for conflict in config * logger: fix name and add to tests * logger: remove zero value field * glorious: panic fix and removal of code * logger: reinstate channels in close * logger: shift reinstate processing to SetupGlobalLogger * logger: segregate config.json from internal log.Config * logger: fix silly mistake that is silly * engine: Add protection for nil issues and implement new constructor in tests * logger: Force singular mutex usage throughout package, throw away funcs that are not used outside of this package, unexport a bunch. Fix tests. * logger: actually set advanced settings * Update log/loggers.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update log/loggers.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update log/loggers.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update log/loggers.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update log/logger_multiwriter.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * glorious: nits * logger: test issue when not purging temp file and contents * loggertest: add more protections for the panics * linter: fix * glorious: nits * cleanup * logger: linter fix * linter: fix(?) :/ * linter: revert change * linter: fix Co-authored-by: Scott <gloriousCode@users.noreply.github.com> Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
130 lines
2.4 KiB
Go
130 lines
2.4 KiB
Go
package log
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common/file"
|
|
)
|
|
|
|
var (
|
|
errExceedsMaxFileSize = errors.New("exceeds max file size")
|
|
errFileNameIsEmpty = errors.New("filename is empty")
|
|
)
|
|
|
|
// Write implementation to satisfy io.Writer handles length check and rotation
|
|
func (r *Rotate) Write(output []byte) (n int, err error) {
|
|
outputLen := int64(len(output))
|
|
if outputLen > r.maxSize() {
|
|
return 0, fmt.Errorf(
|
|
"write length %v %w %v", outputLen, errExceedsMaxFileSize, r.maxSize(),
|
|
)
|
|
}
|
|
|
|
if r.output == nil {
|
|
err = r.openOrCreateFile(outputLen)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
if *r.Rotate {
|
|
if r.size+outputLen > r.maxSize() {
|
|
err = r.rotateFile()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
}
|
|
|
|
n, err = r.output.Write(output)
|
|
r.size += int64(n)
|
|
return n, err
|
|
}
|
|
|
|
func (r *Rotate) openOrCreateFile(n int64) error {
|
|
logFile := filepath.Join(GetLogPath(), r.FileName)
|
|
info, err := os.Stat(logFile)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return r.openNew()
|
|
}
|
|
return fmt.Errorf("error opening log file info: %s", err)
|
|
}
|
|
|
|
if *r.Rotate {
|
|
if info.Size()+n >= r.maxSize() {
|
|
return r.rotateFile()
|
|
}
|
|
}
|
|
|
|
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
return r.openNew()
|
|
}
|
|
|
|
r.output = file
|
|
r.size = info.Size()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Rotate) openNew() error {
|
|
if r.FileName == "" {
|
|
return fmt.Errorf("cannot open new file: %w", errFileNameIsEmpty)
|
|
}
|
|
name := filepath.Join(GetLogPath(), r.FileName)
|
|
_, err := os.Stat(name)
|
|
|
|
if err == nil {
|
|
timestamp := time.Now().Format("2006-01-02T15-04-05")
|
|
newName := filepath.Join(GetLogPath(), timestamp+"-"+r.FileName)
|
|
|
|
err = file.Move(name, newName)
|
|
if err != nil {
|
|
return fmt.Errorf("can't rename log file: %s", err)
|
|
}
|
|
}
|
|
|
|
file, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
|
|
if err != nil {
|
|
return fmt.Errorf("can't open new logfile: %s", err)
|
|
}
|
|
|
|
r.output = file
|
|
r.size = 0
|
|
return nil
|
|
}
|
|
|
|
func (r *Rotate) close() (err error) {
|
|
if r.output == nil {
|
|
return nil
|
|
}
|
|
err = r.output.Close()
|
|
r.output = nil
|
|
return err
|
|
}
|
|
|
|
// Close handler for open file
|
|
func (r *Rotate) Close() error {
|
|
return r.close()
|
|
}
|
|
|
|
func (r *Rotate) rotateFile() (err error) {
|
|
err = r.close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.openNew()
|
|
}
|
|
|
|
func (r *Rotate) maxSize() int64 {
|
|
if r.MaxSize == 0 {
|
|
return defaultMaxSize * megabyte
|
|
}
|
|
return r.MaxSize * megabyte
|
|
}
|