Skip to content

Commit 568c6ce

Browse files
author
Vipul Rawat
authored
fix CMD application unnecessary logs (#340)
1 parent a3bac91 commit 568c6ce

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

pkg/gofr/container/container.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,35 @@ type Container struct {
3636
SQL *sql.DB
3737
}
3838

39+
func NewEmptyContainer() *Container {
40+
return &Container{}
41+
}
42+
3943
func NewContainer(conf config.Config) *Container {
4044
c := &Container{
41-
Logger: logging.NewRemoteLogger(logging.GetLevelFromString(conf.Get("LOG_LEVEL")), conf.Get("REMOTE_LOG_URL"),
42-
conf.GetOrDefault("REMOTE_LOG_FETCH_INTERVAL", "15")),
4345
appName: conf.GetOrDefault("APP_NAME", "gofr-app"),
4446
appVersion: conf.GetOrDefault("APP_VERSION", "dev"),
4547
}
4648

49+
c.Create(conf)
50+
51+
return c
52+
}
53+
54+
func (c *Container) Create(conf config.Config) {
55+
if c.appName != "" {
56+
c.appName = conf.GetOrDefault("APP_NAME", "gofr-app")
57+
}
58+
59+
if c.appVersion != "" {
60+
c.appVersion = conf.GetOrDefault("APP_VERSION", "dev")
61+
}
62+
63+
if c.Logger == nil {
64+
c.Logger = logging.NewRemoteLogger(logging.GetLevelFromString(conf.Get("LOG_LEVEL")), conf.Get("REMOTE_LOG_URL"),
65+
conf.GetOrDefault("REMOTE_LOG_FETCH_INTERVAL", "15"))
66+
}
67+
4768
c.Debug("Container is being created")
4869

4970
c.metricsManager = metrics.NewMetricsManager(exporters.Prometheus(c.appName, c.appVersion), c.Logger)
@@ -74,8 +95,6 @@ func NewContainer(conf config.Config) *Container {
7495
SubscriptionName: conf.Get("GOOGLE_SUBSCRIPTION_NAME"),
7596
}, c.Logger)
7697
}
77-
78-
return c
7998
}
8099

81100
// GetHTTPService returns registered http services.

pkg/gofr/gofr.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ func NewCMD() *App {
9191
app := &App{}
9292
app.readConfig()
9393

94-
app.container = container.NewContainer(app.Config)
94+
app.container = container.NewEmptyContainer()
95+
app.container.Logger = logging.NewFileLogger(app.Config.Get("CMD_LOGS_FILE"))
9596
app.cmd = &cmd{}
96-
// app.container.Logger = logging.NewSilentLogger() // TODO - figure out a proper way to log in CMD
9797

98+
app.container.Create(app.Config)
9899
app.initTracer()
99100

100101
return app

pkg/gofr/gofr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ func Test_addRoute(t *testing.T) {
205205
a := NewCMD()
206206

207207
a.SubCommand("log", func(c *Context) (interface{}, error) {
208-
c.Logger.Info("handler called")
208+
c.Logger.Info("logging in handler")
209209

210-
return nil, nil
210+
return "handler called", nil
211211
})
212212

213213
a.Run()

pkg/gofr/logging/logger.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"gofr.dev/pkg/gofr/service"
1616
)
1717

18+
const fileMode = 0644
19+
1820
type Logger interface {
1921
Debug(args ...interface{})
2022
Debugf(format string, args ...interface{})
@@ -205,12 +207,24 @@ func NewLogger(level Level) Logger {
205207
}
206208

207209
// TODO - Do we need this? Only used for CMD log silencing.
208-
func NewSilentLogger() Logger {
210+
func NewFileLogger(path string) Logger {
209211
l := &logger{
210212
normalOut: io.Discard,
211213
errorOut: io.Discard,
212214
}
213215

216+
if path != "" {
217+
return l
218+
}
219+
220+
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, fileMode)
221+
if err != nil {
222+
return l
223+
}
224+
225+
l.normalOut = f
226+
l.errorOut = f
227+
214228
return l
215229
}
216230

pkg/gofr/logging/logger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func TestPrettyPrint_ServiceAndDefaultLogs(t *testing.T) {
337337

338338
func Test_NewSilentLoggerSTDOutput(t *testing.T) {
339339
logs := testutil.StdoutOutputForFunc(func() {
340-
l := NewSilentLogger()
340+
l := NewFileLogger("")
341341

342342
l.Info("Info Logs")
343343
l.Debug("Debug Logs")

0 commit comments

Comments
 (0)