gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

config.go (6343B)


      1 package log
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 )
      7 
      8 // Config keys.
      9 const (
     10 	ckFormat                 = "LogFormat"
     11 	ckDefaultAdapterName     = "LogDefaultAdapterName"
     12 	ckLevelName              = "LogLevelName"
     13 	ckIncludeNouns           = "LogIncludeNouns"
     14 	ckExcludeNouns           = "LogExcludeNouns"
     15 	ckExcludeBypassLevelName = "LogExcludeBypassLevelName"
     16 )
     17 
     18 // Other constants
     19 const (
     20 	defaultFormat    = "{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}"
     21 	defaultLevelName = LevelNameInfo
     22 )
     23 
     24 // Config
     25 var (
     26 	// Alternative format.
     27 	format = defaultFormat
     28 
     29 	// Alternative adapter.
     30 	defaultAdapterName = ""
     31 
     32 	// Alternative level at which to display log-items
     33 	levelName = defaultLevelName
     34 
     35 	// Configuration-driven comma-separated list of nouns to include.
     36 	includeNouns = ""
     37 
     38 	// Configuration-driven comma-separated list of nouns to exclude.
     39 	excludeNouns = ""
     40 
     41 	// Level at which to disregard exclusion (if the severity of a message
     42 	// meets or exceed this, always display).
     43 	excludeBypassLevelName = ""
     44 )
     45 
     46 // Other
     47 var (
     48 	configurationLoaded = false
     49 )
     50 
     51 // Return the current default adapter name.
     52 func GetDefaultAdapterName() string {
     53 	return defaultAdapterName
     54 }
     55 
     56 // The adapter will automatically be the first one registered. This overrides
     57 // that.
     58 func SetDefaultAdapterName(name string) {
     59 	defaultAdapterName = name
     60 }
     61 
     62 func LoadConfiguration(cp ConfigurationProvider) {
     63 	configuredDefaultAdapterName := cp.DefaultAdapterName()
     64 
     65 	if configuredDefaultAdapterName != "" {
     66 		defaultAdapterName = configuredDefaultAdapterName
     67 	}
     68 
     69 	includeNouns = cp.IncludeNouns()
     70 	excludeNouns = cp.ExcludeNouns()
     71 	excludeBypassLevelName = cp.ExcludeBypassLevelName()
     72 
     73 	f := cp.Format()
     74 	if f != "" {
     75 		format = f
     76 	}
     77 
     78 	ln := cp.LevelName()
     79 	if ln != "" {
     80 		levelName = ln
     81 	}
     82 
     83 	configurationLoaded = true
     84 }
     85 
     86 func getConfigState() map[string]interface{} {
     87 	return map[string]interface{}{
     88 		"format":                 format,
     89 		"defaultAdapterName":     defaultAdapterName,
     90 		"levelName":              levelName,
     91 		"includeNouns":           includeNouns,
     92 		"excludeNouns":           excludeNouns,
     93 		"excludeBypassLevelName": excludeBypassLevelName,
     94 	}
     95 }
     96 
     97 func setConfigState(config map[string]interface{}) {
     98 	format = config["format"].(string)
     99 
    100 	defaultAdapterName = config["defaultAdapterName"].(string)
    101 	levelName = config["levelName"].(string)
    102 	includeNouns = config["includeNouns"].(string)
    103 	excludeNouns = config["excludeNouns"].(string)
    104 	excludeBypassLevelName = config["excludeBypassLevelName"].(string)
    105 }
    106 
    107 func getConfigDump() string {
    108 	return fmt.Sprintf(
    109 		"Current configuration:\n"+
    110 			"  FORMAT=[%s]\n"+
    111 			"  DEFAULT-ADAPTER-NAME=[%s]\n"+
    112 			"  LEVEL-NAME=[%s]\n"+
    113 			"  INCLUDE-NOUNS=[%s]\n"+
    114 			"  EXCLUDE-NOUNS=[%s]\n"+
    115 			"  EXCLUDE-BYPASS-LEVEL-NAME=[%s]",
    116 		format, defaultAdapterName, levelName, includeNouns, excludeNouns, excludeBypassLevelName)
    117 }
    118 
    119 func IsConfigurationLoaded() bool {
    120 	return configurationLoaded
    121 }
    122 
    123 type ConfigurationProvider interface {
    124 	// Alternative format (defaults to .
    125 	Format() string
    126 
    127 	// Alternative adapter (defaults to "appengine").
    128 	DefaultAdapterName() string
    129 
    130 	// Alternative level at which to display log-items (defaults to
    131 	// "info").
    132 	LevelName() string
    133 
    134 	// Configuration-driven comma-separated list of nouns to include. Defaults
    135 	// to empty.
    136 	IncludeNouns() string
    137 
    138 	// Configuration-driven comma-separated list of nouns to exclude. Defaults
    139 	// to empty.
    140 	ExcludeNouns() string
    141 
    142 	// Level at which to disregard exclusion (if the severity of a message
    143 	// meets or exceed this, always display). Defaults to empty.
    144 	ExcludeBypassLevelName() string
    145 }
    146 
    147 // Environment configuration-provider.
    148 type EnvironmentConfigurationProvider struct {
    149 }
    150 
    151 func NewEnvironmentConfigurationProvider() *EnvironmentConfigurationProvider {
    152 	return new(EnvironmentConfigurationProvider)
    153 }
    154 
    155 func (ecp *EnvironmentConfigurationProvider) Format() string {
    156 	return os.Getenv(ckFormat)
    157 }
    158 
    159 func (ecp *EnvironmentConfigurationProvider) DefaultAdapterName() string {
    160 	return os.Getenv(ckDefaultAdapterName)
    161 }
    162 
    163 func (ecp *EnvironmentConfigurationProvider) LevelName() string {
    164 	return os.Getenv(ckLevelName)
    165 }
    166 
    167 func (ecp *EnvironmentConfigurationProvider) IncludeNouns() string {
    168 	return os.Getenv(ckIncludeNouns)
    169 }
    170 
    171 func (ecp *EnvironmentConfigurationProvider) ExcludeNouns() string {
    172 	return os.Getenv(ckExcludeNouns)
    173 }
    174 
    175 func (ecp *EnvironmentConfigurationProvider) ExcludeBypassLevelName() string {
    176 	return os.Getenv(ckExcludeBypassLevelName)
    177 }
    178 
    179 // Static configuration-provider.
    180 type StaticConfigurationProvider struct {
    181 	format                 string
    182 	defaultAdapterName     string
    183 	levelName              string
    184 	includeNouns           string
    185 	excludeNouns           string
    186 	excludeBypassLevelName string
    187 }
    188 
    189 func NewStaticConfigurationProvider() *StaticConfigurationProvider {
    190 	return new(StaticConfigurationProvider)
    191 }
    192 
    193 func (scp *StaticConfigurationProvider) SetFormat(format string) {
    194 	scp.format = format
    195 }
    196 
    197 func (scp *StaticConfigurationProvider) SetDefaultAdapterName(adapterName string) {
    198 	scp.defaultAdapterName = adapterName
    199 }
    200 
    201 func (scp *StaticConfigurationProvider) SetLevelName(levelName string) {
    202 	scp.levelName = levelName
    203 }
    204 
    205 func (scp *StaticConfigurationProvider) SetIncludeNouns(includeNouns string) {
    206 	scp.includeNouns = includeNouns
    207 }
    208 
    209 func (scp *StaticConfigurationProvider) SetExcludeNouns(excludeNouns string) {
    210 	scp.excludeNouns = excludeNouns
    211 }
    212 
    213 func (scp *StaticConfigurationProvider) SetExcludeBypassLevelName(excludeBypassLevelName string) {
    214 	scp.excludeBypassLevelName = excludeBypassLevelName
    215 }
    216 
    217 func (scp *StaticConfigurationProvider) Format() string {
    218 	return scp.format
    219 }
    220 
    221 func (scp *StaticConfigurationProvider) DefaultAdapterName() string {
    222 	return scp.defaultAdapterName
    223 }
    224 
    225 func (scp *StaticConfigurationProvider) LevelName() string {
    226 	return scp.levelName
    227 }
    228 
    229 func (scp *StaticConfigurationProvider) IncludeNouns() string {
    230 	return scp.includeNouns
    231 }
    232 
    233 func (scp *StaticConfigurationProvider) ExcludeNouns() string {
    234 	return scp.excludeNouns
    235 }
    236 
    237 func (scp *StaticConfigurationProvider) ExcludeBypassLevelName() string {
    238 	return scp.excludeBypassLevelName
    239 }
    240 
    241 func init() {
    242 	// Do the initial configuration-load from the environment. We gotta seed it
    243 	// with something for simplicity's sake.
    244 	ecp := NewEnvironmentConfigurationProvider()
    245 	LoadConfiguration(ecp)
    246 }