Go語言代碼自動更新伺服器代碼分享

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package logimport ("io""log""os""runtime""time")type l struct {logs  *log.Loggerlevel intio.Closer}func NewLog(HttpLogPath string, level int) *l {file, err := os.OpenFile(HttpLogPath, os.O_CREATE|os.O_RDWR, 0644)if err != nil {log.Println("Error", err)log.Println("Error", "日誌輸出到標準輸出.")return nil}var Log *log.Logger = log.New(os.Stdout, now(), 0)if file != nil {Log = log.New(file, now(), 0)go flushLogFile(file)}file.Seek(0, 2)return &l{Log, level, file}}func now() string {return time.Now().Format("2006-01-02 15:04:05 ")}func flushLogFile(File *os.File) {for _ = range time.NewTicker(50 * time.Second).C {if File == nil {return}File.Sync()}}func (self *l) SetLogLevel(level int) {if level > 4 {return}self.level = level}func (self *l) Print(v ...interface{}) {self.logs.Print(v)}func (self *l) Printf(formate string, v ...interface{}) {self.logs.Printf(formate, v)}func (self *l) Println(v ...interface{}) {self.logs.Println(v)}func (self *l) PrintfI(formate string, v ...interface{}) {if self.level > 1 {return}self.logs.Printf("Info->"+formate, v...)}func (self *l) PrintfW(formate string, v ...interface{}) {if self.level > 2 {return}self.logs.Printf("Warn->"+formate, v...)}func (self *l) PrintfE(formate string, v ...interface{}) {if self.level > 3 {return}self.logs.Printf("Error->"+formate, v...)}func (self *l) PrintfF(formate string, v ...interface{}) {if self.level > 4 {return}self.logs.Fatalf("Fatal->"+formate, v...)}func (self *l) InfoPrintf(callers int, formate string, v ...interface{}) {_, file, line, ok := runtime.Caller(callers + 1)if !ok {return}self.logs.Printf("File->%s Line->%d\n", file, line)self.logs.Printf(formate, v...)}<pre name="code" class="html">
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#0000ff;">package</span><span style=" color:#f57900;"> </span>main

import "centerserver/log"

func main() {
    l := log.NewLog("http.log", 0)
    l.InfoPrintf(0, "%s\n", "錯誤在這裡↑")
    l.PrintfI("%s\n", "Info")
    l.PrintfW("%s\n", "Warn")
    l.PrintfE("%s\n", "Error")
    l.PrintfF("%s\n", "Fatal")
}




</pre><pre name="code" class="html">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.