我有一个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
编辑:修复了检查错误错误的错误(不是问题的原因)
答案 0 :(得分:1)
您写道:
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)
}
}
如果你写的话会得到什么结果:
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)
}
}