...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/logging/logging.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/logging

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package logging adds common logging hooks for cnrm applications
    16  package logging
    17  
    18  import (
    19  	"io"
    20  	"os"
    21  
    22  	"github.com/go-logr/logr"
    23  	"github.com/go-logr/zapr"
    24  	"go.uber.org/zap/zapcore"
    25  	klog "sigs.k8s.io/controller-runtime/pkg/log"
    26  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    27  )
    28  
    29  var logger = klog.Log
    30  
    31  // BuildLogger constructs a logr.Logger object that matches the standard
    32  // configuration across cnrm applications, writing to the io.Writer passed.
    33  func BuildLogger(output io.Writer) logr.Logger {
    34  	encoderCfg := zapcore.EncoderConfig{
    35  		MessageKey:     "msg",
    36  		LevelKey:       "severity",
    37  		NameKey:        "logger",
    38  		TimeKey:        "timestamp",
    39  		EncodeLevel:    zapcore.LowercaseLevelEncoder,
    40  		EncodeTime:     zapcore.ISO8601TimeEncoder,
    41  		EncodeDuration: zapcore.StringDurationEncoder,
    42  	}
    43  	encoder := zapcore.NewJSONEncoder(encoderCfg)
    44  	return zapr.NewLogger(zap.NewRaw(zap.WriteTo(output), zap.Encoder(encoder)))
    45  }
    46  
    47  // Fatal is a utility function to replace log.Fatal, which doesn't exist
    48  // for logr loggers.
    49  func Fatal(err error, msg string) {
    50  	logger.Error(err, msg)
    51  	os.Exit(1)
    52  }
    53  

View as plain text