package log
This commit is contained in:
parent
f789742434
commit
27f0897faa
|
@ -0,0 +1,133 @@
|
|||
// Copyright (c) 2014, B3log
|
||||
//
|
||||
// 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 includes logging related manipulations.
|
||||
//
|
||||
// logger := log.NewLogger(os.Stdout, log.Debug)
|
||||
//
|
||||
// logger.Debug("debug message")
|
||||
// logger.Info("info message")
|
||||
// logger.Warn("warning message")
|
||||
// logger.Error("error message")
|
||||
//
|
||||
// logger.Errorf("formatted %s message", "error")
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
stdlog "log"
|
||||
)
|
||||
|
||||
// Logging level.
|
||||
const (
|
||||
Debug = iota
|
||||
Info
|
||||
Warn
|
||||
Error
|
||||
)
|
||||
|
||||
// Logger is a simple logger with level.
|
||||
// The underlying logger is the Go standard logging "log".
|
||||
type Logger struct {
|
||||
level int
|
||||
logger *stdlog.Logger
|
||||
}
|
||||
|
||||
// NewLogger creates a logger.
|
||||
func NewLogger(out io.Writer, level int) *Logger {
|
||||
ret := &Logger{level: level, logger: stdlog.New(out, "", stdlog.Ldate|stdlog.Ltime|stdlog.Lshortfile)}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// Debug prints debug level message.
|
||||
func (l *Logger) Debug(v ...interface{}) {
|
||||
if Debug < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("D ")
|
||||
l.logger.Output(2, fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Debugf prints debug level message with format.
|
||||
func (l *Logger) Debugf(format string, v ...interface{}) {
|
||||
if Debug < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("D ")
|
||||
l.logger.Output(2, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Info prints info level message.
|
||||
func (l *Logger) Info(v ...interface{}) {
|
||||
if Info < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("I ")
|
||||
l.logger.Output(2, fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Infof prints info level message with format.
|
||||
func (l *Logger) Infof(format string, v ...interface{}) {
|
||||
if Info < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("I ")
|
||||
l.logger.Output(2, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Warn prints warning level message.
|
||||
func (l *Logger) Warn(v ...interface{}) {
|
||||
if Warn < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("W ")
|
||||
l.logger.Output(2, fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Warn prints warning level message with format.
|
||||
func (l *Logger) Warnf(format string, v ...interface{}) {
|
||||
if Warn < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("W ")
|
||||
l.logger.Output(2, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Error prints error level message.
|
||||
func (l *Logger) Error(v ...interface{}) {
|
||||
if Error < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("E ")
|
||||
l.logger.Output(2, fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Errorf prints error level message with format.
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) {
|
||||
if Error < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
l.logger.SetPrefix("E ")
|
||||
l.logger.Output(2, fmt.Sprintf(format, v...))
|
||||
}
|
Loading…
Reference in New Issue