Using the Logger middleware with a database



I’m looking to store the results from the Logger middleware into a database (SQLServer for now). I’m wondering how to do this with the middleware pattern being used with echo. Right now I’m logging it to a file using Lumberjack (, setting the output of the Logger middleware to Lumberjack.

Has anyone done this before and have a patter that works with Echo? I’m familiar with setting up a data access layer, but not so sure how to go about using the data from the Logger in a custom way that lets me populate SQL queries with the information from the logger.

Any ideas or hints are appreciated!


Code that I’m using right now:


func main() {

func runWebServer() {
	// initialize new echo web server
	e := echo.New()

	// initialize logging subsystem
	logLevel := os.Getenv("log_level")
	if logLevel != "DEV" {
			Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}",` +
				`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
				`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
				`"bytes_out":${bytes_out}}` + "\r\n",
			Output: &lumberjack.Logger{
				Filename:   filepath.Join("logs", "logfile.log"),
				MaxSize:    500,
				MaxBackups: 3,
				MaxAge:     30,
	} else {
	// initialize data access layer to database
	dal, err := dal.NewMSSQL(os.Getenv("database_conn"))
	if err != nil {

	// configure routing tables
	api.Route(e, dal)

	// start echo web server
	e.Logger.Fatal(e.Start(":" + os.Getenv("port")))


Hi there,

If you take a look at you can see that it implements io.Writer, which is not surprising considering that it is required for the Output field of LoggerConfig.

You can use a similar pattern: pass in a type (that can set the connection details) which implements io.Writer. The Write() method would then get what the logger gets, but instead of writing it to a file it sends it to a database. However if you are writing this for a production environment, adding time consuming and/or prone to error things to the request/response cycle is not a good practice (for example, what if you cannot temporarily send data to your database? should the request really error out, or should you rather enqueue and retry later; what if it takes too long, etc.).

From that perspective, your logger could be even simpler as it should be decoupled from the DB anyway, and you should instead simply use it to enqueue log entries (and have a separate goroutine read them and send them to the db, possibly batch them, possibly handle retries, etc.).

It could look something like:

type logger struct {
    ch chan []byte

func (l logger) Write(p []byte) (n int, err error) { <- p
    return len(p), nil

func main() {
    ch, wg := make(chan []byte), new(sync.WaitGroup)

    go runLogger(ch, wg)


func runLogger(ch chan []byte) {
    defer wg.Done()

    for entry := range ch {
        // handle entry - send to SQL, or group them and send them in batches,
        // handle errors, perform retries, etc.

    // handle any cleanup, flush/close db connection, etc.

// Note you will need to implement graceful shutdown:
// so that runLogger() can also complete gracefully.
func runWebServer(ch chan []byte) {
    // ...
			Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}",` +
				`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
				`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
				`"bytes_out":${bytes_out}}` + "\r\n",
			Output: logger{ch},
    // ...

Hope it helps!



Arrange the database from the guidelines of where you will be able to use the structured query language.