@@ -4,25 +4,41 @@ import (
44 "context"
55 "fmt"
66 "log"
7+ "path/filepath"
78 "runtime"
89)
910
11+ const (
12+ panicLevel = "PANIC"
13+ errorLevel = "ERROR"
14+ infoLevel = "INFO"
15+ debugLevel = "DEBUG"
16+ )
17+
1018// RequestIDKey is used together with context for setting/getting X-Request-ID
1119type RequestIDKey struct {}
1220
21+ // LogLevel
22+ type LogLevelKey struct {}
23+
1324// LogError logs error message
1425func LogError (ctx context.Context , format string , a ... interface {}) string {
15- return logLevel (ctx , "ERROR" , format , a ... )
26+ return logLevel (ctx , errorLevel , format , a ... )
1627}
1728
1829// LogInfo logs info message
1930func LogInfo (ctx context.Context , format string , a ... interface {}) string {
20- return logLevel (ctx , "INFO" , format , a ... )
31+ return logLevel (ctx , infoLevel , format , a ... )
32+ }
33+
34+ // LogDebug logs debug message
35+ func LogDebug (ctx context.Context , format string , a ... interface {}) string {
36+ return logLevel (ctx , debugLevel , format , a ... )
2137}
2238
2339// LogPanic logs error message
2440func LogPanic (ctx context.Context , format string , a ... interface {}) string {
25- return logLevel (ctx , "PANIC" , format , a ... )
41+ return logLevel (ctx , panicLevel , format , a ... )
2642}
2743
2844// Log logs message with a given level with no request context
@@ -37,14 +53,22 @@ func Log(level string, format string, a ...interface{}) string {
3753}
3854
3955func logLevel (ctx context.Context , level string , format string , a ... interface {}) string {
40- _ , file , line , _ := runtime .Caller (2 )
4156
42- requestID := ctx .Value (RequestIDKey {})
43- message := fmt .Sprintf (format , a ... )
57+ logLevel := fmt .Sprintf ("%v" , ctx .Value (LogLevelKey {}))
4458
45- log .SetFlags (log .Ldate | log .Ltime | log .Lmicroseconds | log .LUTC )
46- log .Printf ("[%v:%v] %v requestId=%v %v" , file , line , level , requestID , message )
47- return message
59+ if shouldLogMessage (logLevel , level ) {
60+ requestID := ctx .Value (RequestIDKey {})
61+ message := fmt .Sprintf (format , a ... )
62+ _ , file , line , _ := runtime .Caller (2 )
63+ filename := filepath .Base (file )
64+
65+ log .SetFlags (log .Ldate | log .Ltime | log .Lmicroseconds | log .LUTC )
66+ log .Printf ("[%v:%v] %v requestId=%v %v" , filename , line , level , requestID , message )
67+
68+ return message
69+ }
70+
71+ return ""
4872}
4973
5074// FindNthIndex finds index of nth occurance of a character c in string str
@@ -60,3 +84,27 @@ func FindNthIndex(str string, c byte, n int) int {
6084 }
6185 return - 1
6286}
87+
88+ func shouldLogMessage (configLogLevel , targetLevel string ) bool {
89+ // if configLogLevel and targetLevel match then log
90+ if configLogLevel == targetLevel {
91+ return true
92+ }
93+ // if configLogLevel is debug then all messages are logged no need to check targetLevel
94+ if configLogLevel == debugLevel {
95+ return true
96+ }
97+ // if configLogLevel not set then INFO is assumed
98+ // if INFO then all levels should log except of debug
99+ if (len (configLogLevel ) == 0 || configLogLevel == infoLevel ) && targetLevel != debugLevel {
100+ return true
101+ }
102+
103+ // if logLevel is ERROR then only ERROR and PANIC are logged
104+ // ERROR is covered in the beginning of method so need to check only Panic level
105+ if configLogLevel == errorLevel && targetLevel == panicLevel {
106+ return true
107+ }
108+
109+ return false
110+ }
0 commit comments