github.com/aldelo/common@v1.5.1/helper-error.go (about)

     1  package helper
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"runtime"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  func ErrAddLineTimeFileInfo(err error) error {
    12  
    13  	return errors.New(addLineTimeFileInfo(err.Error()))
    14  }
    15  
    16  func ErrNewAddLineTimeFileInfo(msg string) error {
    17  	return errors.New(addLineTimeFileInfo(msg))
    18  }
    19  
    20  func addLineTimeFileInfo(msg string) string {
    21  
    22  	if strings.Contains(msg[:4], "LogE") {
    23  		return msg
    24  	}
    25  
    26  	_, file, line, _ := runtime.Caller(1)
    27  	indexFunc := func(file string) string {
    28  		backup := "/" + file
    29  		lastSlashIndex := strings.LastIndex(backup, "/")
    30  		if lastSlashIndex < 0 {
    31  			return backup
    32  		}
    33  		secondLastSlashIndex := strings.LastIndex(backup[:lastSlashIndex], "/")
    34  		if secondLastSlashIndex < 0 {
    35  			return backup[lastSlashIndex+1:]
    36  		}
    37  		return backup[secondLastSlashIndex+1:]
    38  	}
    39  
    40  	logmessage := fmt.Sprintf("\nLogE: %v %v:%v:%v ", time.Now().UTC().Format("2006-01-02 15:04:05.000"), indexFunc(file), line, msg)
    41  
    42  	return logmessage
    43  }