mirror of
https://github.com/goharbor/harbor.git
synced 2024-09-17 08:14:13 +02:00
266 lines
5.6 KiB
Go
266 lines
5.6 KiB
Go
/*
|
|
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"
|
|
)
|
|
|
|
var logger = New(os.Stdout, NewTextFormatter(""), WarningLevel)
|
|
|
|
// Logger provides a struct with fields that describe the details of logger.
|
|
type Logger struct {
|
|
out io.Writer
|
|
fmtter Formatter
|
|
lvl Level
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func New(out io.Writer, fmtter Formatter, lvl Level) *Logger {
|
|
return &Logger{
|
|
out: out,
|
|
fmtter: fmtter,
|
|
lvl: lvl,
|
|
}
|
|
}
|
|
|
|
func (l *Logger) SetOutput(out io.Writer) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
l.out = out
|
|
}
|
|
|
|
func (l *Logger) SetFormatter(fmtter Formatter) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
l.fmtter = fmtter
|
|
}
|
|
|
|
func (l *Logger) SetLevel(lvl Level) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
l.lvl = lvl
|
|
}
|
|
|
|
func SetOutput(out io.Writer) {
|
|
logger.mu.Lock()
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.out = out
|
|
}
|
|
|
|
func SetFormatter(fmtter Formatter) {
|
|
logger.mu.Lock()
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.fmtter = fmtter
|
|
}
|
|
|
|
func SetLevel(lvl Level) {
|
|
logger.mu.Lock()
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.lvl = lvl
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (l *Logger) Debug(v ...interface{}) {
|
|
if l.lvl <= DebugLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, DebugLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Debugf(format string, v ...interface{}) {
|
|
if l.lvl <= DebugLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, DebugLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Info(v ...interface{}) {
|
|
if l.lvl <= InfoLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Infof(format string, v ...interface{}) {
|
|
if l.lvl <= InfoLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Warning(v ...interface{}) {
|
|
if l.lvl <= WarningLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Warningf(format string, v ...interface{}) {
|
|
if l.lvl <= WarningLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Error(v ...interface{}) {
|
|
if l.lvl <= ErrorLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, ErrorLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Errorf(format string, v ...interface{}) {
|
|
if l.lvl <= ErrorLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, ErrorLevel)
|
|
l.output(record)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Fatal(v ...interface{}) {
|
|
if l.lvl <= FatalLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, FatalLevel)
|
|
l.output(record)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|
if l.lvl <= FatalLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, FatalLevel)
|
|
l.output(record)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
|
|
func Debug(v ...interface{}) {
|
|
if logger.lvl <= DebugLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, DebugLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
if logger.lvl <= DebugLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, DebugLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Info(v ...interface{}) {
|
|
if logger.lvl <= InfoLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Infof(format string, v ...interface{}) {
|
|
if logger.lvl <= InfoLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Warning(v ...interface{}) {
|
|
if logger.lvl <= WarningLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Warningf(format string, v ...interface{}) {
|
|
if logger.lvl <= WarningLevel {
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Error(v ...interface{}) {
|
|
if logger.lvl <= ErrorLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, ErrorLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
if logger.lvl <= ErrorLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, ErrorLevel)
|
|
logger.output(record)
|
|
}
|
|
}
|
|
|
|
func Fatal(v ...interface{}) {
|
|
if logger.lvl <= FatalLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprint(v...), line, FatalLevel)
|
|
logger.output(record)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
if logger.lvl <= FatalLevel {
|
|
line := line(2)
|
|
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, FatalLevel)
|
|
logger.output(record)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
|
|
func line(calldepth int) string {
|
|
_, file, line, ok := runtime.Caller(calldepth)
|
|
if !ok {
|
|
file = "???"
|
|
line = 0
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", file, line)
|
|
}
|