1
0

A custom logger was removed.

This commit is contained in:
Alexander Andreev 2023-05-25 02:56:57 +04:00
parent c5faef303b
commit a7972b8e13
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -1,96 +0,0 @@
package logging
import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
type Logger struct {
sync.Mutex
file io.WriteCloser
}
// New creates a Logger instance with a given filename
func New(path string) (*Logger, error) {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return nil, errors.Wrap(err, "failed to open log file")
}
return &Logger{file: f}, nil
}
func (l *Logger) Reopen(path string) error {
l.Lock()
defer l.Unlock()
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return err
}
l.file.Close()
l.file = f
return nil
}
func (l *Logger) Println(v ...interface{}) {
l.Lock()
defer l.Unlock()
nowStr := time.Now().UTC().Format(time.RFC3339)
fmt.Fprintln(l.file, nowStr, v)
}
func (l *Logger) Printf(format string, v ...interface{}) {
l.Lock()
defer l.Unlock()
// Ensure a new line will be written
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
nowStr := time.Now().UTC().Format(time.RFC3339)
fmt.Fprintf(l.file, nowStr+" "+format, v...)
}
func (l *Logger) Fatalln(v ...interface{}) {
l.Lock()
nowStr := time.Now().UTC().Format(time.RFC3339)
fmt.Fprintln(l.file, nowStr, v)
l.file.Close()
os.Exit(1)
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.Lock()
// Ensure a new line will be written
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
nowStr := time.Now().UTC().Format(time.RFC3339)
fmt.Fprintf(l.file, nowStr+" "+format, v...)
l.file.Close()
os.Exit(1)
}
func (l *Logger) Close() error {
return l.file.Close()
}