1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/pkg/logging/logger.go

148 lines
4 KiB
Go
Raw Normal View History

//
// DISCLAIMER
//
2021-10-09 14:47:18 +00:00
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package logging
import (
"os"
"strings"
"sync"
2019-05-16 13:20:22 +00:00
"time"
2021-01-08 14:35:38 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/rs/zerolog"
)
// Service exposes the interfaces for a logger service
// that supports different loggers with different levels.
type Service interface {
// MustGetLogger creates a logger with given name.
MustGetLogger(name string) zerolog.Logger
// MustSetLevel sets the log level for the component with given name to given level.
MustSetLevel(name, level string)
// ConfigureRootLogger calls the given callback to modify the root logger.
ConfigureRootLogger(cb func(rootLog zerolog.Logger) zerolog.Logger)
}
// loggingService implements Service
type loggingService struct {
mutex sync.Mutex
rootLog zerolog.Logger
defaultLevel zerolog.Level
levels map[string]zerolog.Level
}
// NewRootLogger creates a new zerolog logger with default settings.
func NewRootLogger() zerolog.Logger {
2018-04-05 09:11:21 +00:00
return zerolog.New(zerolog.ConsoleWriter{
2019-05-16 13:20:22 +00:00
Out: os.Stdout,
TimeFormat: time.RFC3339Nano,
NoColor: true,
2018-04-05 09:11:21 +00:00
}).With().Timestamp().Logger()
}
// NewService creates a new Service.
func NewService(defaultLevel string, overrides []string) (Service, error) {
l, err := stringToLevel(defaultLevel)
if err != nil {
2021-01-08 14:35:38 +00:00
return nil, errors.WithStack(err)
}
rootLog := NewRootLogger()
s := &loggingService{
rootLog: rootLog,
defaultLevel: l,
levels: make(map[string]zerolog.Level),
}
for _, override := range overrides {
levelParts := strings.Split(override, "=")
switch size := len(levelParts); size {
case 1:
level, err := stringToLevel(levelParts[0])
if err != nil {
2021-01-08 14:35:38 +00:00
return nil, errors.WithStack(err)
}
s.defaultLevel = level
case 2:
level, err := stringToLevel(levelParts[1])
if err != nil {
2021-01-08 14:35:38 +00:00
return nil, errors.WithStack(err)
}
s.levels[levelParts[0]] = level
default:
2021-01-08 14:35:38 +00:00
return nil, errors.Newf("invalid log definition %s: Length %d is not equal 1 or 2", override, size)
}
}
return s, nil
}
// ConfigureRootLogger calls the given callback to modify the root logger.
func (s *loggingService) ConfigureRootLogger(cb func(rootLog zerolog.Logger) zerolog.Logger) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.rootLog = cb(s.rootLog)
}
// MustGetLogger creates a logger with given name
func (s *loggingService) MustGetLogger(name string) zerolog.Logger {
s.mutex.Lock()
defer s.mutex.Unlock()
level, found := s.levels[name]
if !found {
level = s.defaultLevel
}
return s.rootLog.With().Str("component", name).Logger().Level(level)
}
// MustSetLevel sets the log level for the component with given name to given level.
func (s *loggingService) MustSetLevel(name, level string) {
l, err := stringToLevel(level)
if err != nil {
panic(err)
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.levels[name] = l
}
// stringToLevel converts a level string to a zerolog level
func stringToLevel(l string) (zerolog.Level, error) {
switch strings.ToLower(l) {
case "debug":
return zerolog.DebugLevel, nil
case "info":
return zerolog.InfoLevel, nil
case "warn", "warning":
return zerolog.WarnLevel, nil
case "error":
return zerolog.ErrorLevel, nil
case "fatal":
return zerolog.FatalLevel, nil
case "panic":
return zerolog.PanicLevel, nil
}
2021-01-08 14:35:38 +00:00
return zerolog.InfoLevel, errors.Newf("Unknown log level '%s'", l)
}