-
Notifications
You must be signed in to change notification settings - Fork 206
/
logger.go
78 lines (65 loc) · 2.23 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package log provides centralized logging for the Documize application.
package log
import (
"bytes"
"fmt"
"os"
"runtime"
log "github.com/Sirupsen/logrus"
env "github.com/documize/community/core/environment"
)
var environment = "Non-production"
func init() {
log.SetFormatter(new(log.TextFormatter))
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
env.GetString(&environment, "log", false,
"system being logged e.g. 'PRODUCTION'",
func(*string, string) bool {
log.Infoln(environment + " environment logging enabled")
return false
})
}
// Debug logs a message for debug purposes.
func Debug(message string) {
log.WithFields(log.Fields{"env": environment}).Debug(message)
}
// Info logs a message for information purposes.
func Info(message string) {
log.WithFields(log.Fields{"env": environment}).Info(message)
}
// TestIfErr is used by the test code to signal that a test being run should error, it is reset if an error occurs.
var TestIfErr bool
// ErrorString logs an error, where there is not an error value.
func ErrorString(message string) {
TestIfErr = false
log.WithFields(log.Fields{"env": environment}).Error(message)
}
// Error logs an error, if non-nil, with a message to give some context.
func Error(message string, err error) {
if err != nil {
TestIfErr = false
stack := make([]byte, 4096)
runtime.Stack(stack, false)
if idx := bytes.IndexByte(stack, 0); idx > 0 && idx < len(stack) {
stack = stack[:idx] // remove trailing nulls from stack dump
}
log.WithFields(log.Fields{"env": environment, "error": err.Error(), "stack": fmt.Sprintf("%s", stack)}).Error(message)
//log.WithField("error: "+message, err.Error()).Errorf("%q\n%s\n", err, stack[:])
}
}
// IfErr logs an error if one exists.
// It is a convenience wrapper for Error(), with no context message.
func IfErr(err error) {
Error("", err)
}