Suppress log if needed

This commit is contained in:
Igor Chubin 2022-12-04 10:35:58 +01:00
parent 5d86d36b7e
commit a6f2844c67
3 changed files with 34 additions and 15 deletions

View file

@ -1,2 +1,4 @@
srv: srv.go internal/*/*.go srv: srv.go internal/*/*.go
go build -o srv ./ go build -o srv ./
test:
go test ./

View file

@ -32,6 +32,9 @@ type logEntry struct {
// NewRequestLogger returns a new RequestLogger for the specified log file. // NewRequestLogger returns a new RequestLogger for the specified log file.
// Flush logging entries after period of time. // Flush logging entries after period of time.
//
// If filename is empty, no log will be written, and all logging entries
// will be silently dropped.
func NewRequestLogger(filename string, period time.Duration) *RequestLogger { func NewRequestLogger(filename string, period time.Duration) *RequestLogger {
return &RequestLogger{ return &RequestLogger{
buf: map[logEntry]int{}, buf: map[logEntry]int{},
@ -74,23 +77,25 @@ func (rl *RequestLogger) flush() error {
return nil return nil
} }
// Generate log output. if rl.filename != "" {
output := "" // Generate log output.
for k, hitsNumber := range rl.buf { output := ""
output += fmt.Sprintf("%s %3d %s\n", time.Now().Format(time.RFC3339), hitsNumber, k.String()) for k, hitsNumber := range rl.buf {
} output += fmt.Sprintf("%s %3d %s\n", time.Now().Format(time.RFC3339), hitsNumber, k.String())
}
// Open log file. // Open log file.
f, err := os.OpenFile(rl.filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) f, err := os.OpenFile(rl.filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
// Save output to log file. // Save output to log file.
_, err = f.Write([]byte(output)) _, err = f.Write([]byte(output))
if err != nil { if err != nil {
return err return err
}
} }
// Flush buffer. // Flush buffer.

View file

@ -19,6 +19,8 @@ type LogSuppressor struct {
// NewLogSuppressor creates a new LogSuppressor for specified // NewLogSuppressor creates a new LogSuppressor for specified
// filename and lines to be suppressed. // filename and lines to be suppressed.
//
// If filename is empty, log entries will be printed to stderr.
func NewLogSuppressor(filename string, suppress []string, linePrefix string) *LogSuppressor { func NewLogSuppressor(filename string, suppress []string, linePrefix string) *LogSuppressor {
return &LogSuppressor{ return &LogSuppressor{
filename: filename, filename: filename,
@ -29,6 +31,9 @@ func NewLogSuppressor(filename string, suppress []string, linePrefix string) *Lo
// Open opens log file. // Open opens log file.
func (ls *LogSuppressor) Open() error { func (ls *LogSuppressor) Open() error {
if ls.filename == "" {
return nil
}
var err error var err error
ls.logFile, err = os.OpenFile(ls.filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) ls.logFile, err = os.OpenFile(ls.filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
return err return err
@ -36,6 +41,9 @@ func (ls *LogSuppressor) Open() error {
// Close closes log file. // Close closes log file.
func (ls *LogSuppressor) Close() error { func (ls *LogSuppressor) Close() error {
if ls.filename == "" {
return nil
}
return ls.logFile.Close() return ls.logFile.Close()
} }
@ -46,6 +54,10 @@ func (ls *LogSuppressor) Write(p []byte) (n int, err error) {
output string output string
) )
if ls.filename == "" {
return os.Stdin.Write(p)
}
ls.m.Lock() ls.m.Lock()
defer ls.m.Unlock() defer ls.m.Unlock()