-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Akhil Durge
committed
Jun 8, 2018
1 parent
885c6dc
commit 085a159
Showing
6 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/satori/go.uuid" | ||
version = "1.2.0" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package apicontext | ||
|
||
import ( | ||
"context" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type contextKey int | ||
|
||
const ( | ||
reqIDKey contextKey = 0 | ||
clientIDKey contextKey = 1 | ||
userIDKey contextKey = 2 | ||
) | ||
|
||
// WithReqID returns a new Context that carries request ID | ||
func WithReqID(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, reqIDKey, uuid.NewV4().String()) | ||
} | ||
|
||
// WithClientID returns a new Context that carries client ID | ||
func WithClientID(ctx context.Context, clientID uint) context.Context { | ||
return context.WithValue(ctx, clientIDKey, clientID) | ||
} | ||
|
||
// WithUserID returns a new Context that carries user ID | ||
func WithUserID(ctx context.Context, userID uint) context.Context { | ||
return context.WithValue(ctx, userIDKey, userID) | ||
} | ||
|
||
//ReqID returns the requestID from context. | ||
//Returns 0 if requestID is not set in context | ||
func ReqID(ctx context.Context) string { | ||
reqID := ctx.Value(reqIDKey) | ||
if reqID != nil { | ||
return reqID.(string) | ||
} | ||
return "" | ||
} | ||
|
||
//ClientID returns the client_id of authorized client from context. | ||
//Returns 0 if client_id is not set in context | ||
func ClientID(ctx context.Context) uint { | ||
clientID := ctx.Value(clientIDKey) | ||
if clientID != nil { | ||
return clientID.(uint) | ||
} | ||
return 0 | ||
} | ||
|
||
//UserID returns user_id of authenticated user of a client from context. | ||
//Returns 0 if user_id is not set in context | ||
func UserID(ctx context.Context) uint { | ||
userID := ctx.Value(userIDKey) | ||
if userID != nil { | ||
return userID.(uint) | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"log-context/apicontext" | ||
"runtime" | ||
) | ||
|
||
//Info logs with context | ||
func Info(ctx context.Context, msg string, v ...interface{}) { | ||
ctxString := fmt.Sprintf("[%v:%v:%v] %v", apicontext.ReqID(ctx), apicontext.ClientID(ctx), apicontext.UserID(ctx), caller()) | ||
log.Printf("%v INFO: %v", ctxString, fmt.Sprintf(msg, v...)) | ||
} | ||
|
||
//Error logs with context | ||
func Error(ctx context.Context, msg string, v ...interface{}) { | ||
ctxString := fmt.Sprintf("[%v:%v:%v] %v", apicontext.ReqID(ctx), apicontext.ClientID(ctx), apicontext.UserID(ctx), caller()) | ||
log.Printf("%v ERROR: %v", ctxString, fmt.Sprintf(msg, v...)) | ||
} | ||
|
||
//Debug logs with context | ||
func Debug(ctx context.Context, msg string, v ...interface{}) { | ||
ctxString := fmt.Sprintf("[%v:%v:%v] %v", apicontext.ReqID(ctx), apicontext.ClientID(ctx), apicontext.UserID(ctx), caller()) | ||
log.Printf("%v DEBUG: %v", ctxString, fmt.Sprintf(msg, v...)) | ||
} | ||
|
||
//to give you name of the file and line number from where logs are emitted | ||
func caller() string { | ||
_, file, no, ok := runtime.Caller(2) | ||
if ok { | ||
short := file | ||
for i := len(file) - 1; i > 0; i-- { | ||
if file[i] == '/' { | ||
short = file[i+1:] | ||
break | ||
} | ||
} | ||
file = short | ||
return fmt.Sprintf("%v:%v ", file, no) | ||
} | ||
return "???:0 " | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log-context/apicontext" | ||
"log-context/logger" | ||
) | ||
|
||
func main() { | ||
ctx := apicontext.WithReqID(context.Background()) | ||
logger.Info(ctx, "test info log formatted with var %v, var %v ", 1, "two") | ||
logger.Error(ctx, "test error log formatted with var %v, var %v ", 1, "two") | ||
logger.Debug(ctx, "test debug log formatted with var %v, var %v ", 1, "two") | ||
} |