Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil-durge authored Jun 8, 2018
1 parent b5d77a1 commit 09eccf1
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@
)

func main() {
log.Printf("test info log formatted with var %v, var %v ", 1, "two")
log.Printf("test log with var %v, var %v", 1, "two")
doSomethingError()
doSomethingDebug()
}

func doSomethingError() {
log.Printf("test error log formatted with var %v, var %v ", 1, "two")
log.Printf("test log with var %v, var %v", 2, "three")
}

func doSomethingDebug() {
log.Printf("test debug log formatted with var %v, var %v ", 1, "two")
log.Printf("test log with var %v, var %v", "one", 2)
}

This prints-

2018/06/08 18:28:49 test info log formatted with var 1, var two
2018/06/08 18:28:49 test error log formatted with var 2, var three
2018/06/08 18:28:49 test debug log formatted with var one, var 2
2018/06/08 18:28:49 test log with var 1, var two
2018/06/08 18:28:49 test log with var 2, var three
2018/06/08 18:28:49 test log with var one, var 2

But then you deploy your code to production, you look at your logs and see this sort of thing:

2018/06/08 18:28:49 test info log formatted with var 1, var two
2018/06/08 18:28:49 test info log formatted with var 1, var two
2018/06/08 18:28:50 test error log formatted with var 2, var three
2018/06/08 18:28:50 test debug log formatted with var one, var 2
2018/06/08 18:28:50 test error log formatted with var 2, var three
2018/06/08 18:28:51 test debug log formatted with var one, var 2
2018/06/08 18:28:49 test log with var 1, var two
2018/06/08 18:28:49 test log with var 1, var two
2018/06/08 18:28:50 test log with var 2, var three
2018/06/08 18:28:50 test log with var one, var 2
2018/06/08 18:28:50 test log with var 2, var three
2018/06/08 18:28:51 test log with var one, var 2


Mr. Akhil reports something went wrong. But because your program is a high performance parallelized wonder, you can’t be sure which line relates to his request. There is just no link between logs, neither is there any INFO/ERROR/DEBUG tag to know something went wrong in first look.
Mr. Akhil reports something went wrong. But because your program is a high performance parallelized wonder, you can’t be sure which line relates to his request. There is just no link between logs, neither is there any INFO/ERROR/DEBUG tag to know something went wrong.

# Solution
Use some simple implementation like in this project. Just copy directories apicontext and logger in your project, and you can write the same code above like-
Expand All @@ -57,26 +57,26 @@

func main() {
ctx := apicontext.WithReqID(context.Background())
logger.Info(ctx, "test info log formatted with var %v, var %v ", 1, "two")
logger.Info(ctx, "test log with var %v, var %v", 1, "two")
doSomethingError(ctx)
doSomethingDebug(ctx)
}

func doSomethingError(ctx context.Context) {
logger.Error(ctx, "test error log formatted with var %v, var %v ", 1, "two")
logger.Error(ctx, "test log with var %v, var %v", 1, "two")
}

func doSomethingDebug(ctx context.Context) {
logger.Debug(ctx, "test debug log formatted with var %v, var %v ", 1, "two")
logger.Debug(ctx, "test log with var %v, var %v", 1, "two")
}

To give you output like-

2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:11 INFO: test info log formatted with var 1, var two
2018/06/08 18:32:19 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:11 INFO: test info log formatted with var 1, var two
2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:17 ERROR: test error log formatted with var 2, var three
2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:21 DEBUG: test debug log formatted with var one, var 2
2018/06/08 18:32:20 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:17 ERROR: test error log formatted with var 2, var three
2018/06/08 18:32:20 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:21 DEBUG: test debug log formatted with var one, var 2
2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:11 INFO: test log with var 1, var two
2018/06/08 18:32:19 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:11 INFO: test log with var 1, var two
2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:17 ERROR: test log with var 2, var three
2018/06/08 18:32:19 [88153e20-efe3-40cb-ad68-4ba8f6a1e19a:0:0] main.go:21 DEBUG: test log with var one, var 2
2018/06/08 18:32:20 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:17 ERROR: test log with var 2, var three
2018/06/08 18:32:20 [b000a173-713b-430b-8815-6ac5cfec50ba:0:0] main.go:21 DEBUG: test log with var one, var 2

As you can see, just by first look, you are able to get the link between different logs and are able to figure out if it's INFO/ERROR/DEBUG. Simple.
As you can see, just by one look, you are able to spot the link between different logs and are able to figure out if it's INFO/ERROR/DEBUG. Simple.

0 comments on commit 09eccf1

Please sign in to comment.