Пример #1
0
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)
}
Пример #2
0
// Tprintf 简单实现打印方法
func (p *CustomPrinter) Tprintf(l log.Level, tag string, format string, m ...interface{}) {
	if log.GetLevel() > l {
		return
	}

	p.mu.Lock()
	defer p.mu.Unlock()

	if p.dateFmt != "" {
		now := time.Now()
		p.buf.WriteString(now.Format(p.dateFmt))
		p.buf.WriteByte(' ')
		if p.timeFmt != "" {
			p.buf.WriteString(now.Format(p.timeFmt))
			p.buf.WriteByte(' ')
		}
	}

	if p.tag {
		if tag == "" {
			tag = "-"
		}
		p.buf.WriteString(tag)
		p.buf.WriteByte(' ')
	}

	if p.prefixLen > -1 {
		_, file, line, ok := runtime.Caller(2) // expensive
		if ok && p.prefixLen < len(file) {
			p.buf.WriteString(file[p.prefixLen:])
			p.buf.WriteByte(':')
			p.buf.WriteString(strconv.Itoa(line))
			p.buf.WriteByte(' ')
		} else {
			p.buf.WriteString("???:0 ")
		}
	}

	if format == "" {
		p.buf.WriteString(fmt.Sprint(m...))
	} else {
		p.buf.WriteString(fmt.Sprintf(format, m...))
	}

	p.buf.WriteByte('\n')
}