Go Hood 保存到 Postgres,但不确定在哪里。

huangapple go评论93阅读模式
英文:

Go Hood saving to Postgres, but not sure where

问题

我有一个SaveRequest方法,它接受一个http.Request,然后将其保存到一个postgres数据库中(目前只保存路径):

func (logger *PostgresLogger) SaveRequest(req *http.Request) {
  os.Stdout.Write([]byte("Saving to PGDB\n"))
  request := db.Requests { Path: req.URL.Path }
  transaction := logger.dbConnection.Begin()
  Id, saveError := transaction.Save(&request)
  if saveError != nil {
    panic(saveError)
  }

  os.Stdout.Write([]byte(fmt.Sprintf("%v\n", Id)))

  transactionError := logger.dbConnection.Commit()
  if transactionError != nil {
    panic(transactionError)
  }
}

dbConnection是从加载Hood配置文件中获取的:

func New(prefix string) (PostgresLogger) {
  dbConnection, err := hood.Load("/Users/ls/Dropbox/programming/go/src/postgres_logger/db/config.json", "development")
  if err != nil {
    panic(err)
  }

  return PostgresLogger{ prefix: prefix, dbConnection: dbConnection }
}

很好。所以当我启动反向代理并要求它保存传入的请求时,我看到了这个(示例,其中日志以RVSPRXY为前缀):

Saving to PGDB
56
RVSPRXY (1368315177148901322):
[::1]:51142 GET /css/editor.css

Saving to PGDB
RVSPRXY (1368315177149851787):
[::1]:51143 GET /js/handlebars.min.js

Saving to PGDB
RVSPRXY (1368315177150164615):
[::1]:51140 GET /css/mercury.bundle.css

Saving to PGDB
RVSPRXY (1368315177150358938):
[::1]:51141 GET /css/mercury_regions.bundle.css

Saving to PGDB
RVSPRXY (1368315177150776986):
[::1]:51144 GET /js/jquery-2.0.0.min.js

Saving to PGDB
57
58
59
60

所以我们可以看到它递增从Save返回的id,但是我在logging_development数据库中查看,没有记录。

停止并重新启动服务器会继续从上次离开的地方递增id,所以它似乎确实在保存,但是保存在哪里呢?

更新

这是开发配置:

{
   "development": {
     "driver": "postgres",
     "source": "user=logging dbname=logging_development sslmode=disable"
   }
 }

以及PGAdmin显示的一些连接信息:

5289	logging	logging_development	2013-05-11 17:54:48.700467-06	127.0.0.1:51403	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5290	logging	logging_development	2013-05-11 17:54:48.746065-06	127.0.0.1:51414	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5291	logging	logging_development	2013-05-11 17:54:48.747876-06	127.0.0.1:51415	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5292	logging	logging_development	2013-05-11 17:54:48.748086-06	127.0.0.1:51416	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5293	logging	logging_development	2013-05-11 17:54:48.74866-06	

编辑:修复了检查错误的错误(不是问题的原因)

英文:

I have a SaveRequest method that takes and http.Request and then, supposedly, saves it (right now just the path) to a postgres database:

func (logger *PostgresLogger) SaveRequest(req *http.Request) {
  os.Stdout.Write([]byte("Saving to PGDB\n"))
  request := db.Requests { Path: req.URL.Path }
  transaction := logger.dbConnection.Begin()
  Id, saveError := transaction.Save(&request)
  if saveError != nil {
    panic(saveError)
  }

  os.Stdout.Write([]byte(fmt.Sprintf("%v\n", Id)))

  transactionError := logger.dbConnection.Commit()
  if transactionError != nil {
    panic(transactionError)
  }
}

That dbConnection comes from loading a Hood config file:

func New(prefix string) (PostgresLogger) {
  dbConnection, err := hood.Load("/Users/ls/Dropbox/programming/go/src/postgres_logger/db/config.json", "development")
  if err != nil {
    panic(err)
  }

  return PostgresLogger{ prefix: prefix, dbConnection: dbConnection }
}

Cool. So when I start the reverse proxy and ask it to save incoming requests, I see this (sample, where logs are prefixed by RVSPRXY):

Saving to PGDB
56
RVSPRXY (1368315177148901322):
[::1]:51142 GET /css/editor.css

Saving to PGDB
RVSPRXY (1368315177149851787):
[::1]:51143 GET /js/handlebars.min.js

Saving to PGDB
RVSPRXY (1368315177150164615):
[::1]:51140 GET /css/mercury.bundle.css

Saving to PGDB
RVSPRXY (1368315177150358938):
[::1]:51141 GET /css/mercury_regions.bundle.css

Saving to PGDB
RVSPRXY (1368315177150776986):
[::1]:51144 GET /js/jquery-2.0.0.min.js

Saving to PGDB
57
58
59
60

So we can see it incrementing the id being returned from Save, but I look in the logging_development database and there are no records.

Stopping and restarting the server continues to increment the ids from where it left off, so it seems like it is actually saving, but where?

Update

Here is the development config:

{
   "development": {
     "driver": "postgres",
     "source": "user=logging dbname=logging_development sslmode=disable"
   }
 }

And some of what PGAdmin shows for connections:

5289	logging	logging_development	2013-05-11 17:54:48.700467-06	127.0.0.1:51403	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5290	logging	logging_development	2013-05-11 17:54:48.746065-06	127.0.0.1:51414	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5291	logging	logging_development	2013-05-11 17:54:48.747876-06	127.0.0.1:51415	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5292	logging	logging_development	2013-05-11 17:54:48.748086-06	127.0.0.1:51416	INSERT INTO "requests" ("path", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"	
5293	logging	logging_development	2013-05-11 17:54:48.74866-06	

Edit: fixed a bug checking the wrong error (not the cause of the issue)

答案1

得分: 1

你写的代码中有一个错误,logger.dbConnection.Commit()应该改为transaction.Commit()。修改后的代码如下:

func (logger *PostgresLogger) SaveRequest(req *http.Request) {
    os.Stdout.Write([]byte("Saving to PGDB\n"))
    request := db.Requests{Path: req.URL.Path}
    transaction := logger.dbConnection.Begin()
    Id, saveError := transaction.Save(&request)
    if saveError != nil {
        panic(saveError)
    }
    os.Stdout.Write([]byte(fmt.Sprintf("%v\n", Id)))
    // commit transaction
    transactionError := transaction.Commit()
    // check transactionError
    if transactionError != nil {
        panic(transactionError)
    }
}
英文:

You wrote:

func (logger *PostgresLogger) SaveRequest(req *http.Request) {
	os.Stdout.Write([]byte("Saving to PGDB\n"))
	request := db.Requests{Path: req.URL.Path}
	transaction := logger.dbConnection.Begin()
	Id, saveError := transaction.Save(&request)
	if saveError != nil {
		panic(saveError)
	}
	os.Stdout.Write([]byte(fmt.Sprintf("%v\n", Id)))
	transactionError := logger.dbConnection.Commit()
	if saveError != nil {
		panic(transactionError)
	}
}

What results do you get if you write:

func (logger *PostgresLogger) SaveRequest(req *http.Request) {
	os.Stdout.Write([]byte("Saving to PGDB\n"))
	request := db.Requests{Path: req.URL.Path}
	transaction := logger.dbConnection.Begin()
	Id, saveError := transaction.Save(&request)
	if saveError != nil {
		panic(saveError)
	}
	os.Stdout.Write([]byte(fmt.Sprintf("%v\n", Id)))
	// commit transaction
	transactionError := transaction.Commit()
	// check transactionError
	if transactionError != nil {
		panic(transactionError)
	}
}

huangapple
  • 本文由 发表于 2013年5月12日 07:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/16502727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定