3 Commits 698ec9cfd6 ... da1e139296

Author SHA1 Message Date
  Volodymyr Tkach da1e139296 Update Go to 1.23 10 months ago
  Volodymyr Tkach 72e25b22ae InArrayInt, InArrayInt64, InArrayStr 10 months ago
  Volodymyr Tkach e5f3c5b2e0 Improve logs 10 months ago
6 changed files with 74 additions and 6 deletions
  1. 1 1
      .github/workflows/go.yml
  2. 1 1
      go.mod
  3. 3 0
      go.sum
  4. 30 0
      utils/http/helpers/helpers.go
  5. 15 0
      utils/http/helpers/helpers_test.go
  6. 24 4
      utils/http/logger/logger.go

+ 1 - 1
.github/workflows/go.yml

@@ -16,7 +16,7 @@ jobs:
     - name: Set up Go
     - name: Set up Go
       uses: actions/setup-go@v3
       uses: actions/setup-go@v3
       with:
       with:
-        go-version: 1.18
+        go-version: 1.23
 
 
     - name: Test
     - name: Test
       run: go test -v ./...
       run: go test -v ./...

+ 1 - 1
go.mod

@@ -1,6 +1,6 @@
 module github.com/vladimirok5959/golang-utils
 module github.com/vladimirok5959/golang-utils
 
 
-go 1.19
+go 1.23
 
 
 require (
 require (
 	github.com/onsi/ginkgo v1.16.5
 	github.com/onsi/ginkgo v1.16.5

+ 3 - 0
go.sum

@@ -13,6 +13,7 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
 github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
 github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
 github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
 github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
 github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
 github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
+github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -25,6 +26,7 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
 github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
 github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
 github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
 github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
 github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
 github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
+github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
 github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
 github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
 github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
 github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
 github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
 github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
@@ -83,6 +85,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
 google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
 google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
 google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
 google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
 google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
 google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
+google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=

+ 30 - 0
utils/http/helpers/helpers.go

@@ -28,6 +28,9 @@ import (
 // func HandleTextJavaScript(data string) http.Handler
 // func HandleTextJavaScript(data string) http.Handler
 // func HandleTextPlain(data string) http.Handler
 // func HandleTextPlain(data string) http.Handler
 // func HandleTextXml(data string) http.Handler
 // func HandleTextXml(data string) http.Handler
+// func InArrayInt(num int, arr []int) bool
+// func InArrayInt64(num int64, arr []int64) bool
+// func InArrayStr(str string, arr []string) bool
 // func IntToStr(value int) string
 // func IntToStr(value int) string
 // func IntToStr64(value int64) string
 // func IntToStr64(value int64) string
 // func Md5Hash(str []byte) string
 // func Md5Hash(str []byte) string
@@ -161,6 +164,33 @@ func HandleTextPlain(data string) http.Handler {
 	return HandleFile(data, "text/plain")
 	return HandleFile(data, "text/plain")
 }
 }
 
 
+func InArrayInt(num int, arr []int) bool {
+	for _, v := range arr {
+		if num == v {
+			return true
+		}
+	}
+	return false
+}
+
+func InArrayInt64(num int64, arr []int64) bool {
+	for _, v := range arr {
+		if num == v {
+			return true
+		}
+	}
+	return false
+}
+
+func InArrayStr(str string, arr []string) bool {
+	for _, v := range arr {
+		if str == v {
+			return true
+		}
+	}
+	return false
+}
+
 func HandleTextXml(data string) http.Handler {
 func HandleTextXml(data string) http.Handler {
 	return HandleFile(data, "text/xml")
 	return HandleFile(data, "text/xml")
 }
 }

+ 15 - 0
utils/http/helpers/helpers_test.go

@@ -311,6 +311,21 @@ var _ = Describe("helpers", func() {
 			})
 			})
 		})
 		})
 
 
+		Context("InArrayInt", func() {
+			Expect(helpers.InArrayInt(3, []int{1, 2, 3, 4, 5})).To(BeTrue())
+			Expect(helpers.InArrayInt(9, []int{1, 2, 3, 4, 5})).To(BeFalse())
+		})
+
+		Context("InArrayInt64", func() {
+			Expect(helpers.InArrayInt64(3, []int64{1, 2, 3, 4, 5})).To(BeTrue())
+			Expect(helpers.InArrayInt64(9, []int64{1, 2, 3, 4, 5})).To(BeFalse())
+		})
+
+		Context("InArrayStr", func() {
+			Expect(helpers.InArrayStr("3", []string{"1", "2", "3"})).To(BeTrue())
+			Expect(helpers.InArrayStr("9", []string{"1", "2", "3"})).To(BeFalse())
+		})
+
 		Context("HandleTextXml", func() {
 		Context("HandleTextXml", func() {
 			BeforeEach(func() {
 			BeforeEach(func() {
 				srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))
 				srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))

+ 24 - 4
utils/http/logger/logger.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"log"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
+	"runtime"
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
@@ -29,7 +30,16 @@ func appendToLogFile(fileName, msg string) error {
 }
 }
 
 
 func LogError(format string, a ...any) {
 func LogError(format string, a ...any) {
-	msg := fmt.Sprintf(format, a...)
+	msg := fmt.Sprintf("[ERROR] %s\n", fmt.Sprintf(format, a...))
+	if pc, file, line, ok := runtime.Caller(1); ok {
+		msg = fmt.Sprintf(
+			"[ERROR] (%s, line #%d, func: %v) %s\n",
+			file,
+			line,
+			runtime.FuncForPC(pc).Name(),
+			fmt.Sprintf(format, a...),
+		)
+	}
 	log.Printf("%s", msg)
 	log.Printf("%s", msg)
 	if ErrorLogFile != "" {
 	if ErrorLogFile != "" {
 		if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 		if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
@@ -39,7 +49,7 @@ func LogError(format string, a ...any) {
 }
 }
 
 
 func LogInfo(format string, a ...any) {
 func LogInfo(format string, a ...any) {
-	msg := fmt.Sprintf(format, a...)
+	msg := fmt.Sprintf("[INFO] %s\n", fmt.Sprintf(format, a...))
 	log.Printf("%s", msg)
 	log.Printf("%s", msg)
 	if AccessLogFile != "" {
 	if AccessLogFile != "" {
 		if err := appendToLogFile(AccessLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 		if err := appendToLogFile(AccessLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
@@ -49,7 +59,16 @@ func LogInfo(format string, a ...any) {
 }
 }
 
 
 func LogInternalError(err error) {
 func LogInternalError(err error) {
-	msg := fmt.Sprintf("%s\n", err.Error())
+	msg := fmt.Sprintf("[ERROR] %s\n", err.Error())
+	if pc, file, line, ok := runtime.Caller(1); ok {
+		msg = fmt.Sprintf(
+			"[ERROR] (%s, line #%d, func: %v) %s\n",
+			file,
+			line,
+			runtime.FuncForPC(pc).Name(),
+			err.Error(),
+		)
+	}
 	log.Printf("%s", msg)
 	log.Printf("%s", msg)
 	if ErrorLogFile != "" {
 	if ErrorLogFile != "" {
 		if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 		if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
@@ -85,15 +104,16 @@ func LogRequests(handler http.Handler) http.Handler {
 			time.Since(start).Seconds(),
 			time.Since(start).Seconds(),
 			ua,
 			ua,
 		)
 		)
-		log.Printf("%s", msg)
 
 
 		if nw.Status < 400 {
 		if nw.Status < 400 {
+			log.Printf("[ACCESS] %s", msg)
 			if AccessLogFile != "" {
 			if AccessLogFile != "" {
 				if err := appendToLogFile(AccessLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 				if err := appendToLogFile(AccessLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 					log.Printf("%s\n", err.Error())
 					log.Printf("%s\n", err.Error())
 				}
 				}
 			}
 			}
 		} else {
 		} else {
+			log.Printf("[ERROR] %s", msg)
 			if ErrorLogFile != "" {
 			if ErrorLogFile != "" {
 				if err := appendToLogFile(ErrorLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 				if err := appendToLogFile(ErrorLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
 					log.Printf("%s\n", err.Error())
 					log.Printf("%s\n", err.Error())