mirror of
https://github.com/VinGarcia/ksql.git
synced 2025-09-04 19:36:56 +00:00
Updated Debugging with ksql.InjectLogger
(markdown)
parent
72a9253f17
commit
1a7c423711
@ -7,37 +7,37 @@ The example below illustrates how this can be done:
|
||||
> Note: The example below is a snippet from a more complete example that can be found [here](https://github.com/VinGarcia/ksql/blob/master/examples/logging_queries/main.go)
|
||||
|
||||
```golang
|
||||
// After we inject a logger, all subsequent queries
|
||||
// will use this logger.
|
||||
//
|
||||
// You can also inject the ksql.ErrorLogger if you only
|
||||
// care about these logs when a query error happens.
|
||||
ctx = ksql.InjectLogger(ctx, ksql.Logger)
|
||||
// After we inject a logger, all subsequent queries
|
||||
// will use this logger.
|
||||
//
|
||||
// You can also inject the ksql.ErrorLogger if you only
|
||||
// care about these logs when a query error happens.
|
||||
ctx = ksql.InjectLogger(ctx, ksql.Logger)
|
||||
|
||||
// This logs: {"query":"CREATE TABLE IF NOT EXISTS users (\n\t id INTEGER PRIMARY KEY,\n\t\tage INTEGER,\n\t\tname TEXT\n\t)","params":null}
|
||||
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
age INTEGER,
|
||||
name TEXT
|
||||
)`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
// This logs: {"query":"CREATE TABLE IF NOT EXISTS users (\n\t id INTEGER PRIMARY KEY,\n\t\tage INTEGER,\n\t\tname TEXT\n\t)","params":null}
|
||||
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
age INTEGER,
|
||||
name TEXT
|
||||
)`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// This logs: {"query":"INSERT INTO `users` (`name`, `age`) VALUES (?, ?)","params":["Alison",22]}
|
||||
var alison = User{
|
||||
Name: "Alison",
|
||||
Age: 22,
|
||||
}
|
||||
err = db.Insert(ctx, UsersTable, &alison)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
// This logs: {"query":"INSERT INTO `users` (`name`, `age`) VALUES (?, ?)","params":["Alison",22]}
|
||||
var alison = User{
|
||||
Name: "Alison",
|
||||
Age: 22,
|
||||
}
|
||||
err = db.Insert(ctx, UsersTable, &alison)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// This logs: {"query":"SELECT `id`, `name`, `age` FROM users LIMIT 10","params":null}
|
||||
var users []User
|
||||
err = db.Query(ctx, &users, "FROM users LIMIT 10")
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
// This logs: {"query":"SELECT `id`, `name`, `age` FROM users LIMIT 10","params":null}
|
||||
var users []User
|
||||
err = db.Query(ctx, &users, "FROM users LIMIT 10")
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user