go log 学习笔记

刚刚学习了golang 中 使用log package 来记录日志的方法。非常简单,但是应付一般的progress 已经完全足够了。
一下是blackcardriver的代码:

package main

import (
	"log"
	"os"
	"time"
)

func main() {
	test_log2()
}

//ouput the log in different flag format,(totally 32 style)
func test_log1() {
	//onte that the original content will be covered!
	fp, err := os.Create("test.log")
	if err != nil {
		log.Fatal("create file faild")
	}
	defer fp.Close()
	//recommand to use flag 18
	aLoger := log.New(fp, "PreFix :", 0)
	for i := 0; i < 33; i++ {
		aLoger.SetFlags(i)
		aLoger.Println("It is style ", i)
	}
}

//create a new file name by time
func create_file_bytime() (*os.File, error) {
	ntime := time.Now()
	filename := ntime.Format("logs_demo 0102#150405.log")
	file, err := os.Create(filename)
	return file, err
}

//record log in different file to avoid cover the old logs file
func test_log2() {
	osfile, err := create_file_bytime()
	if err != nil {
		log.Fatal(err)
	}
	loger := log.New(osfile, "", 18)
	loger.Println("Create log Scuess !!!....")
}

附上样式参考案例

PreFix :It is style  0
PreFix :2019/03/30 It is style  1
PreFix :16:45:05 It is style  2
PreFix :2019/03/30 16:45:05 It is style  3
PreFix :16:45:05.963607 It is style  4
PreFix :2019/03/30 16:45:05.963607 It is style  5
PreFix :16:45:05.963607 It is style  6
PreFix :2019/03/30 16:45:05.963607 It is style  7
PreFix :D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  8
PreFix :2019/03/30 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  9
PreFix :16:45:05 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  10
PreFix :2019/03/30 16:45:05 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  11
PreFix :16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  12
PreFix :2019/03/30 16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  13
PreFix :16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  14
PreFix :2019/03/30 16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style  15
PreFix :main.go:25: It is style  16
PreFix :2019/03/30 main.go:25: It is style  17
PreFix :16:45:05 main.go:25: It is style  18
PreFix :2019/03/30 16:45:05 main.go:25: It is style  19
PreFix :16:45:05.963607 main.go:25: It is style  20
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style  21
PreFix :16:45:05.963607 main.go:25: It is style  22
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style  23
PreFix :main.go:25: It is style  24
PreFix :2019/03/30 main.go:25: It is style  25
PreFix :16:45:05 main.go:25: It is style  26
PreFix :2019/03/30 16:45:05 main.go:25: It is style  27
PreFix :16:45:05.963607 main.go:25: It is style  28
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style  29
PreFix :16:45:05.963607 main.go:25: It is style  30
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style  31
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值