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
2022-03-23 23:19:36 +01:00

190 lines
4.8 KiB
Go

//
// DISCLAIMER
//
// Copyright 2016-2022 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
//
package logging
import (
"os"
"strings"
"sync"
"time"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/rs/zerolog"
)
var (
globalLoggerLock sync.Mutex
globalLogger Service
)
func GlobalLogger() Service {
globalLoggerLock.Lock()
defer globalLoggerLock.Unlock()
if globalLogger == nil {
if err := initGlobalLogger("info", nil); err != nil {
panic(err)
}
}
return globalLogger
}
func InitGlobalLogger(defaultLevel string, overrides []string) error {
globalLoggerLock.Lock()
defer globalLoggerLock.Unlock()
return initGlobalLogger(defaultLevel, overrides)
}
func initGlobalLogger(defaultLevel string, overrides []string) error {
if globalLogger != nil {
return errors.Newf("GlobalLogger already created")
}
s, err := newService(defaultLevel, overrides)
if err != nil {
return err
}
globalLogger = s
return nil
}
// 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 {
return zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339Nano,
NoColor: true,
}).With().Timestamp().Logger()
}
// newService creates a new Service.
func newService(defaultLevel string, overrides []string) (Service, error) {
l, err := stringToLevel(defaultLevel)
if err != nil {
return nil, errors.WithStack(err)
}
rootLog := NewRootLogger()
s := &loggingService{
rootLog: rootLog,
defaultLevel: l,
levels: make(map[string]zerolog.Level),
}
for k, v := range defaultLogLevels {
s.levels[k] = v
}
for _, override := range overrides {
levelParts := strings.Split(override, "=")
switch size := len(levelParts); size {
case 1:
level, err := stringToLevel(levelParts[0])
if err != nil {
return nil, errors.WithStack(err)
}
s.defaultLevel = level
case 2:
level, err := stringToLevel(levelParts[1])
if err != nil {
return nil, errors.WithStack(err)
}
s.levels[levelParts[0]] = level
default:
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 "trace":
return zerolog.TraceLevel, nil
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
}
return zerolog.InfoLevel, errors.Newf("Unknown log level '%s'", l)
}