Ejemplo n.º 1
0
Archivo: format.go Proyecto: gotips/log
func execFormatExamples() {
	// 默认简洁格式
	log.Infof("this is a test message, %d", 6)

	// 带标签的格式
	log.SetFormat(log.DefaultFormatTag)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 7)

	// 自定义其他格式的日志
	format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.000",
		log.LevelToken, log.PathToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 8)

	// 自定义 json 格式的日志
	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 9)

	// 自定义 xml 格式的日志
	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><tid>%s</tid><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.TagToken, log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 10)
}
Ejemplo n.º 2
0
Archivo: level.go Proyecto: gotips/log
func execLevelExamples() {
	// 默认日志级别 debug
	log.Printf("default log level: %s", log.GetLevel())
	log.Tracef("IsTraceEnabled? %t", log.IsTraceEnabled())
	log.Debugf("IsDebugEnabled? %t", log.IsDebugEnabled())
	log.Infof("IsInfoEnabled? %t", log.IsInfoEnabled())

	// trace 级别
	log.SetLevel(log.Ltrace)
	log.Tracef(msgFmt, 1)

	// info 级别
	log.SetLevel(log.Linfo)
	log.Debugf(msgFmt, 2)
	log.Infof(msgFmt, 2)

	// warn 级别
	log.SetLevel(log.Lwarn)
	log.Infof(msgFmt, 3)
	log.Warnf(msgFmt, 3)

	// error 级别
	log.SetLevel(log.Lerror)
	log.Warnf(msgFmt, 4)
	log.Errorf(msgFmt, 4)

	// 恢复默认级别,防止影响其他测试
	// debug 级别
	log.SetLevel(log.Ldebug)
	log.Tracef(msgFmt, 5)
	log.Debugf(msgFmt, 5)
}
Ejemplo n.º 3
0
Archivo: file.go Proyecto: gotips/log
func execSourceFileExamples() {
	// 全路经
	format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.000",
		log.LevelToken, log.PathToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 11)

	// 包
	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.000",
		log.LevelToken, log.PackageToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 12)

	// 项目
	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.000",
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 13)

	// 文件
	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.000",
		log.LevelToken, log.FileToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 14)
}
Ejemplo n.º 4
0
Archivo: custom.go Proyecto: gotips/log
func execCustomPrinterExample() {
	c := NewCustomPrinter()
	c.SetFormat(log.DefaultFormat)
	log.SetPrinter(c)

	log.Infof(msgFmt, 16)

	// 查看缓冲区
	// 每条日志结尾会加一个换行符
	line, err := c.ReadLog()
	if err != nil {
		fmt.Println(err) // 这里不能再调用 log.Error()
		os.Exit(1)
	}
	fmt.Print(line)
}
Ejemplo n.º 5
0
Archivo: writer.go Proyecto: gotips/log
func execChangeWriterExample() {
	// 改变 Writer,把日志打印到缓冲区,也可以打印到文件,定时切换文件,可以实现日志滚动
	buf := bytes.NewBuffer(make([]byte, 255))
	log.SetWriter(buf)

	log.Infof(msgFmt, 15)

	// 查看缓冲区
	// 每条日志结尾会加一个换行符
	line, err := buf.ReadString('\n')
	if err != nil {
		fmt.Println(err) // 这里不能再调用 log.Error()
		os.Exit(1)
	}

	fmt.Print(line)
}
Ejemplo n.º 6
0
func main() {
	log.Debugf("this is a test message, %d", 1111)

	format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.TagToken,
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

	log.Error("level = debug")
	log.Infof("this is a test message, %d", 1111)
	log.Errorf("this is another test message, %d", 22222)
	// Fatalf("%d %s", log.FatalLevel, log.FatalLevel)

	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.9",
		log.LevelToken, log.PathToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02", "15:04:05.999999",
		log.LevelToken, log.PackageToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf("%s %s %s:%d %s", "2006-01-02 15:04:05.000000",
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)
}