blob: 6cb84e23322d6f7e402cccbd6b782d299f8d2756 [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 provides a log for the framework and applications.
package log
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// SetLevel sets log level for different output which may be "0", "1" or "2".
func SetLevel(output string, level Level) {
GetDefaultLogger().SetLevel(output, level)
}
// GetLevel gets log level for different output.
func GetLevel(output string) Level {
return GetDefaultLogger().GetLevel(output)
}
// With adds user defined fields to Logger. Field support multiple values.
func With(fields ...Field) Logger {
return GetDefaultLogger().With(fields...)
}
// RedirectStdLog redirects std log as log level INFO.
// After redirection, log flag is zero, the prefix is empty.
// The returned function may be used to recover log flag and prefix, and redirect output to
// os.Stderr.
func RedirectStdLog(logger Logger) (func(), error) {
return RedirectStdLogAt(logger, zap.InfoLevel)
}
// RedirectStdLogAt redirects std log with a specific level.
// After redirection, log flag is zero, the prefix is empty.
// The returned function may be used to recover log flag and prefix, and redirect output to
// os.Stderr.
func RedirectStdLogAt(logger Logger, level zapcore.Level) (func(), error) {
if l, ok := logger.(*zapLog); ok {
return zap.RedirectStdLogAt(l.logger, level)
}
if l, ok := logger.(*ZapLogWrapper); ok {
return zap.RedirectStdLogAt(l.l.logger, level)
}
return nil, fmt.Errorf("log: only supports redirecting std logs to zap logger")
}
// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
func Debug(args ...interface{}) {
GetDefaultLogger().Debug(args...)
}
// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, args ...interface{}) {
GetDefaultLogger().Debugf(format, args...)
}
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func Info(args ...interface{}) {
GetDefaultLogger().Info(args...)
}
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func Infof(format string, args ...interface{}) {
GetDefaultLogger().Infof(format, args...)
}
// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func Warn(args ...interface{}) {
GetDefaultLogger().Warn(args...)
}
// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func Warnf(format string, args ...interface{}) {
GetDefaultLogger().Warnf(format, args...)
}
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func Error(args ...interface{}) {
GetDefaultLogger().Error(args...)
}
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, args ...interface{}) {
GetDefaultLogger().Errorf(format, args...)
}
// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
// All Fatal logs will exit by calling os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func Fatal(args ...interface{}) {
GetDefaultLogger().Fatal(args...)
}
// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, args ...interface{}) {
GetDefaultLogger().Fatalf(format, args...)
}