grim/convey

f2239a75d239
Parents 5fc49909bc01
Children 2d15c52b8c11
Make the logging look more like the old logging with logrus
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/logging/formatter.go Sat Jul 27 10:07:00 2019 -0500
@@ -0,0 +1,76 @@
+// Convey
+// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package logging
+
+import (
+ "os"
+
+ "github.com/mgutz/ansi"
+ log "github.com/sirupsen/logrus"
+)
+
+type Formatter struct {
+ fmt *log.TextFormatter
+}
+
+func NewFormatter(color bool) log.Formatter {
+ fmt := &log.TextFormatter{
+ FullTimestamp: false,
+ }
+
+ if color {
+ switch os.Getenv("TERM") {
+ case "win":
+ fallthrough
+ case "dumb":
+ fmt.DisableColors = true
+ }
+ } else {
+ fmt.DisableColors = true
+ }
+
+ return &Formatter{
+ fmt: fmt,
+ }
+}
+
+func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
+ id, found := entry.Data["id"]
+ if found {
+ delete(entry.Data, "id")
+ }
+
+ idColor, found := entry.Data["idColor"]
+ if found {
+ delete(entry.Data, "idColor")
+ }
+
+ if id != nil && idColor != nil {
+ prefix := ""
+ if f.fmt.DisableColors {
+ prefix = id.(string) + " "
+ } else {
+ prefix = idColor.(string) + id.(string) + ansi.Reset
+ }
+
+ entry.Message = prefix + " " + entry.Message
+ }
+
+ bytes, err := f.fmt.Format(entry)
+
+ return bytes, err
+}
--- a/logging/logging.go Wed May 22 00:36:58 2019 -0500
+++ b/logging/logging.go Sat Jul 27 10:07:00 2019 -0500
@@ -23,11 +23,6 @@
log "github.com/sirupsen/logrus"
)
-var (
- logTemplate = `{{.Timestamp.Format "2006-01-02 15:04:05.000"}} {{index .Attrs "id"}}: {{.Message}}`
- logColorTemplate = `{{.Timestamp.Format "2006-01-02 15:04:05.000"}} {{index .Attrs "idColor"}}{{index .Attrs "id"}}{{reset}}: {{color}}{{.Message}}{{reset}}`
-)
-
// Setup sets up the logging system based on the input parameters
func Setup(color, verbose bool) {
log.SetOutput(os.Stdout)
@@ -38,22 +33,7 @@
log.SetLevel(log.InfoLevel)
}
- fmt := &log.TextFormatter{
- FullTimestamp: true,
- }
-
- if color {
- switch os.Getenv("TERM") {
- case "win":
- fallthrough
- case "dumb":
- fmt.DisableColors = true
- }
- } else {
- fmt.DisableColors = true
- }
-
- log.SetFormatter(fmt)
+ log.SetFormatter(NewFormatter(color))
}
func Shutdown() {