123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package logger
- import (
- "fmt"
- "log"
- "net/http"
- "os"
- "strings"
- "time"
- "github.com/rollbar/rollbar-go"
- "github.com/vladimirok5959/golang-utils/utils/http/helpers"
- )
- var AccessLogFile = ""
- var ErrorLogFile = ""
- func appendToLogFile(fileName, msg string) error {
- flags := os.O_RDWR | os.O_CREATE | os.O_APPEND
- f, err := os.OpenFile(fileName, flags, 0666)
- if err != nil {
- return err
- }
- defer f.Close()
- if _, err := fmt.Fprint(f, msg); err != nil {
- return err
- }
- return nil
- }
- func LogError(format string, a ...any) {
- msg := fmt.Sprintf(format, a...)
- log.Printf("%s", msg)
- if ErrorLogFile != "" {
- if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
- log.Printf("%s\n", err.Error())
- }
- }
- }
- func LogInfo(format string, a ...any) {
- msg := fmt.Sprintf(format, a...)
- log.Printf("%s", msg)
- if AccessLogFile != "" {
- if err := appendToLogFile(AccessLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
- log.Printf("%s\n", err.Error())
- }
- }
- }
- func LogInternalError(err error) {
- msg := fmt.Sprintf("%s\n", err.Error())
- log.Printf("%s", msg)
- if ErrorLogFile != "" {
- if err := appendToLogFile(ErrorLogFile, time.Now().Format("2006/01/02 15:04:05")+" "+msg); err != nil {
- log.Printf("%s\n", err.Error())
- }
- }
- if RollBarEnabled && !RollBarSkipErrors.contain(err.Error()) {
- rollbar.Error(err)
- }
- }
- func LogRequests(handler http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- start := time.Now()
- nw := &ResponseWriter{
- ResponseWriter: w,
- Content: []byte{},
- Size: 0,
- Status: http.StatusOK,
- }
- handler.ServeHTTP(nw, r)
- ua := strings.TrimSpace(r.Header.Get("User-Agent"))
- if ua == "" || len(ua) > 256 {
- ua = "-"
- }
- msg := fmt.Sprintf(
- "\"%s\" \"%s %s\" %d %d \"%.3f ms\" \"%s\"\n",
- strings.Join(helpers.ClientIPs(r), ", "),
- r.Method,
- r.URL,
- nw.Status,
- nw.Size,
- time.Since(start).Seconds(),
- ua,
- )
- log.Printf("%s", msg)
- if nw.Status < 400 {
- if AccessLogFile != "" {
- if err := appendToLogFile(AccessLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
- log.Printf("%s\n", err.Error())
- }
- }
- } else {
- if ErrorLogFile != "" {
- if err := appendToLogFile(ErrorLogFile, start.Format("2006/01/02 15:04:05")+" "+msg); err != nil {
- log.Printf("%s\n", err.Error())
- }
- }
- }
- if RollBarEnabled && !RollBarSkipStatusCodes.contain(nw.Status) {
- if !RollBarSkipErrors.contain(string(nw.Content)) {
- rollbar.Error(r, nw.Status, nw.Size, string(nw.Content))
- }
- }
- })
- }
|