2016-03-14 07:00:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-03-16 08:54:59 +01:00
|
|
|
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel)
|
|
|
|
|
|
|
|
func init() {
|
2016-04-01 10:48:39 +02:00
|
|
|
logger.callDepth = 3
|
|
|
|
|
2016-03-16 08:54:59 +01:00
|
|
|
// TODO add item in configuaration file
|
|
|
|
lvl := os.Getenv("LOG_LEVEL")
|
|
|
|
if len(lvl) == 0 {
|
|
|
|
logger.SetLevel(InfoLevel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
level, err := parseLevel(lvl)
|
|
|
|
if err != nil {
|
|
|
|
logger.SetLevel(InfoLevel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetLevel(level)
|
|
|
|
|
|
|
|
}
|
2016-03-14 07:00:37 +01:00
|
|
|
|
|
|
|
// Logger provides a struct with fields that describe the details of logger.
|
|
|
|
type Logger struct {
|
2016-04-01 10:48:39 +02:00
|
|
|
out io.Writer
|
|
|
|
fmtter Formatter
|
|
|
|
lvl Level
|
|
|
|
callDepth int
|
|
|
|
mu sync.Mutex
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// New returns a customized Logger
|
2016-03-14 07:00:37 +01:00
|
|
|
func New(out io.Writer, fmtter Formatter, lvl Level) *Logger {
|
|
|
|
return &Logger{
|
2016-04-01 10:48:39 +02:00
|
|
|
out: out,
|
|
|
|
fmtter: fmtter,
|
|
|
|
lvl: lvl,
|
|
|
|
callDepth: 2,
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetOutput sets the output of Logger l
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) SetOutput(out io.Writer) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
l.out = out
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetFormatter sets the formatter of Logger l
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) SetFormatter(fmtter Formatter) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
l.fmtter = fmtter
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetLevel sets the level of Logger l
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) SetLevel(lvl Level) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
l.lvl = lvl
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetOutput sets the output of default Logger
|
2016-03-14 07:00:37 +01:00
|
|
|
func SetOutput(out io.Writer) {
|
2016-03-14 07:53:28 +01:00
|
|
|
logger.SetOutput(out)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetFormatter sets the formatter of default Logger
|
2016-03-14 07:00:37 +01:00
|
|
|
func SetFormatter(fmtter Formatter) {
|
2016-03-14 07:53:28 +01:00
|
|
|
logger.SetFormatter(fmtter)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
//SetLevel sets the level of default Logger
|
2016-03-14 07:00:37 +01:00
|
|
|
func SetLevel(lvl Level) {
|
2016-03-14 07:53:28 +01:00
|
|
|
logger.SetLevel(lvl)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) output(record *Record) (err error) {
|
|
|
|
b, err := l.fmtter.Format(record)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
_, err = l.out.Write(b)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Debug ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Debug(v ...interface{}) {
|
|
|
|
if l.lvl <= DebugLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, DebugLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Debugf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Debugf(format string, v ...interface{}) {
|
|
|
|
if l.lvl <= DebugLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, DebugLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Info ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Info(v ...interface{}) {
|
|
|
|
if l.lvl <= InfoLevel {
|
|
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Infof ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Infof(format string, v ...interface{}) {
|
|
|
|
if l.lvl <= InfoLevel {
|
|
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Warning ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Warning(v ...interface{}) {
|
|
|
|
if l.lvl <= WarningLevel {
|
|
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Warningf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Warningf(format string, v ...interface{}) {
|
|
|
|
if l.lvl <= WarningLevel {
|
|
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Error ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Error(v ...interface{}) {
|
|
|
|
if l.lvl <= ErrorLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, ErrorLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Errorf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Errorf(format string, v ...interface{}) {
|
|
|
|
if l.lvl <= ErrorLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, ErrorLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Fatal ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Fatal(v ...interface{}) {
|
|
|
|
if l.lvl <= FatalLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, FatalLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Fatalf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|
|
|
if l.lvl <= FatalLevel {
|
2016-04-01 10:48:39 +02:00
|
|
|
line := line(l.callDepth)
|
2016-03-14 07:00:37 +01:00
|
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, FatalLevel)
|
|
|
|
l.output(record)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Debug ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Debug(v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Debug(v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Debugf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Debugf(format, v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Info ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Info(v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Info(v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Infof ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Infof(format string, v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Infof(format, v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Warning ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Warning(v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Warning(v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Warningf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Warningf(format string, v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Warningf(format, v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Error ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Error(v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Error(v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Errorf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Errorf(format string, v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Errorf(format, v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Fatal ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Fatal(v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Fatal(v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 07:53:28 +01:00
|
|
|
// Fatalf ...
|
2016-03-14 07:00:37 +01:00
|
|
|
func Fatalf(format string, v ...interface{}) {
|
2016-03-15 06:31:08 +01:00
|
|
|
logger.Fatalf(format, v...)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func line(calldepth int) string {
|
|
|
|
_, file, line, ok := runtime.Caller(calldepth)
|
|
|
|
if !ok {
|
|
|
|
file = "???"
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:13:04 +02:00
|
|
|
for i := len(file) - 2; i > 0; i-- {
|
|
|
|
if file[i] == os.PathSeparator {
|
2016-04-01 11:10:21 +02:00
|
|
|
file = file[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:13:04 +02:00
|
|
|
return fmt.Sprintf("[%s:%d]:", file, line)
|
2016-03-14 07:00:37 +01:00
|
|
|
}
|