| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package logger
- import (
- "fmt"
- "runtime"
- "time"
- )
- //
- //******************monitor.log文件
- //日志格式举例:
- //[error][20180529 13:50:55] [122] [log/a.php:27][read_ihead:56 read_body:123 total:400][host(12.33.33.33:3600) word (aa cc)] Read mysql failed
- //整体日志列分割采用[]进行分割,如果字段为空在[]不输出内容
- //括号中内容为与falcon对应关系,在做指标监控时
- //1、日志级别:可以分debug info warn error fatal,如果作为指标监控时,请采用info字段,对应falcon的json字段在下方括号内对应关系标注
- //2、时间:必须按例子中规定格式输出内容 (timestamp)
- //3、指标或错误:主要用于运维根据错误号来配置报警等级、接收人等,必须按要求输出且需要在wiki记录,错误与指标只能用小写英语单词加下划线或者纯数字,该字段占位最长10个字符(metric)
- //4、程序文件名与行号:可为空
- //5、耗时:超时日志必须打印 总耗时:毫秒。其他两个字段用于自定义扩展,为各阶段毫秒,可为空(value)
- //6、参数:可为空 (tags)
- //7、描述:可为空
- //
- //New 实例化
- const (
- //日志级别
- MONITOR_FATAL = "fatal"
- MONITOR_ERROR = "error"
- MONITOR_WARN = "warn"
- MONITOR_INFO = "info"
- MONITOR_DEBUG = "debug"
- //错误code
- MONITOR_ERROR_INTERFACE_TIMEOUT = 101 //调用接口超时
- MONITOR_ERROR_INTERFACE_EXCEPTION = 102 //接口返回数据异常
- MONITOR_ERROR_INTERFACE_EXCEED_MAXTIME = 103 //接口超过最大时间
- )
- //监控日志通用结构
- func monitorLog(logLevel string, reasonCode uint32, consumingTime uint64, desc string) {
- //获取代码文件名称与代码行数
- _, file, line, ok := runtime.Caller(3)
- if !ok {
- file = "???"
- line = 0
- } else {
- short := file
- for i := len(file) - 1; i > 0; i-- {
- if file[i] == '/' {
- short = file[i+1:]
- break
- }
- }
- file = short
- }
- err := _fileLogWriter.Monitorlog().Output(0, fmt.Sprintf("[%s][%s] [%d] [%s/%d] [read_ihead:0 read_body:0 total:%d] [] %s",
- logLevel,
- time.Now().Format("20060102 15:04:05"),
- reasonCode,
- file,
- line,
- consumingTime,
- desc,
- ))
- if err != nil {
- consoleLogger.Errorf("write monitor log to file fail, err: %s", err.Error())
- panic("monitorLog")
- }
- }
- var (
- //MONITOR_ERROR_INTERFACE_TIMEOUT = 101 //调用接口超时
- MonitorErrorfInterTimeout = func(consumingTime uint64, format string, v ...interface{}) {
- MonitorErrorf(MONITOR_ERROR_INTERFACE_TIMEOUT, consumingTime, format, v...)
- }
- //MONITOR_ERROR_INTERFACE_EXCEPTION = 102 //接口返回数据异常
- MonitorErrorfInterException = func(consumingTime uint64, format string, v ...interface{}) {
- MonitorErrorf(MONITOR_ERROR_INTERFACE_EXCEPTION, consumingTime, format, v...)
- }
- //MONITOR_ERROR_INTERFACE_EXCEPTION = 102 //接口超过最大时间
- MonitorErrorfInterExceedMaxTime = func(consumingTime uint64, format string, v ...interface{}) {
- MonitorErrorf(MONITOR_ERROR_INTERFACE_EXCEED_MAXTIME, consumingTime, format, v...)
- }
- //[error][20180529 13:50:55] [122] [log/a.php:27][read_ihead:56 read_body:123 total:400][host(12.33.33.33:3600) word (aa cc)] Read mysql failed
- MonitorErrorf = func(reasonCode uint32, consumingTime uint64, format string, v ...interface{}) {
- monitorLog(MONITOR_ERROR, reasonCode, consumingTime, fmt.Sprintf(format, v...))
- }
- MonitorInfo = func(reasonCode uint32, consumingTime uint64, format string, v ...interface{}) {
- monitorLog(MONITOR_INFO, reasonCode, consumingTime, fmt.Sprintf(format, v...))
- }
- MonitorFatal = func(reasonCode uint32, consumingTime uint64, format string, v ...interface{}) {
- monitorLog(MONITOR_FATAL, reasonCode, consumingTime, fmt.Sprintf(format, v...))
- }
- MonitorWarn = func(reasonCode uint32, consumingTime uint64, format string, v ...interface{}) {
- monitorLog(MONITOR_WARN, reasonCode, consumingTime, fmt.Sprintf(format, v...))
- }
- MonitorDebug = func(reasonCode uint32, consumingTime uint64, format string, v ...interface{}) {
- monitorLog(MONITOR_DEBUG, reasonCode, consumingTime, fmt.Sprintf(format, v...))
- }
- )
|