Recording Log:seelog packages in the Go language

Source: Internet
Author: User

The first two weeks to adjust the bug of blood, although solved but still quite a waste of time. Chat with colleagues, feel that we are now in the project is too few log records, resulting in a problem do not know how to do, but also have to change the code to log, and then troubleshoot problems. This way, if there are bugs in the future, it's a lot of trouble, so let me take a closer look at how to log the logs in the Go language (well, I've been on the log lately).

Based on Python's experience and current project requirements, I have two requirements for logging:

One is to define the log level , you can record different levels of debug/warning/error, such as the log, so that in normal operation, it is only necessary to record the state of operation, and error needs to debug, you can display more logs;

The second is to define the log format , want to record what content, such as time, level, location, log information and so on. So, simply look at the log module that go comes with, you know that I can not reach the requirements.

Check on the Internet now more popular logging methods, see someone recommended Seelog, so try. In a few small pits, but still smooth, writing is also relatively smooth. Record it.

Seelog Official website: https://github.com/cihub/seelog

Installing Seelog

Under the command line

Go get-u github.com/cihub/seelog

I've stepped on the first pit here--because I'm not familiar with the go language, and I don't have git installed. Go to Baidu, install a git, come back to find or not, because the Windows Git does not automatically configure environment variables. You need to add the bin path and the Git-core path in path. If it is installed by default, it is usually added

; C:\Program Files\git\bin; C:\Program Files\git\mingw64\libexec\git-core

Pay attention to whether Windows is 32-bit or 64-bit (Linux is not so much, just apt-get install git done)

After you have a command git to try, this command is the environment variable configuration. Again go get install seelog very smoothly installed.

Using Seelog

After the installation, you can try it. Unlike Python and Java logging, Seelog requires an XML configuration file that sets the path, rank, format, etc. of the log output. A typical configuration file is the one that grew into the sauce:

<Seelogtype= "Asynctimer"Asyncinterval= "5000000"MinLevel= "Debug"Maxlevel= "Error">    <Exceptions>        <ExceptionFuncpattern= "*main.test*something*"MinLevel= "Info"/>        <ExceptionFilepattern= "*main.go"MinLevel= "Error"/>    </Exceptions>    <outputsFormatID= "Main">        <Console/>         <SplitterFormatID= "FORMAT1">            <filePath= "Log.log"/>            <filePath= "Log2.log"/>        </Splitter>        <SplitterFormatID= "Format2">            <filePath= "Log3.log"/>            <filePath= "Log4.log"/>        </Splitter>         <RollingfileFormatID= "Someformat"type= "Size"filename= "./log/roll.log"maxsize= "+"Maxrolls= "5" />         <BufferedFormatID= "Testlevels"size= "10000"Flushperiod= "+">            <filePath= "./log/buffileflush.log"/>        </Buffered>         <Filterlevels= "Error">            <filePath= "./log/error.log"/>            <SMTPsenderaddress= "[email protected]"SenderName= "Automatic Notification Service"hostname= "mail.none.org"Hostport= "587"username= "Nns"Password= "123">                <RecipientAddress= "[email protected]"/>                <RecipientAddress= "[email protected]"/>            </SMTP>            <ConnNet= "TCP4"Addr= "server.address:5514"TLS= "true"insecureskipverify= "true" />        </Filter>     </outputs>    <formats>        <formatID= "Main"format= "%date (2006 Jan 02/3:04:05.000000000 PM MST) [%level]%msg%n"/>        <formatID= "Someformat"format= "%ns [%level]%msg%n"/>        <formatID= "Testlevels"format= "%level%lev%level%lev%l%msg%n"/>        <formatID= "Usetags"format= "&lt;msg&gt;%Msg&lt;/time&gt;"/>        <formatID= "FORMAT1"format= "%date/%time [%lev]%msg%n"/>        <formatID= "Format2"format= "%file%fullpath%relfile%msg%n"/>    </formats></Seelog>

See this XML: Https://github.com/cihub/seelog/wiki/Example-config

Don't ask me this shrimp, I can only read part of it. At the moment I only know the simplest few nodes to use to meet my needs. For me, the following XML is basically enough:

(Here I use this symbol to pretend to comment, XML itself without comments)

<Seelog>    <outputsFormatID= "Main">-- to find the format ID main<Filterlevels= "Info,debug,critical,error">-- define record formats<Console/>-- output to the screen</Filter>        <Filterlevels= "Debug">            <filePath= "Debug.txt" />--to the file output.        Can coexist more than one. </Filter>    </outputs>    <formats>        <formatID= "Main"format= "%date/%time [%lev]%msg%n"/>-->format content can coexist as long as the IDs are not the same.    You can then use a different ID to output a different format of the log. </formats></Seelog>

Ok. After you write the XML, you can code it. Write a simple example:

Testseelog.gopackage main import (    seelog "Github.com/cihub/seelog") func main () {    logger, err: = Seelog. Loggerfromconfigasfile ("Seelog.xml")        if err! = Nil {        Seelog. Critical ("Err parsing config log file", err)        return    }    seelog. Replacelogger (Logger)        Seelog. Error ("Seelog error")    Seelog. Info ("Seelog info")    Seelog. Debug ("Seelog Debug")}

(uh.. Blog Park There is no Golang code color, so it, anyway not delay to see)

Finish writing the run. A good news and a bad news. The good news is that there is no problem with the end of normal operation. The bad news is that the special is not out of the log. Neither the screen nor the log file. What about my diary? Where have you been hiding me?

To go back and forth to check for ten times did not find the reason, so to QQ Group asked, thank Wu Yussong and drowning fish two students answer: no time to run the end, and no flush. So I added a sentence before Seelog recorded the contents of the log.

Defer Seelog. Flush ()

After that, we can make the results.

2015-12-17/16:28:23 [ERR] Seelog error2015-12-17/16:28:23 [INF] Seelog info2015-12-17/16:28:23 [DBG] Seelog Debug

So far. Seelog The first step is stepping out. There's a lot of functions in the Seelog, and there's a bunch of things in the config file that I don't understand, as well as the documentation on GitHub, and I think there's more to this library than it's worth digging into. I'll see you later.

Recording Log:seelog packages in the Go language

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.