github.com/safing/portbase@v0.19.5/config/basic_config.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  
     7  	"github.com/safing/portbase/log"
     8  )
     9  
    10  // Configuration Keys.
    11  var (
    12  	CfgDevModeKey  = "core/devMode"
    13  	defaultDevMode bool
    14  
    15  	CfgLogLevel     = "core/log/level"
    16  	defaultLogLevel = log.InfoLevel.String()
    17  	logLevel        StringOption
    18  )
    19  
    20  func init() {
    21  	flag.BoolVar(&defaultDevMode, "devmode", false, "enable development mode; configuration is stronger")
    22  }
    23  
    24  func registerBasicOptions() error {
    25  	// Get the default log level from the log package.
    26  	defaultLogLevel = log.GetLogLevel().Name()
    27  
    28  	// Register logging setting.
    29  	// The log package cannot do that, as it would trigger and import loop.
    30  	if err := Register(&Option{
    31  		Name:           "Log Level",
    32  		Key:            CfgLogLevel,
    33  		Description:    "Configure the logging level.",
    34  		OptType:        OptTypeString,
    35  		ExpertiseLevel: ExpertiseLevelDeveloper,
    36  		ReleaseLevel:   ReleaseLevelStable,
    37  		DefaultValue:   defaultLogLevel,
    38  		Annotations: Annotations{
    39  			DisplayOrderAnnotation: 513,
    40  			DisplayHintAnnotation:  DisplayHintOneOf,
    41  			CategoryAnnotation:     "Development",
    42  		},
    43  		PossibleValues: []PossibleValue{
    44  			{
    45  				Name:        "Critical",
    46  				Value:       "critical",
    47  				Description: "The critical level only logs errors that lead to a partial, but imminent failure.",
    48  			},
    49  			{
    50  				Name:        "Error",
    51  				Value:       "error",
    52  				Description: "The error level logs errors that potentially break functionality. Everything logged by the critical level is included here too.",
    53  			},
    54  			{
    55  				Name:        "Warning",
    56  				Value:       "warning",
    57  				Description: "The warning level logs minor errors and worse. Everything logged by the error level is included here too.",
    58  			},
    59  			{
    60  				Name:        "Info",
    61  				Value:       "info",
    62  				Description: "The info level logs the main events that are going on and are interesting to the user. Everything logged by the warning level is included here too.",
    63  			},
    64  			{
    65  				Name:        "Debug",
    66  				Value:       "debug",
    67  				Description: "The debug level logs some additional debugging details. Everything logged by the info level is included here too.",
    68  			},
    69  			{
    70  				Name:        "Trace",
    71  				Value:       "trace",
    72  				Description: "The trace level logs loads of detailed information as well as operation and request traces. Everything logged by the debug level is included here too.",
    73  			},
    74  		},
    75  	}); err != nil {
    76  		return err
    77  	}
    78  	logLevel = GetAsString(CfgLogLevel, defaultLogLevel)
    79  
    80  	// Register to hook to update the log level.
    81  	if err := module.RegisterEventHook(
    82  		"config",
    83  		ChangeEvent,
    84  		"update log level",
    85  		setLogLevel,
    86  	); err != nil {
    87  		return err
    88  	}
    89  
    90  	return Register(&Option{
    91  		Name:           "Development Mode",
    92  		Key:            CfgDevModeKey,
    93  		Description:    "In Development Mode, security restrictions are lifted/softened to enable unrestricted access for debugging and testing purposes.",
    94  		OptType:        OptTypeBool,
    95  		ExpertiseLevel: ExpertiseLevelDeveloper,
    96  		ReleaseLevel:   ReleaseLevelStable,
    97  		DefaultValue:   defaultDevMode,
    98  		Annotations: Annotations{
    99  			DisplayOrderAnnotation: 512,
   100  			CategoryAnnotation:     "Development",
   101  		},
   102  	})
   103  }
   104  
   105  func loadLogLevel() error {
   106  	return setDefaultConfigOption(CfgLogLevel, log.GetLogLevel().Name(), false)
   107  }
   108  
   109  func setLogLevel(ctx context.Context, data interface{}) error {
   110  	log.SetLogLevel(log.ParseLevel(logLevel()))
   111  
   112  	return nil
   113  }