...

Source file src/github.com/dsoprea/go-logging/v2/config.go

Documentation: github.com/dsoprea/go-logging/v2

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

View as plain text