NewLogger() was renamed to New(). Removed toStdout and all of its logic. Use composition for sync.Mutex.
This commit is contained in:
parent
17ebe18b8b
commit
1e07928b2c
@ -12,23 +12,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
file io.WriteCloser
|
sync.Mutex
|
||||||
toStdout bool
|
file io.WriteCloser
|
||||||
mut sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLogger creates a Logger instance with given filename and
|
// New creates a Logger instance with a given filename
|
||||||
// toStdout tells wether to write to Stdout as well or not.
|
func New(path string) (*Logger, error) {
|
||||||
func NewLogger(path string, toStdout bool) (*Logger, error) {
|
|
||||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to open log file")
|
return nil, errors.Wrap(err, "failed to open log file")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Logger{file: f, toStdout: toStdout}, nil
|
return &Logger{file: f}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Reopen(path string) error {
|
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)
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -40,21 +41,17 @@ func (l *Logger) Reopen(path string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Println(v ...interface{}) {
|
func (l *Logger) Println(v ...interface{}) {
|
||||||
l.mut.Lock()
|
l.Lock()
|
||||||
defer l.mut.Unlock()
|
defer l.Unlock()
|
||||||
|
|
||||||
nowStr := time.Now().UTC().Format(time.RFC3339)
|
nowStr := time.Now().UTC().Format(time.RFC3339)
|
||||||
|
|
||||||
fmt.Fprintln(l.file, nowStr, v)
|
fmt.Fprintln(l.file, nowStr, v)
|
||||||
|
|
||||||
if l.toStdout {
|
|
||||||
fmt.Println(v...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||||
l.mut.Lock()
|
l.Lock()
|
||||||
defer l.mut.Unlock()
|
defer l.Unlock()
|
||||||
|
|
||||||
// Ensure a new line will be written
|
// Ensure a new line will be written
|
||||||
if !strings.HasSuffix(format, "\n") {
|
if !strings.HasSuffix(format, "\n") {
|
||||||
@ -65,28 +62,21 @@ func (l *Logger) Printf(format string, v ...interface{}) {
|
|||||||
|
|
||||||
fmt.Fprintf(l.file, nowStr+" "+format, v...)
|
fmt.Fprintf(l.file, nowStr+" "+format, v...)
|
||||||
|
|
||||||
if l.toStdout {
|
|
||||||
fmt.Printf(format, v...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Fatalln(v ...interface{}) {
|
func (l *Logger) Fatalln(v ...interface{}) {
|
||||||
l.mut.Lock()
|
l.Lock()
|
||||||
|
|
||||||
nowStr := time.Now().UTC().Format(time.RFC3339)
|
nowStr := time.Now().UTC().Format(time.RFC3339)
|
||||||
|
|
||||||
fmt.Fprintln(l.file, nowStr, v)
|
fmt.Fprintln(l.file, nowStr, v)
|
||||||
|
|
||||||
if l.toStdout {
|
|
||||||
fmt.Println(v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
l.file.Close()
|
l.file.Close()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
||||||
l.mut.Lock()
|
l.Lock()
|
||||||
|
|
||||||
// Ensure a new line will be written
|
// Ensure a new line will be written
|
||||||
if !strings.HasSuffix(format, "\n") {
|
if !strings.HasSuffix(format, "\n") {
|
||||||
@ -97,10 +87,6 @@ func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|||||||
|
|
||||||
fmt.Fprintf(l.file, nowStr+" "+format, v...)
|
fmt.Fprintf(l.file, nowStr+" "+format, v...)
|
||||||
|
|
||||||
if l.toStdout {
|
|
||||||
fmt.Printf(format, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
l.file.Close()
|
l.file.Close()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user