From 16a68000d3481c5a768aeff175160197708a48ac Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Mon, 22 Oct 2012 18:07:16 -0700 Subject: [PATCH] Make log level configurable --- config/config.go | 18 ++++++++++++++++ etc/devel.conf | 53 +++++++++++++++++++++++++++++++++++++++++++++++ etc/inbucket.conf | 6 ++++++ inbucket.go | 5 +++++ log/logging.go | 19 +++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 etc/devel.conf diff --git a/config/config.go b/config/config.go index 65f56a3..f04a962 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "github.com/robfig/goconfig/config" "net" "os" + "strings" ) // SmtpConfig houses the SMTP server configuration - not using pointers @@ -52,6 +53,7 @@ func LoadConfig(filename string) error { messages := list.New() // Validate sections + requireSection(messages, "logging") requireSection(messages, "smtp") requireSection(messages, "web") requireSection(messages, "datastore") @@ -64,6 +66,7 @@ func LoadConfig(filename string) error { } // Validate options + requireOption(messages, "logging", "level") requireOption(messages, "smtp", "ip4.address") requireOption(messages, "smtp", "ip4.port") requireOption(messages, "smtp", "domain") @@ -91,6 +94,21 @@ func LoadConfig(filename string) error { return err } +// parseLoggingConfig trying to catch config errors early +func parseLoggingConfig() error { + option := "[logging]level" + str, err := Config.String("logging", "level") + if err != nil { + return fmt.Errorf("Failed to parse %v: %v", option, err) + } + switch strings.ToUpper(str) { + case "TRACE", "INFO", "WARN", "ERROR": + default: + return fmt.Errorf("Invalid value provided for %v: %v", option, str) + } + return nil +} + // parseSmtpConfig trying to catch config errors early func parseSmtpConfig() error { smtpConfig = new(SmtpConfig) diff --git a/etc/devel.conf b/etc/devel.conf new file mode 100644 index 0000000..0dbe942 --- /dev/null +++ b/etc/devel.conf @@ -0,0 +1,53 @@ +# devel.conf +# Sample development configuration + +############################################################################# +[DEFAULT] + +# Not used by directly, but is typically referenced below in %()s format. +install.dir=. + +############################################################################# +[logging] + +# Options from least to most verbose: ERROR, WARN, INFO, TRACE +level=TRACE + +############################################################################# +[smtp] + +# IPv4 address to listen for SMTP connections on. +ip4.address=0.0.0.0 + +# IPv4 port to listen for SMTP connections on. +ip4.port=2500 + +# used in SMTP greeting +domain=inbucket.local + +############################################################################# +[web] + +# IPv4 address to serve HTTP web interface on +ip4.address=0.0.0.0 + +# IPv4 port to serve HTTP web interface on +ip4.port=9000 + +# Name of web theme to use +theme=integral + +# Path to the selected themes template files +template.dir=%(install.dir)s/themes/%(theme)s/templates + +# Should we cache parsed templates (set to false during theme dev) +template.cache=false + +# Path to the selected themes public (static) files +public.dir=%(install.dir)s/themes/%(theme)s/public + +############################################################################# +[datastore] + +# Path to the datastore, mail will be written into subdirectories +path=/tmp/inbucket diff --git a/etc/inbucket.conf b/etc/inbucket.conf index e97ae3a..54ef3bc 100644 --- a/etc/inbucket.conf +++ b/etc/inbucket.conf @@ -7,6 +7,12 @@ # Not used by directly, but is typically referenced below in %()s format. install.dir=. +############################################################################# +[logging] + +# Options from least to most verbose: ERROR, WARN, INFO, TRACE +level=INFO + ############################################################################# [smtp] diff --git a/inbucket.go b/inbucket.go index afffee4..2a64b0d 100644 --- a/inbucket.go +++ b/inbucket.go @@ -7,6 +7,7 @@ import ( "flag" "fmt" "github.com/jhillyerd/inbucket/config" + "github.com/jhillyerd/inbucket/log" "github.com/jhillyerd/inbucket/smtpd" "github.com/jhillyerd/inbucket/web" "os" @@ -32,6 +33,10 @@ func main() { os.Exit(1) } + // Configure logging + level, _ := config.Config.String("logging", "level") + log.SetLogLevel(level) + // Startup SMTP server server := smtpd.New() go server.Start() diff --git a/log/logging.go b/log/logging.go index aa1abcb..1fec113 100644 --- a/log/logging.go +++ b/log/logging.go @@ -2,6 +2,7 @@ package log import ( "log" + "strings" ) type LogLevel int @@ -15,6 +16,24 @@ const ( var MaxLogLevel LogLevel = TRACE +// SetLogLevel sets MaxLogLevel based on the provided string +func SetLogLevel(level string) (ok bool) { + switch strings.ToUpper(level) { + case "ERROR": + MaxLogLevel = ERROR + case "WARN": + MaxLogLevel = WARN + case "INFO": + MaxLogLevel = INFO + case "TRACE": + MaxLogLevel = TRACE + default: + Error("Unknown log level requested: %v", level) + return false + } + return true +} + // Error logs a message to the 'standard' Logger (always) func Error(msg string, args ...interface{}) { msg = "[ERROR] " + msg