[Golang] Starting from scratch socket Server (6) "End": the design of the log module and the timing task module

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

I haven't written an article in a long time. Today, Golang dug this hole to finish it ~

As a server, log function is essential, a well-designed log module, whether it is the development of server debugging, or run time maintenance, are very helpful.

Because this is a simplified server framework, I chose to extend the log library of the Golang itself to implement a simple log module.

Here, I will be the level of the log is roughly divided into Debug,operating,error 3 levels, debug is mainly used to store the debug phase of the log information, operateing used to save the server daily operation of the information generated, error is to save the error message.

The module code is as follows:

Func LogErr (v ... interface{}) {logfile: = os. Stdoutlog.println (v ...) Logger: = log. New (logfile, "\ r \ n", log.) Llongfile|log. Ldate|log. Ltime); logger. Setprefix ("[Error]") logger. Println (v ...) Defer logfile. Close ();} Func Log (v ... interface{}) {logfile: = os. Stdoutlog.println (v ...) Logger: = log. New (logfile, "\ r \ n", log.) Ldate|log. Ltime); logger. Setprefix ("[Info]") logger. Println (v ...) Defer logfile. Close ();} Func logdebug (v ... interface{}) {logfile: = os. Stdoutlog.println (v ...) Logger: = log. New (logfile, "\ r \ n", log.) Ldate|log. Ltime); logger. Setprefix ("[Debug]") logger. Println (v ...) Defer logfile. Close ();} Func checkerror (err error) {if err! = Nil {logerr (OS). Stderr, "Fatal Error:%s", err. Error ())}}

Note that the output of the log here I use is stdout, because this allows the server to directly redirect the log to the specified location to facilitate the deployment of the entire server. However, in the daily development, in order to facilitate debugging code, I recommend the log output to the specified file location, so in the debugging time will be much more convenient (mainly because the Golang debugging is too cumbersome, many times to rely on the log when stepping. Easy to debug Log module code:

Func Log (v ... interface{}) {logfile: = os. OpenFile ("Server.log", OS. O_rdwr|os. O_append|os. o_create,0); if err! = Nil {fmt. fprintf (OS. Stderr, "Fatal Error:%s", err. Error ()) return    }log. Println (v ...) Logger: = log. New (logfile, "\ r \ n", log.) Ldate|log. Ltime); logger. Setprefix ("[Info]") logger. Println (v ...) Defer logfile. Close ();}


Then is the timing cycle module, daily operation, the server often to perform a number of scheduled tasks, such as a certain time to refresh the background, interval time automatically refresh the crawler and so on, too lazy to design an interface, Here I am more rough directly using the time.sleep to sleep over time, in fact, the better way is to use a channel for time control and prevention has been blocked, but do not bother to change (o w o)

Here is an example of a timed crawler:

Func getsomething () error{resp, err: = http. Get ("http://www.myurl.com") if err! = Nil {Log (err)}fetch, err: = Ioutil. ReadAll (resp. Body) Defer resp. Body.close () if err! = Nil {Log (err)}if string (fetch) = = "" {return errors. New ("Get a blank URL")}  return Nil}func MyTask (GAP) {if  getsomething ()!=nil {  getsomething ()}time. Sleep (time. Duration (GAP) * time. Second) Log ("awake! Here we go the next place ") MyTask (GAP)}

It is important to note that because Time.sleep is used here to block threads, it is necessary to use go for concurrent operations when calling MyTask, otherwise the entire server will not be able to do anything during blocking.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.