Golang log Processing

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

Golang Program Log records

Package Mainimport ("Flag" "FMT" "Log" "OS" "Runtime") var (logfilename = flag.) String ("Log", "CServer.log", "Log file name")) Func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) flag. Parse ()//set logfile stdoutlogfile, LogErr: = OS. OpenFile (*logfilename, OS. O_create|os. O_rdwr|os. O_append, 0666) if logerr! = nil {fmt. Println ("Fail to find", *logfile, "cserver start Failed") os. Exit (1)}log. Setoutput (logFile) log. SetFlags (log. Ldate | Log. Ltime | Log. Lshortfile)//write Loglog. Printf ("Server abort! CAUSE:%V \ n "," Test log File ")}

Description

First, after the main program starts to initialize the log format, later with LOG.XXX will be recorded in the original CServer.log file

Second, the above initialization format is shown in the log file as:

2013/03/19 10:44:26 Main.go:29:server abort! Cause:test log file 2013/03/19 10:44:27 main.go:29:server abort! Cause:test log File

Third, you can pass the log. SetFlags () Customize the format you want to express

Four, set the output destination log. Setoutput ()

Five, OS. OpenFile (*logfilename, OS. O_create|os. O_rdwr|os. O_append, 0666) This is the creation of the log file.

5.1 If the log file does not exist, create a new file os. O_create

5.2 Open the read-write OS for the file . O_rdwr

5.3 Writes log information to the log file, inheriting to the current log file, not overwriting the OS. O_append

5.3log file Permissions bit 0666(that is, all users can read and write)

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.