/
gf_log.go
168 lines (127 loc) · 4.56 KB
/
gf_log.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_core
import (
"os"
"fmt"
"time"
"strconv"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
)
//-------------------------------------------------
type GFlogFun func(string, string, map[string]interface{})
//-------------------------------------------------
func LogsInit() (func(string, string), GFlogFun) {
return LogsInitNew(true, "")
}
// pLogLevelStr - allows for explicit programmatic setting of log_level.
// if this is set to "" then the ENV var is checked.
// if this is also not set then the default "info" level is used.
func LogsInitNew(pLogrusBool bool, pLogLevelStr string) (func(string, string), GFlogFun) {
//--------------------
// LOGRUS_INIT
if pLogrusBool {
//--------------------
// LOG_LEVEL
var logLevelStr string
logLevelDefaultStr := "info"
if pLogLevelStr != "" {
logLevelStr = pLogLevelStr
} else {
logLevelENVstr := os.Getenv("GF_LOG_LEVEL")
if logLevelENVstr == "" {
logLevelStr = logLevelDefaultStr
} else {
logLevelStr = logLevelENVstr
}
}
fmt.Printf("log level - %s\n", logLevelStr)
level, err := logrus.ParseLevel(logLevelStr)
if err != nil {
fmt.Println(err)
panic("log level is not valid, has to be : trace, debug, info, warning, error, fatal, panic")
}
// set loging severity level, and above.
// the level that was set will include that level and all severities higher than that
logrus.SetLevel(level)
//--------------------
// log := logrus.New()
// log to stdout instead of the default stderr
logrus.SetOutput(os.Stdout)
// Log as JSON instead of the default ASCII formatter.
// log.SetFormatter(&log.JSONFormatter{})
logrus.SetFormatter(&logrus.TextFormatter{
// DisableColors: true,
FullTimestamp: true,
})
// adds the caller function as 'method' to the output
// log.SetReportCaller(true)
}
//--------------------
green := color.New(color.FgGreen).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
//-------------------------------------------------
// DEPRECATED!! - when all logging is migrated to logNewFun delete this function.
logFun := func(pGroupStr string, pMsgStr string) {
timeStr := strconv.FormatFloat(float64(time.Now().UnixNano())/1000000000.0, 'f', 10, 64)
if pGroupStr == "FUN_ENTER" {
fmt.Printf(timeStr+":"+yellow(pGroupStr)+":"+pMsgStr+"\n")
} else if pGroupStr == "INFO" {
fmt.Printf(timeStr+":"+green(pGroupStr)+":"+green(pMsgStr)+"\n")
} else if pGroupStr == "ERROR" {
fmt.Printf(timeStr+":"+red(pGroupStr)+":"+pMsgStr+"\n")
}
}
//-------------------------------------------------
// IMPORTANT!! - migrate all loging to this function
logNewFun := func(pLevelStr string, pMsgStr string, pMetaMap map[string]interface{}) {
logFields := logrus.Fields{}
if pMetaMap != nil {
for k, v := range pMetaMap {
logFields[k] = v
}
// contextLogger := logrus.WithFields(logFields)
}
switch pLevelStr {
// very low-level logs
case "TRACE":
logrus.WithFields(logFields).Trace(pMsgStr)
// debugging logs for devs or troubleshooting
case "DEBUG":
logrus.WithFields(logFields).Debug(pMsgStr)
// informative logs for general observation
case "INFO":
logrus.WithFields(logFields).Info(pMsgStr)
case "WARNING":
logrus.WithFields(logFields).Warn(pMsgStr)
// failure occured, but the process is not exiting
case "ERROR":
logrus.WithFields(logFields).Error(pMsgStr)
// process will exit after this log
case "FATAL":
logrus.WithFields(logFields).Fatal(pMsgStr)
}
}
//-------------------------------------------------
return logFun, logNewFun
}
//-------------------------------------------------
func LogsIsDebugEnabled() bool {
level, _ := logrus.ParseLevel("debug")
return logrus.IsLevelEnabled(level)
}