Skip to content

Commit 84998fb

Browse files
authored
Merge pull request #11 from kefniark/feature/logging
Implement Logging
2 parents 3443106 + 0f0c534 commit 84998fb

27 files changed

Lines changed: 1209 additions & 186 deletions

cmd/mangosql/mangosql.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"regexp"
12+
"slices"
1213
"strings"
1314

1415
"github.com/kefniark/mango-sql/internal"
@@ -17,40 +18,64 @@ import (
1718

1819
func main() {
1920
app := &cli.App{
21+
Version: "v0.0.1",
2022
Name: "mangosql",
2123
HelpName: "MangoSQL",
2224
Usage: "Generate a SQL Client from a SQL file or folder of SQL migrations",
2325
UsageText: `Syntax: mangosql [options] <source folder>
2426
Example: mangosql --output db/file.go db/schema.sql`,
25-
Suggest: true,
27+
Suggest: true,
28+
EnableBashCompletion: true,
2629
Flags: []cli.Flag{
2730
&cli.StringFlag{
28-
Name: "output",
29-
Value: "database/client.go",
30-
Usage: "Output file",
31+
Name: "output",
32+
Aliases: []string{"o"},
33+
Value: "database/client.go",
34+
Usage: "Output file",
3135
},
3236
&cli.StringFlag{
33-
Name: "package",
34-
Value: "database",
35-
Usage: "Go Package",
37+
Name: "package",
38+
Aliases: []string{"p"},
39+
Value: "database",
40+
Usage: "Go Package",
3641
},
3742
&cli.StringFlag{
38-
Name: "driver",
39-
Value: "pgx",
40-
Usage: "SQL Driver",
43+
Name: "driver",
44+
Aliases: []string{"d"},
45+
Value: "pgx",
46+
Usage: "SQL Driver",
47+
},
48+
&cli.StringFlag{
49+
Name: "logger",
50+
Aliases: []string{"l"},
51+
Value: "none",
52+
Usage: "Logging library",
4153
},
4254
},
4355
Action: func(ctx *cli.Context) error {
4456
if ctx.NArg() <= 0 {
4557
return fmt.Errorf("missing source folder")
4658
}
4759

60+
allowed_drivers := []string{"pq", "pgx", "sqlite"}
61+
driver := ctx.String("driver")
62+
if !slices.Contains(allowed_drivers, driver) {
63+
return fmt.Errorf("unknown driver, should be one of %v", allowed_drivers)
64+
}
65+
66+
allowed_logger := []string{"none", "zap", "logrus", "zerolog", "console"}
67+
logger := ctx.String("logger")
68+
if !slices.Contains(allowed_logger, logger) {
69+
return fmt.Errorf("unknown logger, should be one of %v", allowed_logger)
70+
}
71+
4872
name := ctx.Args().Get(0)
4973
return generate(GenerateOptions{
5074
Src: name,
5175
Output: ctx.String("output"),
5276
Package: ctx.String("package"),
53-
Driver: ctx.String("driver"),
77+
Driver: driver,
78+
Logger: logger,
5479
})
5580
},
5681
}
@@ -65,6 +90,7 @@ type GenerateOptions struct {
6590
Output string
6691
Package string
6792
Driver string
93+
Logger string
6894
}
6995

7096
func generate(opts GenerateOptions) error {
@@ -110,7 +136,7 @@ func generate(opts GenerateOptions) error {
110136
var b bytes.Buffer
111137
contents := bufio.NewWriter(&b)
112138

113-
if err = internal.Generate(schema, contents, opts.Package, opts.Driver); err != nil {
139+
if err = internal.Generate(schema, contents, opts.Package, opts.Driver, opts.Logger); err != nil {
114140
return err
115141
}
116142

docs/.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default defineConfig({
3434
items: [
3535
// { text: 'UUID Generation', link: '/api/mutations' },
3636
{ text: 'Soft Delete', link: '/features/soft-delete' },
37-
// { text: 'Logging', link: '/api/mutations' },
37+
{ text: 'Logging', link: '/features/logging' },
3838
// { text: 'Migrations', link: '/api/mutations' },
3939
]
4040
},

docs/features/logging.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Logging
2+
3+
MangoSQL has integration for common Go loggers, this make working with SQL queries easier.
4+
5+
This logging feature is opt-in and comes with:
6+
* Every Query will emit a **Debug** log
7+
* With the function name
8+
* With duration measurements
9+
* With SQL Query and arguments
10+
* **warning** for slow queries, when a query takes more than >500ms
11+
* Every SQL error will be automatically logged as **error**
12+
13+
::: tips
14+
15+
During development, we recommend to set the log level to `DebugLevel` to see the SQL queries generated and how long they take.
16+
17+
:::
18+
19+
## Logrus (https://github.com/sirupsen/logrus) { #logrus }
20+
21+
Add `--logger logrus` to the cli command
22+
23+
```sh
24+
mangosql --logger logrus ./schema.sql
25+
```
26+
27+
And provide the logger to MangoSQL Client at initialization.
28+
29+
```go
30+
package database
31+
32+
import (
33+
// ...
34+
"github.com/sirupsen/logrus"
35+
)
36+
37+
// create your own logger instance
38+
logger := logrus.New()
39+
40+
// instantiate DBClient
41+
return New(db, logger)
42+
```
43+
44+
## Zap (https://github.com/uber-go/zap) { #zap }
45+
46+
Add `--logger zap` to the cli command
47+
48+
```sh
49+
mangosql --logger zap ./schema.sql
50+
```
51+
52+
And provide the logger to MangoSQL Client at initialization.
53+
54+
```go
55+
package database
56+
57+
import (
58+
// ...
59+
"go.uber.org/zap"
60+
)
61+
62+
// create your own logger instance
63+
logger, _ := zap.NewProduction()
64+
65+
// instantiate DBClient
66+
return New(db, logger)
67+
```
68+
69+
## Zerolog (https://github.com/rs/zerolog) { #zerolog }
70+
71+
Add `--logger zerolog` to the cli command
72+
73+
```sh
74+
mangosql --logger zerolog ./schema.sql
75+
```
76+
77+
And provide the logger to MangoSQL Client at initialization.
78+
79+
```go
80+
package database
81+
82+
import (
83+
// ...
84+
"github.com/rs/zerolog"
85+
)
86+
87+
// create your own logger instance
88+
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
89+
90+
// instantiate DBClient
91+
return New(db, logger)
92+
```
93+
94+
## Testing / Console
95+
96+
This logger option is a bit special, this is a console writer intended for development and testing only.
97+
It's literally just calling `log.Println()`, has colors and is zero configuration.
98+
99+
Add `--logger console` to the cli command and you have nothing else to modify in your code.
100+
101+
```sh
102+
mangosql --logger console ./schema.sql
103+
```
104+
105+
The output will looks like this
106+
```logs
107+
2024/08/20 04:32:46 [DEBUG] DB.User.FindMany 308.236µs
108+
| Args: [[1]]
109+
| SQL: SELECT id, name, created_at, deleted_at FROM users WHERE id = $1 LIMIT 1 OFFSET 0
110+
```

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ require (
88
github.com/Masterminds/squirrel v1.5.4
99
github.com/auxten/postgresql-parser v1.0.1
1010
github.com/gertd/go-pluralize v0.2.1
11+
github.com/go-echarts/go-echarts/v2 v2.4.1
1112
github.com/google/uuid v1.6.0
1213
github.com/iancoleman/strcase v0.3.0
1314
github.com/jackc/pgx/v5 v5.6.0
1415
github.com/jmoiron/sqlx v1.4.0
1516
github.com/lib/pq v1.10.9
1617
github.com/peterldowns/pgtestdb v0.0.14
1718
github.com/peterldowns/pgtestdb/migrators/goosemigrator v0.0.14
19+
github.com/rs/zerolog v1.33.0
20+
github.com/sirupsen/logrus v1.9.3
1821
github.com/stretchr/testify v1.9.0
1922
github.com/urfave/cli/v2 v2.27.4
23+
go.uber.org/zap v1.27.0
2024
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
2125
gorm.io/driver/postgres v1.5.9
2226
gorm.io/driver/sqlite v1.5.6
@@ -49,6 +53,7 @@ require (
4953
github.com/kr/text v0.2.0 // indirect
5054
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
5155
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
56+
github.com/mattn/go-colorable v0.1.13 // indirect
5257
github.com/mattn/go-isatty v0.0.20 // indirect
5358
github.com/mattn/go-sqlite3 v1.14.22 // indirect
5459
github.com/mfridman/interpolate v0.0.2 // indirect
@@ -60,7 +65,6 @@ require (
6065
github.com/rogpeppe/go-internal v1.12.0 // indirect
6166
github.com/russross/blackfriday/v2 v2.1.0 // indirect
6267
github.com/sethvargo/go-retry v0.3.0 // indirect
63-
github.com/sirupsen/logrus v1.9.3 // indirect
6468
github.com/spf13/pflag v1.0.5 // indirect
6569
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
6670
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcju
4141
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
4242
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
4343
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
44+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
4445
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
4546
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
4647
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@@ -77,6 +78,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
7778
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
7879
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
7980
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
81+
github.com/go-echarts/go-echarts/v2 v2.4.1 h1:imBFGngJ9zv/2zJVjK3k0uLL+LzyPDgzeV7MWzxH0rs=
82+
github.com/go-echarts/go-echarts/v2 v2.4.1/go.mod h1:56YlvzhW/a+du15f3S2qUGNDfKnFOeJSThBIrVFHDtI=
8083
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
8184
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
8285
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
@@ -86,6 +89,7 @@ github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqw
8689
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
8790
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
8891
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
92+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
8993
github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
9094
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
9195
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
@@ -191,9 +195,13 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
191195
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
192196
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
193197
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
198+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
199+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
194200
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
195201
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
196202
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
203+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
204+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
197205
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
198206
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
199207
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
@@ -249,6 +257,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
249257
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
250258
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
251259
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
260+
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
261+
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
262+
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
252263
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
253264
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
254265
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -299,8 +310,12 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDf
299310
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
300311
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
301312
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
313+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
314+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
302315
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
303316
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
317+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
318+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
304319
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
305320
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
306321
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -365,7 +380,9 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
365380
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
366381
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
367382
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
383+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
368384
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
385+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
369386
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
370387
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
371388
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)