1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

Make log level configurable

This commit is contained in:
James Hillyerd
2012-10-22 18:07:16 -07:00
parent 3099777044
commit 16a68000d3
5 changed files with 101 additions and 0 deletions

View File

@@ -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)

53
etc/devel.conf Normal file
View File

@@ -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

View File

@@ -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]

View File

@@ -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()

View File

@@ -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