2016-04-07 18:38:24 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2017-02-09 15:46:27 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-07 18:38:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log *Logger
|
|
|
|
|
|
|
|
// Logger ...
|
|
|
|
type Logger struct {
|
|
|
|
*logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log = New()
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Instance returns the underlying logger instance
|
2016-04-07 18:38:24 +00:00
|
|
|
func Instance() *logrus.Logger {
|
|
|
|
return log.Logger
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// SetLevel sets the level of the standard logger
|
|
|
|
func SetLevel(v string) {
|
|
|
|
log.SetLevel(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFormat sets the format of the standard logger
|
|
|
|
func SetFormat(v string) {
|
|
|
|
log.SetFormat(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the output of the standard logger
|
|
|
|
func SetOutput(v string) {
|
|
|
|
log.SetOutput(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug logs a message at level Debug on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Debug(v ...interface{}) {
|
|
|
|
log.Debug(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Debugf logs a message at level Debug on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
log.Debugf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Debugln logs a message at level Debug on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Debugln(v ...interface{}) {
|
|
|
|
log.Debugln(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Error loggs a message at level Error on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Error(v ...interface{}) {
|
|
|
|
log.Error(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Errorf loggs a message at level Error on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
log.Errorf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Errorln loggs a message at level Error on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Errorln(v ...interface{}) {
|
|
|
|
log.Errorln(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Fatal loggs a message at level Fatal on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Fatal(v ...interface{}) {
|
|
|
|
log.Fatal(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Fatalf loggs a message at level Fatal on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
|
|
log.Fatalf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Fatalln loggs a message at level Fatal on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Fatalln(v ...interface{}) {
|
|
|
|
log.Fatalln(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Info loggs a message at level Info on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Info(v ...interface{}) {
|
|
|
|
log.Info(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Infof loggs a message at level Info on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
log.Infof(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Infoln loggs a message at level Info on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Infoln(v ...interface{}) {
|
|
|
|
log.Infoln(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Panic loggs a message at level Panic on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Panic(v ...interface{}) {
|
|
|
|
log.Panic(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Panicf loggs a message at level Panic on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Panicf(format string, v ...interface{}) {
|
|
|
|
log.Panicf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Panicln loggs a message at level Panic on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Panicln(v ...interface{}) {
|
|
|
|
log.Panicln(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Print loggs a message at level Print on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Print(v ...interface{}) {
|
|
|
|
log.Print(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Printf loggs a message at level Print on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Printf(format string, v ...interface{}) {
|
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Println loggs a message at level Print on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Println(v ...interface{}) {
|
|
|
|
log.Println(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Warn loggs a message at level Warn on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Warn(v ...interface{}) {
|
|
|
|
log.Warn(v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Warnf loggs a message at level Warn on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
log.Warnf(format, v...)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// Warnln loggs a message at level Warn on the standard logger.
|
2016-04-07 18:38:24 +00:00
|
|
|
func Warnln(v ...interface{}) {
|
|
|
|
log.Warnln(v...)
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:59 +00:00
|
|
|
// WithPrefix prepares a log entry with a prefix.
|
|
|
|
func WithPrefix(value interface{}) *logrus.Entry {
|
|
|
|
return log.WithField("prefix", value)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// WithField prepares a log entry with a single data field.
|
2016-04-07 18:38:24 +00:00
|
|
|
func WithField(key string, value interface{}) *logrus.Entry {
|
|
|
|
return log.WithField(key, value)
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// WithFields prepares a log entry with multiple data fields.
|
2016-04-07 18:38:24 +00:00
|
|
|
func WithFields(fields map[string]interface{}) *logrus.Entry {
|
|
|
|
return log.WithFields(fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Logger instance.
|
|
|
|
func New() *Logger {
|
|
|
|
|
|
|
|
return &Logger{
|
|
|
|
&logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Level: logrus.ErrorLevel,
|
|
|
|
Hooks: logrus.LevelHooks{},
|
|
|
|
Formatter: &TextFormatter{
|
|
|
|
TimestampFormat: time.RFC3339,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// SetLevel sets the logging level of the logger instance.
|
2016-04-07 18:38:24 +00:00
|
|
|
func (l *Logger) SetLevel(v string) {
|
|
|
|
switch v {
|
|
|
|
case "debug", "DEBUG":
|
|
|
|
l.Level = logrus.DebugLevel
|
|
|
|
case "info", "INFO":
|
|
|
|
l.Level = logrus.InfoLevel
|
|
|
|
case "warning", "WARNING":
|
|
|
|
l.Level = logrus.WarnLevel
|
|
|
|
case "error", "ERROR":
|
|
|
|
l.Level = logrus.ErrorLevel
|
|
|
|
case "fatal", "FATAL":
|
|
|
|
l.Level = logrus.FatalLevel
|
|
|
|
case "panic", "PANIC":
|
|
|
|
l.Level = logrus.PanicLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// SetFormat sets the logging format of the logger instance.
|
2016-04-07 18:38:24 +00:00
|
|
|
func (l *Logger) SetFormat(v string) {
|
|
|
|
switch v {
|
|
|
|
case "json":
|
|
|
|
l.Formatter = &logrus.JSONFormatter{}
|
|
|
|
case "text":
|
|
|
|
l.Formatter = &TextFormatter{
|
|
|
|
TimestampFormat: time.RFC3339,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:16 +00:00
|
|
|
// SetOutput sets the logging output of the logger instance.
|
2016-04-07 18:38:24 +00:00
|
|
|
func (l *Logger) SetOutput(v string) {
|
|
|
|
switch v {
|
|
|
|
case "stdout":
|
|
|
|
l.Out = os.Stdout
|
|
|
|
case "stderr":
|
|
|
|
l.Out = os.Stderr
|
|
|
|
}
|
|
|
|
}
|