-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_internal.go
70 lines (58 loc) · 1.33 KB
/
logger_internal.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
package go_logger
import (
"encoding/json"
"reflect"
"time"
)
//------------------------------------------------------------------------------
type globalOptions struct {
// Set the initial logging level to use.
Level LogLevel
// Set the initial logging level for debug output to use.
DebugLevel uint
// A callback to call if an internal error is encountered.
ErrorHandler ErrorHandler
}
//------------------------------------------------------------------------------
func (logger *Logger) getTimestamp() time.Time {
now := time.Now()
if !logger.useLocalTime {
now = now.UTC()
}
return now
}
func (logger *Logger) parseObj(obj interface{}) (msg string, isJSON bool, ok bool) {
// Quick check for strings, structs or pointer to strings or structs
refObj := reflect.ValueOf(obj)
switch refObj.Kind() {
case reflect.Ptr:
if !refObj.IsNil() {
switch refObj.Elem().Kind() {
case reflect.String:
msg = *(obj.(*string))
ok = true
case reflect.Struct:
// Marshal struct
b, err := json.Marshal(obj)
if err == nil {
msg = string(b)
isJSON = true
ok = true
}
}
}
case reflect.String:
msg = obj.(string)
ok = true
case reflect.Struct:
// Marshal struct
b, err := json.Marshal(obj)
if err == nil {
msg = string(b)
isJSON = true
ok = true
}
}
// Done
return
}