The difference between log and FMT in Golang

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.