Logging request and response to mongodb


#1

Hi all,

Some time you need to log all request and response HTTP, but you do not want to write into file, you want to store into database for easy search,analytic or somethings else. I will share you some code to do that.

I create package to logger for database.

package logger2MongoDB

var (
	collection string
	err        error
	fLogger    *lumberjack.Logger
)

type (
	//Logger struct logger for echo
	Logger struct {
		Time       time.Time `bson:"time" json:"time"`
		Lv         string    `bson:"level" json:"level"`
		Prefix     string    `bson:"prefix" json:"prefix"`
		Message    string    `bson:"-" json:"message"`
		Data       ctxLogger `bson:"data" json:"data"`
		Collection string    `bson:"-"`
	}

	ctxLogger struct {
		ID  string      `json:"id" bson:"id"`
		Req interface{} `json:"req" bson:"request"`
		Res interface{} `json:"res" bson:"response"`
	}
)

func init() {

//some time shutdown database, you will need this.
	fLogger = &lumberjack.Logger{
		Filename: config.DataConfig.Logger,
		MaxSize:  650,  // megabytes
		MaxAge:   15,   //days
		Compress: true, // disabled by default
	}
}

func (lg *Logger) Write(logByte []byte) (n int, err error) {
	err = json.Unmarshal(logByte, &lg)
	if err != nil {
		return
	}
	go func() {
		fLogger.Write(logByte)
	}()
	err = json.NewDecoder(strings.NewReader(lg.Message)).Decode(&lg.Data)
	go func() {
//insert to database, you can change this.
		if err := dbconnect.InserToCollection(lg.Collection, lg); err != nil {
			fmt.Printf("\ntime:%s,%s\n", time.Now(), lg.Message)
		}
	}()
	return len(logByte), nil
}

Set output to use your custom package logger and using middleware

func init() {
	log.SetPrefix("prefix")
	log.SetOutput(&logger2MongoDB.Logger{Collection: "logger"})
}

// Logger request, response
e.Use(middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{
	Handler: func(c echo.Context, reqBody, resBody []byte) {
		reqB := ""
		if len(reqBody) > 0 {
			reqB = string(reqBody)
		}
		log.Infof(`{"id":"%s","req":%s,"res":%s}`, c.Response().Header().Get(echo.HeaderXRequestID), reqB, resBody)
	},
}))

How can I log POST request data in JSON