This is an automated email from the ASF dual-hosted git repository.

liuhan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-rover.git


The following commit(s) were added to refs/heads/main by this push:
     new 7534c73  Let the logger as a configurable module (#41)
7534c73 is described below

commit 7534c73a91bf05d73e80bd862c13cda143c2bcee
Author: mrproliu <[email protected]>
AuthorDate: Mon Jul 25 19:22:50 2022 +0800

    Let the logger as a configurable module (#41)
---
 CHANGES.md                                 |  1 +
 configs/rover_configs.yaml                 |  4 +++
 docs/en/setup/configuration/logger.md      |  9 +++++++
 docs/en/setup/overview.md                  |  1 +
 docs/menu.yml                              |  2 ++
 internal/cmd/start.go                      |  7 -----
 pkg/boot/module.go                         |  7 +++++
 pkg/boot/register.go                       |  2 ++
 pkg/logger/logger.go                       | 17 ------------
 pkg/{boot/register.go => logger/module.go} | 43 +++++++++++++++++++++++-------
 pkg/logger/settings.go                     | 13 +++++++++
 11 files changed, 73 insertions(+), 33 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index e4e1e34..5f025b4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ Release Notes.
 ------------------
 #### Features
 * Support `NETWORK` Profiling.
+* Let the logger as a configurable module.
 
 #### Bug Fixes
 
diff --git a/configs/rover_configs.yaml b/configs/rover_configs.yaml
index 7a990ae..339b9d5 100644
--- a/configs/rover_configs.yaml
+++ b/configs/rover_configs.yaml
@@ -15,6 +15,10 @@
 # limitations under the License.
 #
 
+logger:
+  # The lowest level of printing allowed.
+  level: ${ROVER_LOGGER_LEVEL:INFO}
+
 core:
   backend:
     # The backend server address
diff --git a/docs/en/setup/configuration/logger.md 
b/docs/en/setup/configuration/logger.md
new file mode 100644
index 0000000..0b9f26e
--- /dev/null
+++ b/docs/en/setup/configuration/logger.md
@@ -0,0 +1,9 @@
+# Logger Module
+
+Logger module is used to configure the system log.
+
+## Configuration
+
+| Name | Default | Environment Key | Description |
+|------|---------|-----------------|-------------|
+| logger.level | INFO | ROVER_LOGGER_LEVEL | The lowest level of printing 
allowed. |
\ No newline at end of file
diff --git a/docs/en/setup/overview.md b/docs/en/setup/overview.md
index 5f15dc0..26b78c2 100644
--- a/docs/en/setup/overview.md
+++ b/docs/en/setup/overview.md
@@ -42,6 +42,7 @@ You can quickly build your Rover according to the following 
examples:
 The core concept behind this setting file is, that SkyWalking Rover is based 
on pure modularization design. The end-user can switch or assemble the 
collector features to their requirements.
 
 So, in rover_configs.yaml, there contains these parts.
+1. [Logger Module](./configuration/logger.md).
 1. [Core Module](./configuration/core.md).
 2. [Process Discovery Module](./configuration/process_discovery/overview.md).
 3. [Profiling Module](./configuration/profiling.md).
diff --git a/docs/menu.yml b/docs/menu.yml
index 86d3832..719b1ec 100644
--- a/docs/menu.yml
+++ b/docs/menu.yml
@@ -35,6 +35,8 @@ catalog:
           path: /en/setup/overview
         - name: Configuration
           catalog:
+            - name: Logger Module
+              path: /en/setup/configuration/logger
             - name: Core Module
               path: /en/setup/configuration/core
             - name: Process Discovery Module
diff --git a/internal/cmd/start.go b/internal/cmd/start.go
index 1c5ae70..49f2b06 100644
--- a/internal/cmd/start.go
+++ b/internal/cmd/start.go
@@ -23,19 +23,13 @@ import (
        "github.com/spf13/cobra"
 
        "github.com/apache/skywalking-rover/pkg/boot"
-       "github.com/apache/skywalking-rover/pkg/logger"
 )
 
 func newStartCmd() *cobra.Command {
-       logConfig := &logger.Config{}
        configPath := ""
        cmd := &cobra.Command{
                Use:   "start",
                Short: "start the rover",
-               PersistentPostRunE: func(cmd *cobra.Command, args []string) 
error {
-                       // setup logger
-                       return logger.SetupLogger(logConfig)
-               },
                RunE: func(cmd *cobra.Command, args []string) error {
                        ctx := context.Background()
 
@@ -44,7 +38,6 @@ func newStartCmd() *cobra.Command {
                },
        }
 
-       cmd.Flags().StringVarP(&logConfig.Level, "verbosity", "v", "info", "the 
level of logger")
        cmd.Flags().StringVarP(&configPath, "config", "c", 
"configs/rover_configs.yaml", "the rover config file path")
        return cmd
 }
diff --git a/pkg/boot/module.go b/pkg/boot/module.go
index a66e4ed..ade378a 100644
--- a/pkg/boot/module.go
+++ b/pkg/boot/module.go
@@ -22,9 +22,11 @@ import (
        "fmt"
        "os"
        "os/signal"
+       "sort"
        "sync"
        "syscall"
 
+       "github.com/apache/skywalking-rover/pkg/logger"
        "github.com/apache/skywalking-rover/pkg/module"
 )
 
@@ -119,6 +121,11 @@ func (m *ModuleStarter) Run(ctx context.Context) error {
 }
 
 func (m *ModuleStarter) ResolveDependency() error {
+       // make the log module as first active module
+       sort.Slice(m.activeModules, func(i, j int) bool {
+               return m.activeModules[i].Name() == logger.ModuleName
+       })
+
        // check has required module is not include
        for _, module := range m.activeModules {
                for _, reqModule := range module.RequiredModules() {
diff --git a/pkg/boot/register.go b/pkg/boot/register.go
index 10e161d..8f0d64d 100644
--- a/pkg/boot/register.go
+++ b/pkg/boot/register.go
@@ -19,6 +19,7 @@ package boot
 
 import (
        "github.com/apache/skywalking-rover/pkg/core"
+       "github.com/apache/skywalking-rover/pkg/logger"
        "github.com/apache/skywalking-rover/pkg/module"
        "github.com/apache/skywalking-rover/pkg/process"
        "github.com/apache/skywalking-rover/pkg/profiling"
@@ -26,6 +27,7 @@ import (
 
 func init() {
        // register all active module
+       module.Register(logger.NewModule())
        module.Register(core.NewModule())
        module.Register(process.NewModule())
        module.Register(profiling.NewModule())
diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go
index b3d308f..018c5c4 100644
--- a/pkg/logger/logger.go
+++ b/pkg/logger/logger.go
@@ -19,36 +19,19 @@ package logger
 
 import (
        "strings"
-       "sync"
 
        "github.com/sirupsen/logrus"
 )
 
 var (
        root = initializeDefaultLogger()
-       once sync.Once
 )
 
-type Config struct {
-       Level string
-}
-
 type Logger struct {
        *logrus.Entry
        module []string
 }
 
-// SetupLogger when Bootstrap
-func SetupLogger(config *Config) (err error) {
-       once.Do(func() {
-               err = updateLogger(root, config)
-       })
-       if err != nil {
-               return err
-       }
-       return nil
-}
-
 // GetLogger for the module
 func GetLogger(modules ...string) *Logger {
        moduleString := ""
diff --git a/pkg/boot/register.go b/pkg/logger/module.go
similarity index 59%
copy from pkg/boot/register.go
copy to pkg/logger/module.go
index 10e161d..79b27ea 100644
--- a/pkg/boot/register.go
+++ b/pkg/logger/module.go
@@ -15,18 +15,43 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package boot
+package logger
 
 import (
-       "github.com/apache/skywalking-rover/pkg/core"
+       "context"
+
        "github.com/apache/skywalking-rover/pkg/module"
-       "github.com/apache/skywalking-rover/pkg/process"
-       "github.com/apache/skywalking-rover/pkg/profiling"
 )
 
-func init() {
-       // register all active module
-       module.Register(core.NewModule())
-       module.Register(process.NewModule())
-       module.Register(profiling.NewModule())
+const ModuleName = "logger"
+
+type Module struct {
+       config *Config
+}
+
+func NewModule() *Module {
+       return &Module{config: &Config{}}
+}
+
+func (m *Module) Name() string {
+       return ModuleName
+}
+
+func (m *Module) RequiredModules() []string {
+       return []string{}
+}
+
+func (m *Module) Config() module.ConfigInterface {
+       return m.config
+}
+
+func (m *Module) Start(ctx context.Context, mgr *module.Manager) error {
+       return setupLogger(m.config)
+}
+
+func (m *Module) NotifyStartSuccess() {
+}
+
+func (m *Module) Shutdown(ctx context.Context, mgr *module.Manager) error {
+       return nil
 }
diff --git a/pkg/logger/settings.go b/pkg/logger/settings.go
index fd44b88..a56c6ab 100644
--- a/pkg/logger/settings.go
+++ b/pkg/logger/settings.go
@@ -23,6 +23,15 @@ const (
        DefaultLoggerLevel = logrus.InfoLevel
 )
 
+type Config struct {
+       Level string `mapstructure:"level"`
+}
+
+// setupLogger when Bootstrap
+func setupLogger(config *Config) (err error) {
+       return updateLogger(root, config)
+}
+
 func updateLogger(log *logrus.Logger, config *Config) error {
        level, err := logrus.ParseLevel(config.Level)
        if err != nil {
@@ -41,3 +50,7 @@ func initializeDefaultLogger() *logrus.Logger {
        })
        return l
 }
+
+func (c *Config) IsActive() bool {
+       return true
+}

Reply via email to