Golang日誌--Seelog的簡單用法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Beego內建的log包雖然功能比較完善,但是在日誌中會帶上記錄層級的時間戳記,而且這個時間戳記還刪除不了。

因為項目需要,所以研究了下Golang中其他的日誌包,發現seelog正好能滿足俺們項目的需要,因此在這做簡單介紹。

seelog的地址:"github.com/cihub/seelog"

seelog功能強大,在此簡單介紹將日誌配置儲存在xml檔案中,並從檔案中擷取配置,然後使用的流程。

在controllers包的某個檔案中:

mainlog, _ = seelog.LoggerFromConfigAsFile("conf/seelog-main.xml")
LoggerFromConfigAsFile會從後面的檔案中擷取日誌配置,如上則是從conf包的seelog-main.xml檔案中擷取配置;

seelog-main.xml檔案內容如下:

<seelog><outputs formatid="main"><buffered size="10000" flushperiod="1000"><rollingfile type="date" filename="/var/log/main.log" datepattern="2006.01.02" maxrolls="30"/></buffered></outputs><formats>        <format id="main" format="%Msg%n"/>    </formats></seelog>

其中:

outputs裡面是日誌的配置,formats裡面是日誌輸出格式配置。

formatid對應下面的formats裡對應id的輸出格式。

buffered指的是緩衝設定:

size--緩衝區大小,單位byte;flushperiod--緩衝間隔,單位ms

rollingfile裡面是日誌的設定:

type--復原的方式,date指按日期復原,size指按大小復原

filename--檔案路徑

datepattern--日期的格式,按日期復原時才會有此選項

maxrolls--日誌儲存的最大期限,此處會儲存30天以內的日誌

format裡面:

id--對應的outputs的formatid

format--日誌輸出的格式,%Msg%n表示只會輸出日誌並換行

使用的時候,如下:

defer mainlog.Flush()mainlog.Info("需要輸入的日誌")
即可。以後會完善seelog的用法


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.