Files
gocryptotrader/log/logger_types.go
Ryan O'Hara-Reid 4e135c9590 logger: reduce go routine generation (#992)
* 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>
2022-10-24 11:46:18 +11:00

108 lines
3.1 KiB
Go

package log
import (
"io"
"sync"
)
const (
timestampFormat = " 02/01/2006 15:04:05 "
spacer = " | "
// DefaultMaxFileSize for logger rotation file
DefaultMaxFileSize int64 = 100
// defaultBufferCapacity has 200kb of memory per buffer, there has been some
// instances where it was 3/4 of this. This size so as to not need a resize.
defaultBufferCapacity = 200000
defaultJobChannelCapacity = 10000
)
var (
logger = Logger{}
// FileLoggingConfiguredCorrectly flag set during config check if file logging meets requirements
fileLoggingConfiguredCorrectly bool
// GlobalLogConfig holds global configuration options for logger
globalLogConfig = &Config{}
// GlobalLogFile hold global configuration options for file logger
globalLogFile = &Rotate{}
jobsPool = &sync.Pool{New: func() interface{} { return new(job) }}
jobsChannel = make(chan *job, defaultJobChannelCapacity)
// Note: Logger state within logFields will be persistent until it's garbage
// collected. This is a little bit more efficient.
logFieldsPool = &sync.Pool{New: func() interface{} { return &logFields{logger: logger} }}
// LogPath system path to store log files in
logPath string
// read/write mutex for logger
mu = &sync.RWMutex{}
)
type job struct {
Writers []io.Writer
fn deferral
Header string
SlName string
Spacer string
TimestampFormat string
ShowLogSystemName bool
Passback chan<- struct{}
}
// Config holds configuration settings loaded from bot config
type Config struct {
Enabled *bool `json:"enabled"`
SubLoggerConfig
LoggerFileConfig *loggerFileConfig `json:"fileSettings,omitempty"`
AdvancedSettings advancedSettings `json:"advancedSettings"`
SubLoggers []SubLoggerConfig `json:"subloggers,omitempty"`
}
type advancedSettings struct {
ShowLogSystemName *bool `json:"showLogSystemName"`
Spacer string `json:"spacer"`
TimeStampFormat string `json:"timeStampFormat"`
Headers headers `json:"headers"`
BypassJobChannelFilledWarning bool `json:"bypassJobChannelFilledWarning"`
}
type headers struct {
Info string `json:"info"`
Warn string `json:"warn"`
Debug string `json:"debug"`
Error string `json:"error"`
}
// SubLoggerConfig holds sub logger configuration settings loaded from bot config
type SubLoggerConfig struct {
Name string `json:"name,omitempty"`
Level string `json:"level"`
Output string `json:"output"`
}
type loggerFileConfig struct {
FileName string `json:"filename,omitempty"`
Rotate *bool `json:"rotate,omitempty"`
MaxSize int64 `json:"maxsize,omitempty"`
}
// Logger each instance of logger settings
type Logger struct {
ShowLogSystemName bool
BypassJobChannelFilledWarning bool
TimestampFormat string
InfoHeader, ErrorHeader, DebugHeader, WarnHeader string
Spacer string
}
// Levels flags for each sub logger type
type Levels struct {
Info, Debug, Warn, Error bool
}
type multiWriterHolder struct {
writers []io.Writer
}