2022-01-21 02:57:39 +03:00
|
|
|
// Copyright 2022 Security Scorecard Authors
|
|
|
|
//
|
|
|
|
// 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 (
|
2022-01-25 20:17:46 +03:00
|
|
|
"log"
|
|
|
|
"strings"
|
2022-01-21 02:57:39 +03:00
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
"github.com/bombsimon/logrusr/v2"
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-21 02:57:39 +03:00
|
|
|
)
|
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
// Logger exposes logging capabilities using
|
|
|
|
// https://pkg.go.dev/github.com/go-logr/logr.
|
2022-01-21 02:57:39 +03:00
|
|
|
type Logger struct {
|
2022-01-25 20:17:46 +03:00
|
|
|
*logr.Logger
|
2022-01-21 02:57:39 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
// NewLogger creates an instance of *Logger.
|
|
|
|
// TODO(log): Consider adopting production config from zap.
|
|
|
|
func NewLogger(logLevel Level) *Logger {
|
|
|
|
logrusLog := logrus.New()
|
2022-01-21 02:57:39 +03:00
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
// Set log level from logrus
|
|
|
|
logrusLevel := parseLogrusLevel(logLevel)
|
|
|
|
logrusLog.SetLevel(logrusLevel)
|
2022-01-21 02:57:39 +03:00
|
|
|
|
2022-04-13 04:18:53 +03:00
|
|
|
return NewLogrusLogger(logrusLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogrusLogger creates an instance of *Logger backed by the supplied
|
|
|
|
// logrusLog instance.
|
|
|
|
func NewLogrusLogger(logrusLog *logrus.Logger) *Logger {
|
2022-01-25 20:17:46 +03:00
|
|
|
logrLogger := logrusr.New(logrusLog)
|
2022-01-21 02:57:39 +03:00
|
|
|
logger := &Logger{
|
2022-01-25 20:17:46 +03:00
|
|
|
&logrLogger,
|
|
|
|
}
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:33:04 +03:00
|
|
|
// ParseLevel takes a string level and returns the sclog Level constant.
|
|
|
|
// If the level is not recognized, it defaults to `sclog.InfoLevel` to swallow
|
2022-01-25 20:17:46 +03:00
|
|
|
// potential configuration errors/typos when specifying log levels.
|
|
|
|
// https://pkg.go.dev/github.com/sirupsen/logrus#ParseLevel
|
2022-02-16 22:33:04 +03:00
|
|
|
func ParseLevel(lvl string) Level {
|
2022-01-25 20:17:46 +03:00
|
|
|
switch strings.ToLower(lvl) {
|
|
|
|
case "panic":
|
2022-02-16 22:33:04 +03:00
|
|
|
return PanicLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "fatal":
|
2022-02-16 22:33:04 +03:00
|
|
|
return FatalLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "error":
|
2022-02-16 22:33:04 +03:00
|
|
|
return ErrorLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "warn":
|
2022-02-16 22:33:04 +03:00
|
|
|
return WarnLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "info":
|
2022-02-16 22:33:04 +03:00
|
|
|
return InfoLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "debug":
|
2022-02-16 22:33:04 +03:00
|
|
|
return DebugLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
case "trace":
|
2022-02-16 22:33:04 +03:00
|
|
|
return TraceLevel
|
2022-01-21 02:57:39 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:33:04 +03:00
|
|
|
return DefaultLevel
|
2022-01-21 02:57:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Level is a string representation of log level, which can easily be passed as
|
|
|
|
// a parameter, in lieu of defined types in upstream logging packages.
|
|
|
|
type Level string
|
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
// Log levels.
|
2022-01-21 02:57:39 +03:00
|
|
|
const (
|
|
|
|
DefaultLevel = InfoLevel
|
2022-01-25 20:17:46 +03:00
|
|
|
TraceLevel Level = "trace"
|
2022-01-21 02:57:39 +03:00
|
|
|
DebugLevel Level = "debug"
|
|
|
|
InfoLevel Level = "info"
|
|
|
|
WarnLevel Level = "warn"
|
|
|
|
ErrorLevel Level = "error"
|
|
|
|
PanicLevel Level = "panic"
|
|
|
|
FatalLevel Level = "fatal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (l Level) String() string {
|
|
|
|
return string(l)
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:17:46 +03:00
|
|
|
func parseLogrusLevel(lvl Level) logrus.Level {
|
|
|
|
logrusLevel, err := logrus.ParseLevel(lvl.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"defaulting to INFO log level, as %s is not a valid log level: %+v",
|
|
|
|
lvl,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
|
|
|
logrusLevel = logrus.InfoLevel
|
2022-01-21 02:57:39 +03:00
|
|
|
}
|
2022-01-25 20:17:46 +03:00
|
|
|
|
|
|
|
return logrusLevel
|
2022-01-21 02:57:39 +03:00
|
|
|
}
|