Remove logrus to allow for easier customization

This commit is contained in:
Arthur Barr
2018-02-22 17:29:06 +00:00
parent b497a04dcb
commit 91bf65ab57
52 changed files with 241 additions and 3795 deletions

129
internal/logger/logger.go Normal file
View File

@@ -0,0 +1,129 @@
/*
© Copyright IBM Corporation 2018
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logger
import (
"encoding/json"
"fmt"
"io"
"os"
"time"
)
// timestampFormat matches the format used by MQ messages (includes milliseconds)
const timestampFormat string = "2006-01-02T15:04:05.000Z07:00"
const debugLevel string = "DEBUG"
const infoLevel string = "INFO"
const errorLevel string = "ERROR"
// A Logger is used to log messages to stdout
type Logger struct {
writer io.Writer
debug bool
json bool
processName string
pid int
}
func NewLogger(writer io.Writer, debug bool, json bool) *Logger {
return &Logger{
writer: writer,
debug: debug,
json: json,
processName: os.Args[0],
pid: os.Getpid(),
}
}
func (l *Logger) format(entry map[string]interface{}) (string, error) {
if l.json {
b, err := json.Marshal(entry)
if err != nil {
return "", err
}
return string(b), err
}
return fmt.Sprintf("%v %v\n", entry["ibm_datetime"], entry["message"]), nil
}
// log logs a message at the specified level. The message is enriched with
// additional fields.
func (l *Logger) log(level string, msg string) {
t := time.Now()
entry := map[string]interface{}{
"message": fmt.Sprint(msg),
"ibm_datetime": t.Format(timestampFormat),
"loglevel": level,
"ibm_processName": l.processName,
"ibm_processId": l.pid,
}
s, err := l.format(entry)
if err != nil {
// TODO: Fix this
fmt.Println(err)
}
if l.json {
fmt.Fprintln(l.writer, s)
} else {
fmt.Fprint(l.writer, s)
}
}
func (l *Logger) LogDirect(msg string) {
fmt.Println(msg)
}
func (l *Logger) Debug(args ...interface{}) {
if l.debug {
l.log(debugLevel, fmt.Sprint(args...))
}
}
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.debug {
l.log(debugLevel, fmt.Sprintf(format, args...))
}
}
func (l *Logger) Print(args ...interface{}) {
l.log(infoLevel, fmt.Sprint(args...))
}
func (l *Logger) Println(args ...interface{}) {
l.Print(args...)
}
func (l *Logger) Printf(format string, args ...interface{}) {
l.log(infoLevel, fmt.Sprintf(format, args...))
}
func (l *Logger) PrintString(msg string) {
l.log(infoLevel, msg)
}
func (l *Logger) Error(args ...interface{}) {
l.log(errorLevel, fmt.Sprint(args...))
}
func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(errorLevel, fmt.Sprintf(format, args...))
}
// TODO: Remove this
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log("FATAL", fmt.Sprintf(format, args...))
}

View File

@@ -0,0 +1,49 @@
/*
© Copyright IBM Corporation 2018
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logger
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestJSONLogger(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(buf, true, true)
s := "Hello world"
l.Print(s)
var e map[string]interface{}
err := json.Unmarshal([]byte(buf.String()), &e)
if err != nil {
t.Error(err)
}
if s != e["message"] {
t.Errorf("Expected JSON to contain message=%v; got %v", s, buf.String())
}
}
func TestSimpleLogger(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(buf, true, false)
s := "Hello world"
l.Print(s)
if !strings.Contains(buf.String(), s) {
t.Errorf("Expected log output to contain %v; got %v", s, buf.String())
}
}