Skip to content

Commit b328187

Browse files
authored
Merge pull request #267 from carlosms/go-log
Use go-log lib
2 parents 7fc767f + 9aab534 commit b328187

7 files changed

Lines changed: 48 additions & 20 deletions

File tree

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ COMPOSE := docker-compose
2626
GO_BINDATA_TAG := bindata
2727

2828
# Environment and arguments to use in `go run` calls.
29-
GO_RUN_ENV := GITBASEPG_ENV=dev
29+
GO_RUN_ENV := LOG_LEVEL=DEBUG
3030
GO_RUN_ARGS += -tags "$(GO_BINDATA_TAG)"
3131

3232
GORUN = $(GOCMD) run $(GO_RUN_ARGS)

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ Any of the previous execution methods accept configuration through the following
7777
| `GITBASEPG_SERVER_URL` | `--server` | | URL used to access the application in the form `HOSTNAME[:PORT]`. Leave it unset to allow connections from any proxy or public address |
7878
| `GITBASEPG_DB_CONNECTION` | `--db` | `root@tcp(localhost:3306)/none?maxAllowedPacket=4194304` | gitbase connection string. Use the DSN (Data Source Name) format described in the [Go MySQL Driver docs](https://github.com/go-sql-driver/mysql#dsn-data-source-name). |
7979
| `GITBASEPG_BBLFSH_SERVER_URL` | `--bblfsh` | `127.0.0.1:9432` | Address where bblfsh server is listening |
80-
| `GITBASEPG_ENV` | `--env` | `production` | Sets the log level. Use `dev` to enable debug log messages |
8180
| `GITBASEPG_SELECT_LIMIT` | `--select-limit` | `100` | Default `LIMIT` forced on all the SQL queries done from the UI. Set it to 0 to remove any limit |
8281
| `GITBASEPG_FOOTER_HTML` | `--footer` | | Allows to add any custom html to the page footer. It must be a string encoded in base64. Use it, for example, to add your analytics tracking code snippet |
82+
| `LOG_LEVEL` | `--log-level=` | `info` | Logging level (`info`, `debug`, `warning` or `error`) |
83+
| `LOG_FORMAT` | `--log-format=` | | log format (`text` or `json`), defaults to `text` on a terminal and `json` otherwise |
84+
| `LOG_FIELDS` | `--log-fields=` | | default fields for the logger, specified in json |
85+
| `LOG_FORCE_FORMAT` | `--log-force-format` | | ignore if it is running on a terminal or not |
86+
8387

8488
# Contribute
8589

cmd/gitbase-web/main.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package main
22

33
import (
44
"database/sql"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78

9+
"github.com/sirupsen/logrus"
810
"github.com/src-d/gitbase-web/server"
911
"github.com/src-d/gitbase-web/server/handler"
10-
"github.com/src-d/gitbase-web/server/service"
1112

1213
_ "github.com/go-sql-driver/mysql"
1314
"gopkg.in/src-d/go-cli.v0"
15+
"gopkg.in/src-d/go-log.v1"
1416
)
1517

1618
// version will be replaced automatically by the CI build.
@@ -30,7 +32,7 @@ var app = cli.New(name, version, build, "gitbase web client")
3032
// https://github.com/go-sql-driver/mysql/pull/680
3133
type ServeCommand struct {
3234
cli.PlainCommand `name:"serve" short-description:"serve the app" long-description:"starts serving the application"`
33-
Env string `long:"env" env:"GITBASEPG_ENV" default:"production" description:"Sets the log level. Use 'dev' to enable debug log messages"`
35+
cli.LogOptions `group:"Log Options"`
3436
Host string `long:"host" env:"GITBASEPG_HOST" default:"0.0.0.0" description:"IP address to bind the HTTP server"`
3537
Port int `long:"port" env:"GITBASEPG_PORT" default:"8080" description:"Port to bind the HTTP server"`
3638
ServerURL string `long:"server" env:"GITBASEPG_SERVER_URL" description:"URL used to access the application in the form 'HOSTNAME[:PORT]'. Leave it unset to allow connections from any proxy or public address"`
@@ -41,13 +43,12 @@ type ServeCommand struct {
4143
}
4244

4345
func (c *ServeCommand) Execute(args []string) error {
44-
// logger
45-
logger := service.NewLogger(c.Env)
46+
c.initLog()
4647

4748
// database
4849
db, err := sql.Open("mysql", c.DBConn)
4950
if err != nil {
50-
logger.Fatalf("error opening the database: %s", err)
51+
return fmt.Errorf("error opening the database: %s", err.Error())
5152
}
5253
defer db.Close()
5354

@@ -56,14 +57,35 @@ func (c *ServeCommand) Execute(args []string) error {
5657
static := handler.NewStatic("build/public", c.ServerURL, c.SelectLimit, c.FooterHTML)
5758

5859
// start the router
59-
router := server.Router(logger, static, version, db, c.BblfshServerURL)
60-
logger.Infof("listening on %s:%d", c.Host, c.Port)
61-
err = http.ListenAndServe(fmt.Sprintf("%s:%d", c.Host, c.Port), router)
62-
logger.Fatal(err)
60+
router := server.Router(logrus.StandardLogger(), static, version, db, c.BblfshServerURL)
61+
62+
log.With(log.Fields{"version": version, "build": build}).
63+
Infof("listening on %s:%d", c.Host, c.Port)
6364

64-
return nil
65+
err = http.ListenAndServe(fmt.Sprintf("%s:%d", c.Host, c.Port), router)
66+
log.Errorf(err, "")
67+
return err
6568
}
6669

70+
func (c *ServeCommand) initLog() {
71+
if c.LogFields == "" {
72+
bytes, err := json.Marshal(log.Fields{"app": name})
73+
if err != nil {
74+
panic(err)
75+
}
76+
c.LogFields = string(bytes)
77+
}
78+
79+
log.DefaultFactory = &log.LoggerFactory{
80+
Level: c.LogLevel,
81+
Format: c.LogFormat,
82+
Fields: c.LogFields,
83+
ForceFormat: c.LogForceFormat,
84+
}
85+
log.DefaultFactory.ApplyToLogrus()
86+
87+
log.DefaultLogger = log.New(nil)
88+
}
6789
func main() {
6890
app.AddCommand(&ServeCommand{})
6991

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ services:
66
ports:
77
- "8080:8080"
88
environment:
9-
GITBASEPG_ENV: ${GITBASEPG_ENV}
109
GITBASEPG_DB_CONNECTION: root@tcp(gitbase:3306)/none?maxAllowedPacket=4194304
1110
GITBASEPG_BBLFSH_SERVER_URL: bblfsh:9432
1211
depends_on:

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Instead of rebuilding the frontend and restarting the backend every time you do
8888
In one terminal run the Go backend:
8989

9090
```bash
91-
$ GITBASEPG_ENV=dev go run cmd/gitbase-web/main.go serve
91+
$ LOG_LEVEL=DEBUG go run cmd/gitbase-web/main.go serve
9292
```
9393

9494
In another terminal, run the frontend:

server/handler/query.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"strings"
1313

14+
"github.com/pressly/lg"
1415
"github.com/src-d/gitbase-web/server/serializer"
1516
"github.com/src-d/gitbase-web/server/service"
1617

@@ -87,7 +88,7 @@ func Query(db service.SQLDB) RequestProcessFunc {
8788
}
8889

8990
if r.Context().Err() != nil {
90-
killQuery(db, query)
91+
killQuery(r, db, query)
9192
return nil, dbError(r.Context().Err())
9293
}
9394

@@ -127,10 +128,11 @@ func Query(db service.SQLDB) RequestProcessFunc {
127128
}
128129
}
129130

130-
func killQuery(db service.SQLDB, query string) {
131-
pRows, pErr := db.Query("SHOW FULL PROCESSLIST")
131+
func killQuery(r *http.Request, db service.SQLDB, query string) {
132+
const showProcessList = "SHOW FULL PROCESSLIST"
133+
pRows, pErr := db.Query(showProcessList)
132134
if pErr != nil {
133-
// TODO (carlosms) log error when we migrate to go-log
135+
lg.RequestLog(r).WithError(pErr).Errorf("failed to execute %q", showProcessList)
134136
return
135137
}
136138
defer pRows.Close()
@@ -146,15 +148,15 @@ func killQuery(db service.SQLDB, query string) {
146148
// Id, User, Host, db, Command, Time, State, Info
147149
// gitbase returns the query on "Info".
148150
if err := pRows.Scan(&id, &rb, &rb, &rb, &rb, &rb, &rb, &info); err != nil {
149-
// TODO (carlosms) log error when we migrate to go-log
151+
lg.RequestLog(r).WithError(err).Errorf("failed to scan the results of %q", showProcessList)
150152
return
151153
}
152154

153155
if info.Valid && info.String == query {
154156
if found {
155157
// Found more than one match for current query, we cannot know which
156158
// one is ours. Skip the cancellation
157-
// TODO (carlosms) log error when we migrate to go-log
159+
lg.RequestLog(r).Errorf("cannot cancel the query, found more than one match in gitbase")
158160
return
159161
}
160162

0 commit comments

Comments
 (0)