github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/log/config.go (about)

     1  /*
     2   * Copyright 2020 The Compass Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/sirupsen/logrus"
    24  )
    25  
    26  // Config missing godoc
    27  type Config struct {
    28  	Level                  string `envconfig:"APP_LOG_LEVEL,default=info"`
    29  	Format                 string `envconfig:"APP_LOG_FORMAT,default=text"`
    30  	Output                 string `envconfig:"APP_LOG_OUTPUT,default=/dev/stdout"`
    31  	BootstrapCorrelationID string `envconfig:"APP_LOG_BOOTSTRAP_CORRELATION_ID,default=bootstrap"`
    32  }
    33  
    34  // DefaultConfig returns default values for Log settings
    35  func DefaultConfig() *Config {
    36  	return &Config{
    37  		Level:                  "info",
    38  		Format:                 "text",
    39  		Output:                 os.Stdout.Name(),
    40  		BootstrapCorrelationID: "bootstrap",
    41  	}
    42  }
    43  
    44  // Validate validates the logging settings
    45  func (s *Config) Validate() error {
    46  	if _, err := logrus.ParseLevel(s.Level); err != nil {
    47  		return fmt.Errorf("validate Config: log level %s is invalid: %s", s.Level, err)
    48  	}
    49  
    50  	if len(s.Format) == 0 {
    51  		return fmt.Errorf("validate Config: log format missing")
    52  	}
    53  
    54  	if _, ok := supportedFormatters[s.Format]; !ok {
    55  		return fmt.Errorf("validate Config: log format %s is invalid", s.Format)
    56  	}
    57  
    58  	if len(s.Output) == 0 {
    59  		return fmt.Errorf("validate Config: log output missing")
    60  	}
    61  
    62  	if _, ok := supportedOutputs[s.Output]; !ok {
    63  		return fmt.Errorf("validate Config: log output %s is invalid", s.Output)
    64  	}
    65  
    66  	if s.BootstrapCorrelationID == "" {
    67  		return fmt.Errorf("validate Config: bootstrap correlation id cannot be empty")
    68  	}
    69  
    70  	return nil
    71  }