2020-03-09 22:11:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package log exports logging related types and functions.
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
stdlog "log"
|
2020-03-12 22:31:10 +08:00
|
|
|
"os"
|
2020-03-09 22:11:16 +08:00
|
|
|
)
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// NewLogger creates and returns a new instance of Logger.
|
2020-03-09 22:11:16 +08:00
|
|
|
func NewLogger(out io.Writer) *Logger {
|
|
|
|
return &Logger{
|
|
|
|
stdlog.New(out, "", stdlog.Ldate|stdlog.Ltime|stdlog.Lmicroseconds|stdlog.LUTC),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Logger is a wrapper object around log.Logger from the standard library.
|
|
|
|
// It supports logging at various log levels.
|
2020-03-09 22:11:16 +08:00
|
|
|
type Logger struct {
|
|
|
|
*stdlog.Logger
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Debug logs a message at Debug level.
|
2020-03-12 22:31:10 +08:00
|
|
|
func (l *Logger) Debug(format string, args ...interface{}) {
|
|
|
|
format = "DEBUG: " + format
|
|
|
|
l.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Info logs a message at Info level.
|
2020-03-09 22:11:16 +08:00
|
|
|
func (l *Logger) Info(format string, args ...interface{}) {
|
|
|
|
format = "INFO: " + format
|
|
|
|
l.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Warn logs a message at Warning level.
|
2020-03-09 22:11:16 +08:00
|
|
|
func (l *Logger) Warn(format string, args ...interface{}) {
|
|
|
|
format = "WARN: " + format
|
|
|
|
l.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Error logs a message at Error level.
|
2020-03-09 22:11:16 +08:00
|
|
|
func (l *Logger) Error(format string, args ...interface{}) {
|
|
|
|
format = "ERROR: " + format
|
|
|
|
l.Printf(format, args...)
|
|
|
|
}
|
2020-03-12 22:31:10 +08:00
|
|
|
|
2020-03-14 11:53:31 +08:00
|
|
|
// Fatal logs a message at Fatal level
|
|
|
|
// and process will exit with status set to 1.
|
2020-03-12 22:31:10 +08:00
|
|
|
func (l *Logger) Fatal(format string, args ...interface{}) {
|
|
|
|
format = "FATAL: " + format
|
|
|
|
l.Printf(format, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|