Remove logrus to allow for easier customization
This commit is contained in:
129
internal/logger/logger.go
Normal file
129
internal/logger/logger.go
Normal 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...))
|
||||
}
|
||||
49
internal/logger/logger_test.go
Normal file
49
internal/logger/logger_test.go
Normal 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())
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,6 @@ package ready
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const fileName string = "/run/runmqserver/ready"
|
||||
@@ -39,7 +37,6 @@ func fileExists() (bool, error) {
|
||||
|
||||
// Clear ensures that any readiness state is cleared
|
||||
func Clear() error {
|
||||
log.Debug("Clear()")
|
||||
exist, err := fileExists()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -53,7 +50,6 @@ func Clear() error {
|
||||
// Set lets any subsequent calls to `CheckReady` know that the queue
|
||||
// manager has finished its configuration step
|
||||
func Set() error {
|
||||
log.Debug("Set()")
|
||||
return ioutil.WriteFile(fileName, []byte("1"), 0770)
|
||||
}
|
||||
|
||||
@@ -62,9 +58,7 @@ func Set() error {
|
||||
func Check() (bool, error) {
|
||||
exists, err := fileExists()
|
||||
if err != nil {
|
||||
log.Debug("Check() -> false")
|
||||
return false, err
|
||||
}
|
||||
log.Debugf("Check() -> %v", exists)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user