示例#1
0
func (hook *SyslogHook) Fire(entry *logrus.Entry) error {
	file, lineNumber := caller.GetCallerIgnoringLogMulti(1)
	if file != "" {
		sep := fmt.Sprintf("%s/src/", os.Getenv("GOPATH"))
		fileName := strings.Split(file, sep)
		if len(fileName) >= 2 {
			file = fileName[1]
		}
	}
	entry.Data["file"] = file
	entry.Data["line"] = lineNumber
	line, err := entry.String()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
		return err
	}

	switch entry.Level {
	case logrus.PanicLevel:
		return hook.Writer.Crit(line)
	case logrus.FatalLevel:
		return hook.Writer.Crit(line)
	case logrus.ErrorLevel:
		return hook.Writer.Err(line)
	case logrus.WarnLevel:
		return hook.Writer.Warning(line)
	case logrus.InfoLevel:
		return hook.Writer.Info(line)
	case logrus.DebugLevel:
		return hook.Writer.Debug(line)
	default:
		return nil
	}
}
示例#2
0
func getMessage(entry *logrus.Entry) (message string, err error) {
	file, lineNumber := caller.GetCallerIgnoringLogMulti(2)
	if file != "" {
		sep := fmt.Sprintf("%s/src/", os.Getenv("GOPATH"))
		fileName := strings.Split(file, sep)
		if len(fileName) >= 2 {
			file = fileName[1]
		}
	}
	var fields string
	for k, v := range entry.Data {
		fields = fields + fmt.Sprintf("%s:%v;", k, v)
	}
	if len(fields) > 0 {
		fields = fields[:len(fields)-1]
	}
	call := fmt.Sprintf("%s:%d[%s]", file, lineNumber, fields)
	message = fmt.Sprintf("%s\n%s", entry.Message, call)
	return
}
示例#3
0
文件: hook.go 项目: trigrass2/logrus
func getMessage(entry *logrus.Entry) (message string, err error) {
	message = message + fmt.Sprintf("%s\n", entry.Message)
	for k, v := range entry.Data {
		if !strings.HasPrefix(k, "err_") {
			message = message + fmt.Sprintf("%v:%v\n", k, v)
		}
	}
	if full, ok := entry.Data["err_full"]; ok {
		message = message + fmt.Sprintf("%v", full)
	} else {
		file, lineNumber := caller.GetCallerIgnoringLogMulti(2)
		if file != "" {
			sep := fmt.Sprintf("%s/src/", os.Getenv("GOPATH"))
			fileName := strings.Split(file, sep)
			if len(fileName) >= 2 {
				file = fileName[1]
			}
		}
		message = message + fmt.Sprintf("%s:%d", file, lineNumber)
	}
	return
}