mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 01:57:02 +00:00
Make log level configurable
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/robfig/goconfig/config"
|
"github.com/robfig/goconfig/config"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SmtpConfig houses the SMTP server configuration - not using pointers
|
// SmtpConfig houses the SMTP server configuration - not using pointers
|
||||||
@@ -52,6 +53,7 @@ func LoadConfig(filename string) error {
|
|||||||
messages := list.New()
|
messages := list.New()
|
||||||
|
|
||||||
// Validate sections
|
// Validate sections
|
||||||
|
requireSection(messages, "logging")
|
||||||
requireSection(messages, "smtp")
|
requireSection(messages, "smtp")
|
||||||
requireSection(messages, "web")
|
requireSection(messages, "web")
|
||||||
requireSection(messages, "datastore")
|
requireSection(messages, "datastore")
|
||||||
@@ -64,6 +66,7 @@ func LoadConfig(filename string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate options
|
// Validate options
|
||||||
|
requireOption(messages, "logging", "level")
|
||||||
requireOption(messages, "smtp", "ip4.address")
|
requireOption(messages, "smtp", "ip4.address")
|
||||||
requireOption(messages, "smtp", "ip4.port")
|
requireOption(messages, "smtp", "ip4.port")
|
||||||
requireOption(messages, "smtp", "domain")
|
requireOption(messages, "smtp", "domain")
|
||||||
@@ -91,6 +94,21 @@ func LoadConfig(filename string) error {
|
|||||||
return err
|
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
|
// parseSmtpConfig trying to catch config errors early
|
||||||
func parseSmtpConfig() error {
|
func parseSmtpConfig() error {
|
||||||
smtpConfig = new(SmtpConfig)
|
smtpConfig = new(SmtpConfig)
|
||||||
|
|||||||
53
etc/devel.conf
Normal file
53
etc/devel.conf
Normal 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
|
||||||
@@ -7,6 +7,12 @@
|
|||||||
# Not used by directly, but is typically referenced below in %()s format.
|
# Not used by directly, but is typically referenced below in %()s format.
|
||||||
install.dir=.
|
install.dir=.
|
||||||
|
|
||||||
|
#############################################################################
|
||||||
|
[logging]
|
||||||
|
|
||||||
|
# Options from least to most verbose: ERROR, WARN, INFO, TRACE
|
||||||
|
level=INFO
|
||||||
|
|
||||||
#############################################################################
|
#############################################################################
|
||||||
[smtp]
|
[smtp]
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket/config"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"github.com/jhillyerd/inbucket/smtpd"
|
"github.com/jhillyerd/inbucket/smtpd"
|
||||||
"github.com/jhillyerd/inbucket/web"
|
"github.com/jhillyerd/inbucket/web"
|
||||||
"os"
|
"os"
|
||||||
@@ -32,6 +33,10 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure logging
|
||||||
|
level, _ := config.Config.String("logging", "level")
|
||||||
|
log.SetLogLevel(level)
|
||||||
|
|
||||||
// Startup SMTP server
|
// Startup SMTP server
|
||||||
server := smtpd.New()
|
server := smtpd.New()
|
||||||
go server.Start()
|
go server.Start()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogLevel int
|
type LogLevel int
|
||||||
@@ -15,6 +16,24 @@ const (
|
|||||||
|
|
||||||
var MaxLogLevel LogLevel = TRACE
|
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)
|
// Error logs a message to the 'standard' Logger (always)
|
||||||
func Error(msg string, args ...interface{}) {
|
func Error(msg string, args ...interface{}) {
|
||||||
msg = "[ERROR] " + msg
|
msg = "[ERROR] " + msg
|
||||||
|
|||||||
Reference in New Issue
Block a user