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)
|
> 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
|
```golang
|
||||||
// After we inject a logger, all subsequent queries
|
// After we inject a logger, all subsequent queries
|
||||||
// will use this logger.
|
// will use this logger.
|
||||||
//
|
//
|
||||||
// You can also inject the ksql.ErrorLogger if you only
|
// You can also inject the ksql.ErrorLogger if you only
|
||||||
// care about these logs when a query error happens.
|
// care about these logs when a query error happens.
|
||||||
ctx = ksql.InjectLogger(ctx, ksql.Logger)
|
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}
|
// 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 (
|
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
age INTEGER,
|
age INTEGER,
|
||||||
name TEXT
|
name TEXT
|
||||||
)`)
|
)`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// This logs: {"query":"INSERT INTO `users` (`name`, `age`) VALUES (?, ?)","params":["Alison",22]}
|
// This logs: {"query":"INSERT INTO `users` (`name`, `age`) VALUES (?, ?)","params":["Alison",22]}
|
||||||
var alison = User{
|
var alison = User{
|
||||||
Name: "Alison",
|
Name: "Alison",
|
||||||
Age: 22,
|
Age: 22,
|
||||||
}
|
}
|
||||||
err = db.Insert(ctx, UsersTable, &alison)
|
err = db.Insert(ctx, UsersTable, &alison)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// This logs: {"query":"SELECT `id`, `name`, `age` FROM users LIMIT 10","params":null}
|
// This logs: {"query":"SELECT `id`, `name`, `age` FROM users LIMIT 10","params":null}
|
||||||
var users []User
|
var users []User
|
||||||
err = db.Query(ctx, &users, "FROM users LIMIT 10")
|
err = db.Query(ctx, &users, "FROM users LIMIT 10")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user