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.
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2018-01-16 13:20:59 +00:00
|
|
|
"context"
|
2017-06-25 21:45:22 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
"github.com/pengsrc/go-shared/log"
|
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 {
|
2018-01-16 13:20:59 +00:00
|
|
|
Debugf(ctx context.Context, format string, v ...interface{})
|
|
|
|
Infof(ctx context.Context, format string, v ...interface{})
|
|
|
|
Warnf(ctx context.Context, format string, v ...interface{})
|
|
|
|
Errorf(ctx context.Context, format string, v ...interface{})
|
|
|
|
Fatalf(ctx context.Context, format string, v ...interface{})
|
|
|
|
Panicf(ctx context.Context, format string, v ...interface{})
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckLevel checks whether the log level is valid.
|
|
|
|
func CheckLevel(level string) error {
|
2018-01-16 13:20:59 +00:00
|
|
|
if _, err := log.ParseLevel(level); err != nil {
|
2017-06-25 21:45:22 +00:00
|
|
|
return fmt.Errorf(`log level not valid: "%s"`, level)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel get the log level string.
|
|
|
|
func GetLevel() string {
|
2018-01-16 13:20:59 +00:00
|
|
|
if l, ok := instance.(*log.Logger); ok {
|
|
|
|
return l.GetLevel()
|
2017-09-30 14:27:27 +00:00
|
|
|
}
|
2018-01-16 13:20:59 +00:00
|
|
|
return "UNKNOWN"
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// SetLevel sets the log level.
|
|
|
|
// Valid levels are "debug", "info", "warn", "error", and "fatal".
|
2017-06-25 21:45:22 +00:00
|
|
|
func SetLevel(level string) {
|
2018-01-16 13:20:59 +00:00
|
|
|
if l, ok := instance.(*log.Logger); ok {
|
|
|
|
err := l.SetLevel(level)
|
2017-09-30 14:27:27 +00:00
|
|
|
if err != nil {
|
2018-01-16 13:20:59 +00:00
|
|
|
Fatalf(nil, fmt.Sprintf(`log level not valid: "%s"`, level))
|
2017-09-30 14:27:27 +00:00
|
|
|
}
|
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.
|
2018-01-16 13:20:59 +00:00
|
|
|
func Debugf(ctx context.Context, format string, v ...interface{}) {
|
|
|
|
instance.Debugf(ctx, 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.
|
2018-01-16 13:20:59 +00:00
|
|
|
func Infof(ctx context.Context, format string, v ...interface{}) {
|
|
|
|
instance.Infof(ctx, 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.
|
2018-01-16 13:20:59 +00:00
|
|
|
func Warnf(ctx context.Context, format string, v ...interface{}) {
|
|
|
|
instance.Warnf(ctx, 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.
|
2018-01-16 13:20:59 +00:00
|
|
|
func Errorf(ctx context.Context, format string, v ...interface{}) {
|
|
|
|
instance.Errorf(ctx, 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().
|
2018-01-16 13:20:59 +00:00
|
|
|
func Fatalf(ctx context.Context, format string, v ...interface{}) {
|
|
|
|
instance.Fatalf(ctx, 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() {
|
2018-01-16 13:20:59 +00:00
|
|
|
l, err := log.NewTerminalLogger(log.WarnLevel.String())
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to initialize QingStor SDK logger: %v", err))
|
|
|
|
}
|
2017-09-30 14:27:27 +00:00
|
|
|
instance = l
|
2017-06-25 21:45:22 +00:00
|
|
|
}
|