Golang Log Simple Use example detailed

Source: Internet
Author: User
Tags local time

The

Golang Log standard library implements a simple logging service. But does not support log classification, classification, filtering and other complex functions. By a third-party library such as Glog (Github), Seelog (Github)
Simple demo is as follows:
Package main
 
Import (
  "log"
  "OS
)
Func main ()   {
 
 file,err: = os. Create ("Test.log")
 if err!= nil {
  log. Fatalln ("Fail to create Test.log file!")
 }
 defer file. Close ()
 
 logger: = log. New (file, "info:", log. Lstdflags|log. Lshortfile)
 
 logger. Println (' This is phpddt.com testing ')//info:2015/11/15 01:11:48 test.go:17:this is phpddt.com testing
}

Visible, the specified prefix can achieve a simple rating.
View the logger structure by viewing the source code:
Type Logger struct {
Mu sync. Mutex//ensures Atomic writes; Protects the following fields
Prefix string//prefix to write at beginning of
Flag Int//Properties
Out IO. Writer//destination for output
BUF []byte//For accumulating text to write
}

Constant for log format flag:
Const (
Ldate = 1 << iota//The date in the local time ZONE:2009/01/23
Ltime//The time zone:01:23:23
Lmicroseconds//Microsecond resolution:01:23:23.123123. Assumes Ltime.
Llongfile//full file name and line number:/a/b/c/d.go:23
Lshortfile//Final file name element and line number:d.go:23. Overrides Llongfile
LUTC//If Ldate or Ltime is set, with UTC rather than the local time zone
Lstdflags = Ldate | Ltime//Initial values for the standard logger
)

The Go-log.pnggolang log implements three types of interfaces:
Print General message output
Fatal forced out
Panic output log after panic

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.