Golang Log 学习笔记
2015-11-13 18:18
661 查看
官方Doc链接
http://godoc.golangtc.com/pkg/log/简介
Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined ‘standard’ Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.译 :
log 包实现了一个简单的日志库。 它定义了提供格式化输出接口的类型 : Logger。它还提供了一个预先定义好的“标准”Logger,这个Logger往标准出错打印每一条日志以及他的时间。 可以使用接口 :
Print[f|ln], Fatal[f|ln], 和 Panic[f|ln]来直接使用”标准”Logger, 比起自己手动创建Logger, 他们要方便的多。
Fatal接口在写完日志后会调用
os.Exit(1)。
Panic接口在写完日志后会调用
panic。( 如果没有配置panic, 也会导致程序退出)
[f|ln]
对于本包的接口,往往会有3个版本 : XXX , XXXf 和 XXXln , 含义分别是 :打印 , 格式化打印, 打印并换行 。接口
打印不同等级的log
Print[f|ln] 打印Panic[f|ln] 恐慌
Fatal[f|ln] 致命
Output 类似Println , 可以指定打印调用栈的特定深度的信息
设置logger熟悉
SetFlags 陪自己你需要的打印熟悉Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Ltime // the time in the local time zone: 01:23:23
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
Llongfile // full file name and line number: /a/b/c/d.go:23
*Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
LstdFlags = Ldate | Ltime // initial values for the standard logger
SetOutput 日志输出到何方, 默认stderr, 任何实现 io.Writer 接口的对象
SetPrefix 配置你的专属前缀
定制自己的Logger
package log_test import "log" import "os" import "runtime" import "fmt" type LogServer struct { logger *log.Logger file *os.File thread_num uint32 file_name chan string log_buffer chan string End chan bool } func NewLogger() (*log.Logger, *LogServer) { new_log := new(LogServer) new_log.logger = log.New(new_log, "", log.Ldate|log.Ltime|log.Lshortfile) new_log.file_name = make(chan string) new_log.log_buffer = make(chan string ) new_log.End = make(chan bool) go new_log.InitLogThread() return new_log.logger, new_log } func (this *LogServer) InitLogThread() { for { select { case file_name , ok := <-this.file_name: if ok { this.UseFile(file_name) }else { fmt.Println("NO") } case a_log , ok := <-this.log_buffer: if ok { this.RealLog(a_log) }else { fmt.Println("NO") } case end , ok:= <-this.End: if end && ok { break }else { fmt.Println("NO") } } } } func (this *LogServer) UseFile(file_name string) { t_file , err := os.Create(file_name) if err != nil || t_file == nil { return }else { if this.file != nil { this.file.Close() } this.file = t_file } } func (this *LogServer) RealLog(p string) { if this.file != nil { this.file.Write([]byte(p)) }else { fmt.Println(string(p)) } } func (this *LogServer) Write(p []byte) (n int, err error) { this.log_buffer <- string(p) return len(p), nil }
相关文章推荐
- goahead移植
- go语言字符串处理
- 别致的语言GO(GO语言初涉)
- go语言基础
- google搜索
- django migrations
- 使用cargo自动化部署maven项目
- goahead Web Server 环境搭建
- LeetCode Longest Palindromic Substring Part Manacher ALGORITHM
- Go 语言项目管理
- HDOJ 3635 Dragon Balls(并查集)
- 【POJ 2195】 Going Home(最小费)
- 浅谈DragonBoard 410c的Miracast无线同屏技术
- Mac系统Google登陆问题解决
- Go中nil
- django安全
- mongo的复制集
- Go语言反射规则 - The Laws of Reflection
- HOW TO BE A GOOD LEADER
- google perftools分析程序性能