Skip to content

Commit

Permalink
Merge pull request #59 from ulule/chore/remove-semicolon-and-newline-…
Browse files Browse the repository at this point in the history
…in-logs

chore: remove semicolon and newline from logs
  • Loading branch information
yansal authored May 27, 2019
2 parents 4915d2d + fbabf33 commit ed4cd9d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 34 deletions.
8 changes: 4 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func Exec(ctx context.Context, driver Driver, stmt builder.Builder, dest ...interface{}) error {
if driver.hasLogger() {
start := time.Now()
queries := Queries{NewQuery(stmt)}
query := NewQuery(stmt)

defer func() {
Log(driver, queries, time.Since(start))
Log(driver, query, time.Since(start))
}()
}

Expand All @@ -39,10 +39,10 @@ func Exec(ctx context.Context, driver Driver, stmt builder.Builder, dest ...inte
func RawExec(ctx context.Context, driver Driver, query string, dest ...interface{}) error {
if driver.hasLogger() {
start := time.Now()
queries := Queries{NewRawQuery(query)}
query := NewRawQuery(query)

defer func() {
Log(driver, queries, time.Since(start))
Log(driver, query, time.Since(start))
}()
}

Expand Down
8 changes: 4 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type Logger interface {
Log(query string, duration time.Duration)
}

// Log will emmit given queries on driver's attached Logger.
func Log(driver Driver, queries Queries, duration time.Duration) {
if driver == nil || len(queries) == 0 || !driver.hasLogger() {
// Log will emmit given query on driver's attached Logger.
func Log(driver Driver, query Query, duration time.Duration) {
if driver == nil && !driver.hasLogger() {
return
}
go func() {
query := queries.String()
query := query.String()
driver.logger().Log(query, duration)
}()
}
7 changes: 3 additions & 4 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (e *logger) Log(query string, duration time.Duration) {
}

var ErrLogTimeout = fmt.Errorf("logger timeout")
var EOL = "\n"

func (e *logger) read() (string, error) {
select {
Expand Down Expand Up @@ -50,7 +49,7 @@ func TestLogger(t *testing.T) {
is.NoError(err)
expected := fmt.Sprint(
`INSERT INTO ztp_owl (favorite_food, feather_color, group_id, name) VALUES `,
`('Shrimps', 'lavender', NULL, 'Guacamowle') RETURNING id;`, EOL,
`('Shrimps', 'lavender', NULL, 'Guacamowle') RETURNING id`,
)

log, err := logger.read()
Expand All @@ -62,7 +61,7 @@ func TestLogger(t *testing.T) {
is.NoError(err)
expected = fmt.Sprint(
`UPDATE ztp_owl SET favorite_food = 'Shrimps', feather_color = 'lavender', group_id = NULL, `,
`name = 'Nibbles' WHERE (id = `, format.Int(owl.ID), `);`, EOL,
`name = 'Nibbles' WHERE (id = `, format.Int(owl.ID), `)`,
)

log, err = logger.read()
Expand All @@ -71,7 +70,7 @@ func TestLogger(t *testing.T) {

err = makroud.Delete(ctx, driver, owl)
is.NoError(err)
expected = fmt.Sprint(`DELETE FROM ztp_owl WHERE (id = `, format.Int(owl.ID), `);`, EOL)
expected = fmt.Sprint(`DELETE FROM ztp_owl WHERE (id = `, format.Int(owl.ID), `)`)

log, err = logger.read()
is.NoError(err)
Expand Down
23 changes: 1 addition & 22 deletions queries.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
package makroud

import (
"bytes"
"strings"

lkb "github.com/ulule/loukoum/v3/builder"
)

// Queries is a list of Query instances.
type Queries []Query

func (q Queries) String() string {
buffer := &bytes.Buffer{}
for i := range q {
query := q[i].String()
buffer.WriteString(query)
if !strings.HasSuffix(query, ";") {
buffer.WriteString(";")
}
buffer.WriteString("\n")
}
return buffer.String()
}
import lkb "github.com/ulule/loukoum/v3/builder"

// Query is a query generated by loukoum and makroud.
type Query struct {
Expand Down

0 comments on commit ed4cd9d

Please sign in to comment.