This is a creation in Article, where the information may have evolved or changed.
The difference between using log and using FMT
The original is to print directly, and then a little upgrade, such as the time of the output, plus the concurrency between the goroutine (printing information is not necessarily in accordance with the order you ordered the output of each output may be different), according to different log levels to print ... In addition to printing and dump, a special log packet is required to log the program's information.
Imagine that a large project has a lot of output information, and if the output information is not preceded by a date, debugging is almost unimaginable, because it is impossible to stare at the screen to monitor the information.
Therefore, the use of log to replace the FMT has several main reasons:
- Added output time
- Thread Safety
- Easy to dump log information to form a log file
For the basic structure of logger, you can refer to this blog:
http://gotaly.blog.51cto.com/8861157/1406905
Look at the structure of logger, in addition to the basic information, there are many other functions:
http://studygolang.com/articles/3383
Specifically understand that log is thread-safe, you can refer to this blog:
http://blog.csdn.net/paladinosment/article/details/42666147
and a log dump print.
When you create a new object, you can directly output to the standard output by controlling the first parameter, or you can save it to a file.
If you just want to output to the terminal, you can call log directly. The XXX method does not have to be used as a logger object, and then goes through the logger object to invoke. In the log packet, there are some methods directly to the implementation of logger, some are direct library functions, can be used directly.
http://www.sharejs.com/codes/go/7308
About the t.logf of the Unit test
In fact, the use of similar logs methods in the internal, all of the basic parallel operation is encapsulated after processing, coupled with the mutex operation, so in the unit test is not recommended to use the method of fmt.printf directly.