Go Language Kee Log:log,logger

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Explain how Golang logs.

Log

The first is Golang's own package log. Use Godoc to view, and then you can view it on godoc -http=:8001 localhost:8001/pkg/log .

The most important thing is that the Setoutput function, the prototype is func SetOutput(w io.Writer) , determines where the log should be output, and the default is the standard output. Here is a simple code example that outputs log to a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Package   main

Import (
"Log"
"OS"
)

func Main () {
F, err: = OS. OpenFile ("Logfile.log", OS. O_rdwr|os. O_create|os. O_append, 0666)
if err! = Nil {
log. Fatalf ("File Open error:%v", err)
}
defer f.close ()
log. Setoutput (f)
log. Println ("This is a test log entry")
}

When we open logfile.log the file, we see the contents of the file as follows.

1
2016/03/11 17:54:10 This is a test log entry

Sometimes we don't need the previous date and time information, such as automated testing, than the log certainly do not want to have time information. What should we do then? It's time for SetFlags () to play.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Package   main

Import (
"Log"
"OS"
)

func Main () {
F, err: = OS. OpenFile ("Logfile.log", OS. O_rdwr|os. O_create|os. O_append, 0666)
if err! = Nil {
log. Fatalf ("File Open error:%v", err)
}
defer f.close ()
log. Setoutput (f)
log. SetFlags (0)
log. Println ("This is a test log entry")
}

The setflags (flag int) function can be used from the output format that defines the log, and flag can be selected as any of the following flags or combinations of operations.

1
2
3
4
5
6
7
8
Const (
Ldate = 1 << iota //The DATE:2009/01/23
Ltime //The TIME: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
Lstdflags = Ldate | Ltime//Initial values for the standard logger
)

Iota Grammar is not elaborate, here ldate,ltime,lmicroseconds, respectively, the 123th place.

Custom log format You can also use Setprefix (prefix string) to add a specific prefix to the front of our log. The rest of the API, for example, is Fatal Fatalf equivalent to Panic outputting log first and then calling exit () or the panic function.

Logger

Logger is a simple package for log, and using logger makes it easier to record logs. A simple example is shown below.

1
2
3
4
5
6
7
8
9
10
11
 Package Main

Import (
"Log"
"OS"
)

func Main () {
"DEBUG", log. Ldata | Log. Ltime)
Logger. Println ("This is a DEBUG LOG")
}

To run the above go program, you will get the following results at the terminal:

1
DEBUG:2016/03/11 22:21:19 This is a DEBUG LOG

Let's take a brief look at log. New () function prototype.

1
Func New (out IO. Writer, prefix string, flag int) *logger

Os. STDOUT represents the standard output, if you want to log into a file, the OS. Open () or OS. The return value of OpenFile () is passed in. "DEBUG" is the prefix for each log, and the last flag is the one above. The following code is more practical than the code snippet above.

1
2
3
4
5
6
7
8
9
Ten
One
A
-
-
the
-
-
-
+
-
+
A
at
-
-
-
-
-
in
-
to
+
-
the
*
$
Panax Notoginseng
-
the
+
Package   main

Import (
"IO"
"Io/ioutil"
"Log"
"OS"
)

var (
Info *log. Logger
Warning *log. Logger
Error *log. Logger
)

func Init (
infohandle io. Writer,
warninghandle io. Writer,
errorhandle io. Writer) {

Info = log. New (Infohandle,
"INFO:",
log. Ldate|log. Ltime|log. Lshortfile)

Warning = log. New (Warninghandle,
"WARNING:",
log. Ldate|log. Ltime|log. Lshortfile)

Error = log. New (Errorhandle,
"ERROR:",
log. Ldate|log. Ltime|log. Lshortfile)
}

func Main () {
Init (Ioutil. Discard, OS. Stdout, OS. Stdout, OS. Stderr)

info.println ("special Information")
warning.println ("There is something you need to Know")
error.println ("Something has failed")
}

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.