Skip to content

Commit

Permalink
Refactor logger (#186)
Browse files Browse the repository at this point in the history
* WIP refactor logger

* Add log levels

* Refactor a bit to avoid breaking change

* Replace string log level with iota consts

* Fix missing import

* Restore old dropCmd
  • Loading branch information
stanislas-m authored Aug 7, 2018
1 parent 3f06dc6 commit 1f17dac
Show file tree
Hide file tree
Showing 24 changed files with 189 additions and 115 deletions.
1 change: 0 additions & 1 deletion columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func (c *Columns) Add(names ...string) []*Column {
}

col = c.Cols[xs[0]]
//fmt.Printf("column: %v, col: %v, xs: %v, ss: %v\n", xs[0], col, xs, ss)
if col == nil {
if ss == "" {
ss = xs[0]
Expand Down
5 changes: 3 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package pop
import (
"fmt"

"github.com/gobuffalo/pop/logging"
"github.com/pkg/errors"
)

// CreateDB creates a database, given a connection definition
func CreateDB(c *Connection) error {
deets := c.Dialect.Details()
if deets.Database != "" {
Log(fmt.Sprintf("Create %s (%s)", deets.Database, c.URL()))
log(logging.Info, fmt.Sprintf("create %s (%s)", deets.Database, c.URL()))
return errors.Wrapf(c.Dialect.CreateDB(), "couldn't create database %s", deets.Database)
}
return nil
Expand All @@ -20,7 +21,7 @@ func CreateDB(c *Connection) error {
func DropDB(c *Connection) error {
deets := c.Dialect.Details()
if deets.Database != "" {
Log(fmt.Sprintf("Drop %s (%s)", deets.Database, c.URL()))
log(logging.Info, fmt.Sprintf("drop %s (%s)", deets.Database, c.URL()))
return errors.Wrapf(c.Dialect.DropDB(), "couldn't drop database %s", deets.Database)
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/template"

"github.com/gobuffalo/envy"
"github.com/gobuffalo/pop/logging"
"github.com/pkg/errors"

"gopkg.in/yaml.v2"
Expand All @@ -20,6 +21,8 @@ var lookupPaths = []string{"", "./config", "/config", "../", "../config", "../..
var ConfigName = "database.yml"

func init() {
SetLogger(defaultLogger)

ap := os.Getenv("APP_PATH")
if ap != "" {
AddLookupPaths(ap)
Expand All @@ -38,7 +41,7 @@ func LoadConfigFile() error {
return errors.WithStack(err)
}
Connections = map[string]*Connection{}
Log("Loading config file from %s\n", path)
log(logging.Info, "Loading config file from %s", path)
f, err := os.Open(path)
if err != nil {
return errors.WithStack(err)
Expand Down
6 changes: 4 additions & 2 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ func (cd *ConnectionDetails) Finalize() error {
return nil
}

// Parse is deprecated! Please use `ConnectionDetails.Finalize()` instead!
// Parse cleans up the connection details by normalizing names,
// filling in default values, etc...
// Deprecated: use ConnectionDetails.Finalize() instead.
func (cd *ConnectionDetails) Parse(port string) error {
fmt.Println("[POP] ConnectionDetails#Parse(port string) has been deprecated!")
fmt.Println("Warning: ConnectionDetails#Parse(port string) is deprecated, and will be removed in a future version. Please use ForStructWithAlias instead.")
return cd.Finalize()
}

Expand Down
17 changes: 9 additions & 8 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gobuffalo/fizz"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/logging"
"github.com/gobuffalo/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
Expand Down Expand Up @@ -44,7 +45,7 @@ func genericCreate(s store, model *Model, cols columns.Columns) error {
var id int64
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
log(logging.SQL, query)
res, err := s.NamedExec(query, model.Value)
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -72,7 +73,7 @@ func genericCreate(s store, model *Model, cols columns.Columns) error {
w := cols.Writeable()
w.Add("id")
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
log(logging.SQL, query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return errors.WithStack(err)
Expand All @@ -91,7 +92,7 @@ func genericCreate(s store, model *Model, cols columns.Columns) error {

func genericUpdate(s store, model *Model, cols columns.Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where %s", model.TableName(), cols.Writeable().UpdateString(), model.whereID())
Log(stmt)
log(logging.SQL, stmt)
_, err := s.NamedExec(stmt, model.Value)
if err != nil {
return errors.WithStack(err)
Expand All @@ -109,7 +110,7 @@ func genericDestroy(s store, model *Model) error {
}

func genericExec(s store, stmt string) error {
Log(stmt)
log(logging.SQL, stmt)
_, err := s.Exec(stmt)
if err != nil {
return errors.WithStack(err)
Expand All @@ -119,7 +120,7 @@ func genericExec(s store, stmt string) error {

func genericSelectOne(s store, model *Model, query Query) error {
sql, args := query.ToSQL(model)
Log(sql, args...)
log(logging.SQL, sql, args...)
err := s.Get(model.Value, sql, args...)
if err != nil {
return errors.WithStack(err)
Expand All @@ -129,7 +130,7 @@ func genericSelectOne(s store, model *Model, query Query) error {

func genericSelectMany(s store, models *Model, query Query) error {
sql, args := query.ToSQL(models)
Log(sql, args...)
log(logging.SQL, sql, args...)
err := s.Select(models.Value, sql, args...)
if err != nil {
return errors.WithStack(err)
Expand All @@ -152,7 +153,7 @@ func genericLoadSchema(deets *ConnectionDetails, migrationURL string, r io.Reade
}

if len(contents) == 0 {
fmt.Printf("schema is empty for %s, skipping\n", deets.Database)
log(logging.Info, "schema is empty for %s, skipping", deets.Database)
return nil
}

Expand All @@ -161,6 +162,6 @@ func genericLoadSchema(deets *ConnectionDetails, migrationURL string, r io.Reade
return errors.WithMessage(err, fmt.Sprintf("unable to load schema for %s", deets.Database))
}

fmt.Printf("loaded schema for %s\n", deets.Database)
log(logging.Info, "loaded schema for %s", deets.Database)
return nil
}
15 changes: 8 additions & 7 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func (p *cockroach) Create(s store, model *Model, cols columns.Columns) error {
}{}
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) returning id", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
log(logging.SQL, query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -89,14 +90,14 @@ func (p *cockroach) CreateDB() error {
}
defer db.Close()
query := fmt.Sprintf("CREATE DATABASE \"%s\"", deets.Database)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error creating Cockroach database %s", deets.Database)
}

fmt.Printf("created database %s\n", deets.Database)
log(logging.Info, "created database %s", deets.Database)
return nil
}

Expand All @@ -108,14 +109,14 @@ func (p *cockroach) DropDB() error {
}
defer db.Close()
query := fmt.Sprintf("DROP DATABASE \"%s\" CASCADE;", deets.Database)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error dropping Cockroach database %s", deets.Database)
}

fmt.Printf("dropped database %s\n", deets.Database)
log(logging.Info, "dropped database %s", deets.Database)
return nil
}

Expand Down Expand Up @@ -170,7 +171,7 @@ func (p *cockroach) DumpSchema(w io.Writer) error {
secure = "--insecure"
}
cmd := exec.Command("cockroach", "dump", p.Details().Database, "--dump-mode=schema", secure)
Log(strings.Join(cmd.Args, " "))
log(logging.SQL, strings.Join(cmd.Args, " "))
cmd.Stdout = w
cmd.Stderr = os.Stderr

Expand All @@ -179,7 +180,7 @@ func (p *cockroach) DumpSchema(w io.Writer) error {
return err
}

fmt.Printf("dumped schema for %s\n", p.Details().Database)
log(logging.Info, "dumped schema for %s", p.Details().Database)
return nil
}

Expand Down
19 changes: 10 additions & 9 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/logging"
"github.com/jmoiron/sqlx"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
Expand All @@ -39,7 +40,7 @@ func (m *mysql) URL() string {
}
encoding := defaults.String(deets.Encoding, "utf8_general_ci")
if deets.Encoding == "" {
Log(`Warning: The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
log(logging.Warn, `The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
}
s := "%s:%s@(%s:%s)/%s?parseTime=true&multiStatements=true&readTimeout=1s&collation=%s"
return fmt.Sprintf(s, deets.User, deets.Password, deets.Host, deets.Port, deets.Database, encoding)
Expand All @@ -54,7 +55,7 @@ func (m *mysql) urlWithoutDb() string {
}
encoding := defaults.String(deets.Encoding, "utf8_general_ci")
if deets.Encoding == "" {
Log(`Warning: The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
log(logging.Warn, `The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
}
s := "%s:%s@(%s:%s)/?parseTime=true&multiStatements=true&readTimeout=1s&collation=%s"
return fmt.Sprintf(s, deets.User, deets.Password, deets.Host, deets.Port, encoding)
Expand Down Expand Up @@ -94,17 +95,17 @@ func (m *mysql) CreateDB() error {
defer db.Close()
encoding := defaults.String(deets.Encoding, "utf8_general_ci")
if deets.Encoding == "" {
Log(`Warning: The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
log(logging.Warn, `The default encoding will change to "utf8mb4_general_ci" in the next version. Set the "encoding" param in your connection setup to "utf8_general_ci" if you still want to use this encoding with MySQL.`)
}
query := fmt.Sprintf("CREATE DATABASE `%s` DEFAULT COLLATE `%s`", deets.Database, encoding)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error creating MySQL database %s", deets.Database)
}

fmt.Printf("created database %s\n", deets.Database)
log(logging.Info, "created database %s", deets.Database)
return nil
}

Expand All @@ -117,14 +118,14 @@ func (m *mysql) DropDB() error {
}
defer db.Close()
query := fmt.Sprintf("DROP DATABASE `%s`", deets.Database)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error dropping MySQL database %s", deets.Database)
}

fmt.Printf("dropped database %s\n", deets.Database)
log(logging.Info, "dropped database %s", deets.Database)
return nil
}

Expand All @@ -147,7 +148,7 @@ func (m *mysql) DumpSchema(w io.Writer) error {
if deets.Port == "socket" {
cmd = exec.Command("mysqldump", "-d", "-S", deets.Host, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
}
Log(strings.Join(cmd.Args, " "))
log(logging.SQL, strings.Join(cmd.Args, " "))
cmd.Stdout = w
cmd.Stderr = os.Stderr

Expand All @@ -156,7 +157,7 @@ func (m *mysql) DumpSchema(w io.Writer) error {
return err
}

fmt.Printf("dumped schema for %s\n", m.Details().Database)
log(logging.Info, "dumped schema for %s", m.Details().Database)
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions dialect_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func (p *postgresql) Create(s store, model *Model, cols columns.Columns) error {
}{}
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) returning id", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
log(logging.SQL, query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -86,14 +87,14 @@ func (p *postgresql) CreateDB() error {
}
defer db.Close()
query := fmt.Sprintf("CREATE DATABASE \"%s\"", deets.Database)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error creating PostgreSQL database %s", deets.Database)
}

fmt.Printf("created database %s\n", deets.Database)
log(logging.Info, "created database %s", deets.Database)
return nil
}

Expand All @@ -105,14 +106,14 @@ func (p *postgresql) DropDB() error {
}
defer db.Close()
query := fmt.Sprintf("DROP DATABASE \"%s\"", deets.Database)
Log(query)
log(logging.SQL, query)

_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error dropping PostgreSQL database %s", deets.Database)
}

fmt.Printf("dropped database %s\n", deets.Database)
log(logging.Info, "dropped database %s", deets.Database)
return nil
}

Expand Down Expand Up @@ -166,7 +167,7 @@ func (p *postgresql) Lock(fn func() error) error {

func (p *postgresql) DumpSchema(w io.Writer) error {
cmd := exec.Command("pg_dump", "-s", fmt.Sprintf("--dbname=%s", p.URL()))
Log(strings.Join(cmd.Args, " "))
log(logging.SQL, strings.Join(cmd.Args, " "))
cmd.Stdout = w
cmd.Stderr = os.Stderr

Expand All @@ -175,7 +176,7 @@ func (p *postgresql) DumpSchema(w io.Writer) error {
return err
}

fmt.Printf("dumped schema for %s\n", p.Details().Database)
log(logging.Info, "dumped schema for %s", p.Details().Database)
return nil
}

Expand Down
Loading

0 comments on commit 1f17dac

Please sign in to comment.