refined api logging

This commit is contained in:
Matthias Hannig 2018-10-02 14:52:32 +02:00
parent c3f53f5cb9
commit af126507f3
2 changed files with 27 additions and 9 deletions

View File

@ -7,10 +7,17 @@ import (
)
// Log an api error
func apiLogError(module string, params ...interface{}) {
func apiLogSourceError(module string, sourceId int, params ...interface{}) {
var err error
args := []string{}
// Get source configuration
source := AliceConfig.Sources[sourceId]
sourceName := "unknown"
if source != nil {
sourceName = source.Name
}
// Build args string and get error from params
for _, p := range params {
// We have our error
@ -24,13 +31,13 @@ func apiLogError(module string, params ...interface{}) {
if err != nil {
log.Println(fmt.Sprintf(
"API :: %s(%s) :: ERROR: %v",
module, strings.Join(args, ", "), err,
"API ERROR :: %s.%s(%s) :: %v",
sourceName, module, strings.Join(args, ", "), err,
))
} else {
log.Println(fmt.Sprintf(
"API :: %s(%s)",
module, strings.Join(args, ", "),
"API ERROR :: %s.%s(%s)",
sourceName, module, strings.Join(args, ", "),
))
}
}

View File

@ -5,10 +5,21 @@ import (
"testing"
)
func TestApiLogError(t *testing.T) {
func TestApiLogSourceError(t *testing.T) {
err := fmt.Errorf("an unexpected error occured")
apiLogError("foo.bar", 23, "Test")
apiLogError("foo.bam", err)
apiLogError("foo.baz", 23, 42, "foo", err)
conf := &Config{
Sources: []*SourceConfig{
&SourceConfig{
Id: 0,
Name: "rs1.example.net (IPv4)",
},
},
}
AliceConfig = conf
apiLogSourceError("foo.bar", 0, 23, "Test")
apiLogSourceError("foo.bam", 0, err)
apiLogSourceError("foo.baz", 0, 23, 42, "foo", err)
}