Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #796 from gobuffalo/task/moving-logs-to-logrus
Browse files Browse the repository at this point in the history
Moving log and fmt PrintX to be logrus Info and Debug
  • Loading branch information
markbates authored Dec 20, 2017
2 parents 081cc68 + a44841a commit ec4fab3
Show file tree
Hide file tree
Showing 25 changed files with 78 additions and 72 deletions.
19 changes: 10 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package buffalo
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
Expand All @@ -12,6 +11,8 @@ import (
"sync"
"syscall"

"github.com/sirupsen/logrus"

"github.com/gobuffalo/envy"
"github.com/gorilla/mux"
"github.com/markbates/going/defaults"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (a *App) Start(port string) error {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}

log.Println(warningMsg)
logrus.Info(warningMsg)

a.Addr = defaults.String(a.Addr, fmt.Sprintf("%s:%s", envy.Get("ADDR", "127.0.0.1"), port))
return a.Serve()
Expand All @@ -53,7 +54,7 @@ func (a *App) Start(port string) error {
// interrupt and kill signals and will attempt to stop the application
// gracefully. This will also start the Worker process, unless WorkerOff is enabled.
func (a *App) Serve() error {
fmt.Printf("Starting application at %s\n", a.Options.Addr)
logrus.Infof("Starting application at %s", a.Options.Addr)
server := http.Server{
Handler: a,
}
Expand All @@ -63,25 +64,25 @@ func (a *App) Serve() error {
go func() {
// gracefully shut down the application when the context is cancelled
<-ctx.Done()
fmt.Println("Shutting down application")
logrus.Info("Shutting down application")

err := a.Stop(ctx.Err())
if err != nil {
fmt.Println(err)
logrus.Error(err)
}

if !a.WorkerOff {
// stop the workers
fmt.Println("Shutting down worker")
logrus.Info("Shutting down worker")
err = a.Worker.Stop()
if err != nil {
fmt.Println(err)
logrus.Error(err)
}
}

err = server.Shutdown(ctx)
if err != nil {
fmt.Println(err)
logrus.Error(err)
}

}()
Expand Down Expand Up @@ -125,7 +126,7 @@ func (a *App) Serve() error {
func (a *App) Stop(err error) error {
a.cancel()
if err != nil && errors.Cause(err) != context.Canceled {
fmt.Println(err)
logrus.Error(err)
return err
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions buffalo/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -44,7 +43,7 @@ var xbuildCmd = &cobra.Command{
go func() {
<-ctx.Done()
if ctx.Err() == context.Canceled {
fmt.Println("~~~ BUILD CANCELLED ~~~")
logrus.Info("~~~ BUILD CANCELLED ~~~")
err := b.Cleanup()
if err != nil {
logrus.Fatal(err)
Expand All @@ -57,7 +56,7 @@ var xbuildCmd = &cobra.Command{
return errors.WithStack(err)
}

fmt.Printf("\nYou application was successfully built at %s\n", filepath.Join(b.Root, b.Bin))
logrus.Infof("\nYou application was successfully built at %s\n", filepath.Join(b.Root, b.Bin))

return nil
},
Expand Down
6 changes: 3 additions & 3 deletions buffalo/cmd/build/apkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (b *Builder) buildADatabase() error {
return errors.WithStack(err)
}
if !bytes.Contains(bb.Bytes(), []byte("sqlite")) {
logrus.Debugf("no sqlite usage in database.yml detected, applying the nosqlite tag")
logrus.Debug("no sqlite usage in database.yml detected, applying the nosqlite tag")
b.Tags = append(b.Tags, "nosqlite")
} else if !b.Static {
fmt.Println("you are building a SQLite application, please consider using the `--static` flag to compile a static binary")
logrus.Debug("you are building a SQLite application, please consider using the `--static` flag to compile a static binary")
}
} else {
logrus.Debugf("no database.yml detected, applying the nosqlite tag")
logrus.Debug("no database.yml detected, applying the nosqlite tag")
// add the nosqlite build tag if there is no database being used
b.Tags = append(b.Tags, "nosqlite")
}
Expand Down
2 changes: 1 addition & 1 deletion buffalo/cmd/build/archived_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
)

func (b *Builder) buildExtractedAssets() error {
Expand Down
2 changes: 1 addition & 1 deletion buffalo/cmd/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"os"

"github.com/sirupsen/logrus"
"github.com/gobuffalo/packr/builder"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Builder builds a Buffalo binary
Expand Down
2 changes: 1 addition & 1 deletion buffalo/cmd/build/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os/exec"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
)

type debugWriter int
Expand Down
2 changes: 1 addition & 1 deletion buffalo/cmd/build/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
)

func (b *Builder) transform(path string, fn func([]byte, io.Writer) error) error {
Expand Down
2 changes: 1 addition & 1 deletion buffalo/cmd/build/transform_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/gobuffalo/plush"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func (b *Builder) transformMain() error {
Expand Down
33 changes: 20 additions & 13 deletions buffalo/cmd/destroy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/markbates/inflect"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -31,7 +32,11 @@ var ResourceCmd = &cobra.Command{
fileName := inflect.Pluralize(inflect.Underscore(name))

removeTemplates(fileName)
removeActions(fileName)
err := removeActions(fileName)
if err != nil {
return err
}

removeLocales(fileName)
removeModel(name)
removeMigrations(fileName)
Expand All @@ -51,36 +56,38 @@ func confirm(msg string) bool {
func removeTemplates(fileName string) {
if YesToAll || confirm("Want to remove templates? (Y/n)") {
templatesFolder := fmt.Sprintf(filepath.Join("templates", fileName))
fmt.Printf("- Deleted %v folder\n", templatesFolder)
logrus.Infof("- Deleted %v folder\n", templatesFolder)
os.RemoveAll(templatesFolder)
}
}

func removeActions(fileName string) {
func removeActions(fileName string) error {
if YesToAll || confirm("Want to remove actions? (Y/n)") {
fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v.go", fileName))
logrus.Infof("- Deleted %v\n", fmt.Sprintf("actions/%v.go", fileName))
os.Remove(filepath.Join("actions", fmt.Sprintf("%v.go", fileName)))

fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v_test.go", fileName))
logrus.Infof("- Deleted %v\n", fmt.Sprintf("actions/%v_test.go", fileName))
os.Remove(filepath.Join("actions", fmt.Sprintf("%v_test.go", fileName)))

content, err := ioutil.ReadFile(filepath.Join("actions", "app.go"))
if err != nil {
fmt.Println("[WARNING] error reading app.go content")
return
logrus.Warn("error reading app.go content")
return err
}

resourceExpression := fmt.Sprintf("app.Resource(\"/%v\", %vResource{})", fileName, inflect.Camelize(fileName))
newContents := strings.Replace(string(content), resourceExpression, "", -1)

err = ioutil.WriteFile(filepath.Join("actions", "app.go"), []byte(newContents), 0)
if err != nil {
fmt.Println("[WARNING] error writing new app.go content")
return
logrus.Error("error writing new app.go content")
return err
}

fmt.Printf("- Deleted References for %v in actions/app.go\n", fileName)
logrus.Infof("- Deleted References for %v in actions/app.go\n", fileName)
}

return nil
}

func removeLocales(fileName string) {
Expand All @@ -96,8 +103,8 @@ func removeModel(name string) {
os.Remove(filepath.Join("models", fmt.Sprintf("%v.go", modelFileName)))
os.Remove(filepath.Join("models", fmt.Sprintf("%v_test.go", modelFileName)))

fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v.go", modelFileName))
fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v_test.go", modelFileName))
logrus.Infof("- Deleted %v\n", fmt.Sprintf("models/%v.go", modelFileName))
logrus.Infof("- Deleted %v\n", fmt.Sprintf("models/%v_test.go", modelFileName))
}
}

Expand All @@ -116,7 +123,7 @@ func removeMatch(folder, pattern string) {
if !f.IsDir() && matches {
path := filepath.Join(folder, f.Name())
os.Remove(path)
fmt.Printf("- Deleted %v\n", path)
logrus.Infof("- Deleted %v\n", path)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"os"
"os/exec"
"runtime"
Expand All @@ -12,6 +11,7 @@ import (
rg "github.com/gobuffalo/buffalo/generators/refresh"
"github.com/markbates/refresh/refresh"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ This behavior can be changed in your .buffalo.dev.yml file.`,
cause = err.Error()
}
}
fmt.Printf(msg, cause)
logrus.Errorf(msg, cause)
}()
os.Setenv("GO_ENV", "development")

Expand Down
12 changes: 6 additions & 6 deletions buffalo/cmd/new.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"

"github.com/pkg/errors"

"github.com/gobuffalo/buffalo/generators/newapp"
Expand Down Expand Up @@ -67,10 +68,9 @@ var newCmd = &cobra.Command{
return errors.WithStack(err)
}

fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
fmt.Println("You can find your new application at:")
fmt.Println(app.Root)
fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.")
logrus.Infof("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
logrus.Infof("You can find your new application at:\n%v", app.Root)
logrus.Info("\nPlease read the README.md file in your new application for next steps on running your application.")

return nil
},
Expand Down Expand Up @@ -108,7 +108,7 @@ func notInGoPath(ag newapp.Generator) error {
if err != nil {
return err
}
fmt.Println(t)
logrus.Error(t)
os.Exit(-1)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cmd

import (
"fmt"
"log"
"os"
"os/exec"
"sync"

"github.com/gobuffalo/buffalo/plugins"
"github.com/gobuffalo/envy"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +23,7 @@ func plugs() plugins.List {
_plugs, err = plugins.Available()
if err != nil {
_plugs = plugins.List{}
log.Printf("error loading plugins %s\n", err)
logrus.Errorf("error loading plugins %s\n", err)
}
}
return _plugs
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"errors"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ var RootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Printf("Error: %s\n\n", err)
logrus.Errorf("Error: %s\n\n", err)
os.Exit(-1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/gobuffalo/envy"
"github.com/markbates/deplist"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -214,7 +214,7 @@ func nodeCheck() error {
}

func run(cmd *exec.Cmd) error {
fmt.Printf("--> %s\n", strings.Join(cmd.Args, " "))
logrus.Infof("--> %s\n", strings.Join(cmd.Args, " "))
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down
Loading

0 comments on commit ec4fab3

Please sign in to comment.