Skip to content

Commit

Permalink
feat: dont print log when running commands unless error (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Dec 31, 2024
1 parent a8c5084 commit b202f13
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions console/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/env"
)

type Application struct {
Expand All @@ -17,19 +18,18 @@ type Application struct {

// NewApplication Create a new Artisan application.
// The artisan parameter is used by goravel/installer.
func NewApplication(name, usage, usageText, version string, artisan ...bool) console.Artisan {
func NewApplication(name, usage, usageText, version string) console.Artisan {
instance := cli.NewApp()
instance.Name = name
instance.Usage = usage
instance.UsageText = usageText
instance.Version = version
instance.CommandNotFound = commandNotFound
instance.OnUsageError = onUsageError
isArtisan := len(artisan) > 0 && artisan[0]

return &Application{
instance: instance,
isArtisan: isArtisan,
isArtisan: env.IsArtisan(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion console/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var testCommand = 0

func TestRun(t *testing.T) {
cliApp := NewApplication("test", "test", "test", "test", true)
cliApp := NewApplication("test", "test", "test", "test")
cliApp.Register([]console.Command{
&TestCommand{},
})
Expand Down
2 changes: 1 addition & 1 deletion console/cli_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
cliApp := NewApplication("test", "test", "test", "test", true)
cliApp := NewApplication("test", "test", "test", "test")
cliApp.Register([]console.Command{
&TestFooCommand{},
&TestBarCommand{},
Expand Down
2 changes: 1 addition & 1 deletion console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (receiver *ServiceProvider) Register(app foundation.Application) {
name := "artisan"
usage := "Goravel Framework"
usageText := "artisan [global options] command [options] [arguments...]"
return NewApplication(name, usage, usageText, app.Version(), true), nil
return NewApplication(name, usage, usageText, app.Version()), nil
})
}

Expand Down
4 changes: 4 additions & 0 deletions database/gorm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import (
"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/log"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/env"
)

func NewLogger(config config.Config, log log.Log) logger.Interface {
level := logger.Warn
if config.GetBool("app.debug") {
level = logger.Info
}
if env.IsArtisan() {
level = logger.Error
}

slowThreshold := config.GetInt("database.slow_threshold", 200)
if slowThreshold <= 0 {
Expand Down
10 changes: 10 additions & 0 deletions support/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func IsArm() bool {
return runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
}

func IsArtisan() bool {
for _, arg := range os.Args {
if arg == "artisan" {
return true
}
}

return false
}

// IsDarwin returns whether the current operating system is Darwin.
// IsDarwin 返回当前操作系统是否为 Darwin。
func IsDarwin() bool {
Expand Down

0 comments on commit b202f13

Please sign in to comment.