Log processing in the go language, using a log packet

Source: Internet
Author: User

Golang provides a native log library "log", which is easy to use and is described in code as an example.

Package Mainimport ("OS" "Log" "FMT") func main () {//open log File///The second parameter is open file mode, optional as follows:/* o_rdonly//Only Read mode Open file O_wronly//write-only mode open file O_rdwr//read-write mode open file O_append//write data append to file tail o_create//if not present     A new file will be created O_EXCL//and O_create together, the file must not exist O_sync//Open file for synchronous I/O o_trunc//If possible, empty the file when open */ The third parameter is file permissions, refer to Linux file permissions, 664 here for octal, for: rw-rw-r--logFile, err: = OS. OpenFile ("E:/go.log", OS. o_wronly | Os. O_create | Os. O_append, 0644) if err! = Nil {log. Fatal (ERR)}//The first parameter is output IO, which can be a file or an object that implements the interface, here is a log file, the second parameter is a custom prefix, the third parameter is the format option for the output log, and the optional combination///third parameter can be selected as follows:/* Ld             ate = 1//date: 2009/01/23 Ltime = 2//time: 01:23:23 lmicroseconds = 4        Microsecond resolution: 01:23:23.123123 (for enhanced ltime bit) Llongfile = 8//File full path name + line number:/a/b/c/d.go:23 Lshortfile = 16//File no path name + line number: d.go:23 (will overwrite llongfile) Lstdflags = Ldate | Ltime//Standard Logger initial value */debuglog: = log. New (LogFile, "[Debug]", log. Ldate|log. Ltime|log. llongfile)//Log output Debuglog.print ("Log test print output, processing with FMT. Print ") debuglog.println (" Log test Println output, processing with FMT. Println ") debuglog.printf (" Log test%s output, processing with FMT. printf "," printf ")//log output, while terminating the program directly, subsequent operations will not execute Debuglog.fatal (" Log test Fatal output, processing equivalent to: Debuglog.print (), then execute the OS. " Exit (1) ") Debuglog.fatalln (" Log test Fatalln output, processing equivalent to: Debuglog.println (), then execute OS. Exit (1) ") debuglog.fatalf (" Log test%s output, processing equivalent to: Debuglog.print (), then execute OS. ") Exit (1) "," fatalf ")//log output, while throwing an exception, available recover snap defer func () {if r: = Recover (); r! = nil {fmt. Println ("===========", R)}} () Debuglog.panic ("Log test Panic output, processing equivalent to: Debuglog.print (), then execute Panic ()") Debuglog. PANICLN ("Log test PANICLN output, processing equivalent to: Debuglog.println (), then execute panic ()") DEBUGLOG.PANICF ("Log test%s output, processing equivalent to: debuglog.printf () After that, execute panic () "," PANICF ") fmt. Println ("prefix:", Debuglog.prefix ())///prefix: [Debug] FMT. Println ("Output option is:", Debuglog.flags ())//Output options are: 11//Set prefix Debuglog.setprefix ("[INFO]")//Set OUTPUT option Debuglog.setflags (log. Lstdflags) fmt. Println ("prefix:", Debuglog.prefix ())///prefix: [INFO] FMT. Println ("Output option:", Debuglog.flags ())//Output option: 3}

Note: The above code executes when you want to fragment the comment, otherwise execute to:

debugLog.Fatal("日志测试Fatal输出,处理等价于:debugLog.Print()后,再执行os.Exit(1)")

Will terminate

Log processing in the go language, using a log packet

Related Article

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.