1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

added logging helper

This commit is contained in:
Matthias Hannig
2018-10-02 13:04:48 +02:00
parent 0e0739c1ca
commit c3f53f5cb9
2 changed files with 50 additions and 0 deletions

36
backend/api_logging.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"fmt"
"log"
"strings"
)
// Log an api error
func apiLogError(module string, params ...interface{}) {
var err error
args := []string{}
// Build args string and get error from params
for _, p := range params {
// We have our error
if e, ok := p.(error); ok {
err = e
continue
}
args = append(args, fmt.Sprintf("%v", p))
}
if err != nil {
log.Println(fmt.Sprintf(
"API :: %s(%s) :: ERROR: %v",
module, strings.Join(args, ", "), err,
))
} else {
log.Println(fmt.Sprintf(
"API :: %s(%s)",
module, strings.Join(args, ", "),
))
}
}

View File

@@ -0,0 +1,14 @@
package main
import (
"fmt"
"testing"
)
func TestApiLogError(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)
}