2017-06-25 21:45:22 +00:00
|
|
|
// +-------------------------------------------------------------------------
|
|
|
|
// | Copyright (C) 2016 Yunify, Inc.
|
|
|
|
// +-------------------------------------------------------------------------
|
|
|
|
// | Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// | you may not use this work except in compliance with the License.
|
|
|
|
// | You may obtain a copy of the License in the LICENSE file, or 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 logger provides support for logging to stdout and stderr.
|
|
|
|
// Log entries will be logged with format: $timestamp $hostname [$pid]: $severity $message.
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-08-08 09:18:33 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-25 21:45:22 +00:00
|
|
|
)
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Logger is the interface of SDK logger.
|
|
|
|
type Logger interface {
|
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Warnf(format string, args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Fatalf(format string, args ...interface{})
|
|
|
|
Panicf(format string, args ...interface{})
|
|
|
|
}
|
2017-06-25 21:45:22 +00:00
|
|
|
|
|
|
|
// LogFormatter is used to format log entry.
|
|
|
|
type LogFormatter struct{}
|
|
|
|
|
|
|
|
// Format formats a given log entry, returns byte slice and error.
|
|
|
|
func (c *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
|
|
level := strings.ToUpper(entry.Level.String())
|
|
|
|
if level == "WARNING" {
|
|
|
|
level = "WARN"
|
|
|
|
}
|
|
|
|
if len(level) < 5 {
|
|
|
|
level = strings.Repeat(" ", 5-len(level)) + level
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(fmt.Sprintf(
|
|
|
|
"[%s #%d] %s -- : %s\n",
|
|
|
|
time.Now().Format("2006-01-02T15:04:05.000Z"),
|
|
|
|
os.Getpid(),
|
|
|
|
level,
|
2017-09-30 14:27:27 +00:00
|
|
|
entry.Message),
|
|
|
|
), nil
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckLevel checks whether the log level is valid.
|
|
|
|
func CheckLevel(level string) error {
|
|
|
|
if _, err := logrus.ParseLevel(level); err != nil {
|
|
|
|
return fmt.Errorf(`log level not valid: "%s"`, level)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel get the log level string.
|
|
|
|
func GetLevel() string {
|
2017-09-30 14:27:27 +00:00
|
|
|
if l, ok := instance.(*logrus.Logger); ok {
|
|
|
|
return l.Level.String()
|
|
|
|
}
|
|
|
|
return "unknown"
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel sets the log level. Valid levels are "debug", "info", "warn", "error", and "fatal".
|
|
|
|
func SetLevel(level string) {
|
2017-09-30 14:27:27 +00:00
|
|
|
if l, ok := instance.(*logrus.Logger); ok {
|
|
|
|
lvl, err := logrus.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
Fatalf(fmt.Sprintf(`log level not valid: "%s"`, level))
|
|
|
|
}
|
|
|
|
l.Level = lvl
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// SetLogger sets the a logger as SDK logger.
|
|
|
|
func SetLogger(l Logger) {
|
|
|
|
instance = l
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Debugf logs a message with severity DEBUG.
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
instance.Debugf(format, v...)
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Infof logs a message with severity INFO.
|
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
instance.Infof(format, v...)
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Warnf logs a message with severity WARN.
|
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
instance.Warnf(format, v...)
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Errorf logs a message with severity ERROR.
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
instance.Errorf(format, v...)
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Fatalf logs a message with severity ERROR followed by a call to os.Exit().
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
|
|
instance.Fatalf(format, v...)
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
var instance Logger
|
|
|
|
|
2017-06-25 21:45:22 +00:00
|
|
|
func init() {
|
2017-09-30 14:27:27 +00:00
|
|
|
l := logrus.New()
|
|
|
|
l.Formatter = &LogFormatter{}
|
|
|
|
l.Out = os.Stderr
|
|
|
|
l.Level = logrus.WarnLevel
|
|
|
|
|
|
|
|
instance = l
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|