Skip to content

Commit f4c35ee

Browse files
committed
enable log level from env
1 parent bf6590a commit f4c35ee

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

cmd/cmd.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/chanmaoganda/fileshare/cmd/cache"
55
"github.com/chanmaoganda/fileshare/cmd/fileshare/client"
66
"github.com/chanmaoganda/fileshare/cmd/fileshare/server"
7-
"github.com/sirupsen/logrus"
87
"github.com/spf13/cobra"
98
)
109

@@ -17,11 +16,6 @@ var RootCmd = &cobra.Command{
1716
}
1817

1918
func init() {
20-
logrus.SetLevel(logrus.DebugLevel)
21-
logrus.SetFormatter(&logrus.TextFormatter{
22-
ForceColors: true,
23-
})
24-
2519
// server commands
2620
RootCmd.AddCommand(server.ServerCmd)
2721

internal/core/setup/setup.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/chanmaoganda/fileshare/internal/config"
7+
"github.com/chanmaoganda/fileshare/internal/pkg/logger"
78
"github.com/chanmaoganda/fileshare/internal/pkg/util"
89
"github.com/chanmaoganda/fileshare/internal/service"
910
"github.com/sirupsen/logrus"
@@ -16,6 +17,7 @@ func SetupClient(cmd *cobra.Command, args []string) error {
1617
logrus.Error(err)
1718
return err
1819
}
20+
logger.SetupLogger()
1921
service.InitClientService()
2022
// if directory cannot be set correctly, following actions will panic
2123
return setupDirectory()
@@ -27,6 +29,7 @@ func SetupServer(cmd *cobra.Command, args []string) error {
2729
logrus.Error(err)
2830
return err
2931
}
32+
logger.SetupLogger()
3033
service.InitServerService()
3134
// if directory cannot be set correctly, following actions will panic
3235
return setupDirectory()

internal/pkg/logger/logger.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package logger
2+
3+
import (
4+
"os"
5+
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
func SetupLogger() {
10+
levelString := os.Getenv("LOG")
11+
12+
level, err := logrus.ParseLevel(levelString)
13+
if err != nil {
14+
logrus.SetLevel(logrus.InfoLevel)
15+
}
16+
17+
logrus.SetLevel(level)
18+
logrus.SetFormatter(&logrus.TextFormatter{
19+
ForceColors: true,
20+
})
21+
}

0 commit comments

Comments
 (0)