File tree 4 files changed +39
-2
lines changed
4 files changed +39
-2
lines changed Original file line number Diff line number Diff line change
1
+ * .test
2
+ .idea
3
+ * .iml
4
+ * ~
Original file line number Diff line number Diff line change 1
1
package logging
2
2
3
- import "os"
3
+ import (
4
+ "os"
5
+ "time"
6
+ )
4
7
5
8
func Example () {
6
9
// This call is for testing purposes and will set the time to unix epoch.
@@ -27,6 +30,11 @@ func Example() {
27
30
backend2Leveled := AddModuleLevel (backend2Formatter )
28
31
backend2Leveled .SetLevel (ERROR , "" )
29
32
33
+ // Log UTC time, default is Local
34
+ SetTimeNow (func () time.Time {
35
+ return time .Now ().UTC ()
36
+ })
37
+
30
38
// Set the backends to be used and the default level.
31
39
SetBackend (backend1 , backend2Leveled )
32
40
Original file line number Diff line number Diff line change @@ -123,6 +123,11 @@ func MustGetLogger(module string) *Logger {
123
123
return logger
124
124
}
125
125
126
+ // SetTimeNow configures the time.Time value generated for log time values
127
+ func SetTimeNow (now func () time.Time ) {
128
+ timeNow = now
129
+ }
130
+
126
131
// Reset restores the internal state of the logging library.
127
132
func Reset () {
128
133
// TODO make a global Init() method to be less magic? or make it such that
Original file line number Diff line number Diff line change 4
4
5
5
package logging
6
6
7
- import "testing"
7
+ import (
8
+ "testing"
9
+ "time"
10
+ )
8
11
9
12
type Password string
10
13
@@ -60,3 +63,20 @@ func TestPrivateBackend(t *testing.T) {
60
63
t .Error ("logged to defaultBackend:" , MemoryRecordN (privateBackend , 0 ))
61
64
}
62
65
}
66
+
67
+ func TestLogger_SetTimerNow (t * testing.T ) {
68
+ _ = InitForTesting (DEBUG )
69
+ _ = MustGetLogger ("test" )
70
+
71
+ if now := timeNow (); now != time .Unix (0 , 0 ).UTC () {
72
+ t .Error ("test timeNow incorrect" , now )
73
+ }
74
+
75
+ SetTimeNow (func () time.Time {
76
+ return time .Unix (1 , 1 )
77
+ })
78
+
79
+ if now := timeNow (); now != time .Unix (1 , 1 ) {
80
+ t .Error ("test timeNow dit not get overwritten" , now )
81
+ }
82
+ }
You can’t perform that action at this time.
0 commit comments