summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-02-16 12:53:34 +0300
committerValery Piashchynski <[email protected]>2021-02-16 12:53:34 +0300
commitac159557f3a31e53e0c76c4196b0e42e1e71aa52 (patch)
tree3e26d5fa6f022d1cebd2da7bc9ccb832009a29f5
parent69622100a1c95656f977638f75d8cea81afc0d4a (diff)
Update logger plugin. Now it's optional
Signed-off-by: Valery Piashchynski <[email protected]>
-rwxr-xr-x.rr.yaml8
-rw-r--r--plugins/logger/config.go12
-rw-r--r--plugins/logger/plugin.go14
-rw-r--r--tests/plugins/logger/configs/.rr-no-logger.yaml0
-rw-r--r--tests/plugins/logger/configs/.rr-no-logger2.yaml21
-rw-r--r--tests/plugins/logger/configs/.rr.yaml (renamed from tests/plugins/logger/.rr.yaml)0
-rw-r--r--tests/plugins/logger/logger_test.go125
7 files changed, 173 insertions, 7 deletions
diff --git a/.rr.yaml b/.rr.yaml
index 3f41c126..adb36c46 100755
--- a/.rr.yaml
+++ b/.rr.yaml
@@ -7,13 +7,21 @@ rpc:
server:
command: "php tests/psr-worker-bench.php"
+ # optional
user: ""
+ # optional
group: ""
+ env:
+ - SOME_KEY: "SOME_VALUE"
+ - SOME_KEY2: "SOME_VALUE2"
relay: "pipes"
relay_timeout: 20s
+# optional for development
logs:
+ # default - development
mode: development
+ # default - debug
level: error
# Workflow and activity mesh service
diff --git a/plugins/logger/config.go b/plugins/logger/config.go
index bf7343c7..52594bc4 100644
--- a/plugins/logger/config.go
+++ b/plugins/logger/config.go
@@ -88,7 +88,17 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) {
zCfg.ErrorOutputPaths = cfg.ErrorOutput
}
- // todo: https://github.com/uber-go/zap/blob/master/FAQ.md#does-zap-support-log-rotation
+ // todo:
return zCfg.Build()
}
+
+// Initialize default logger
+func (cfg *Config) InitDefault() {
+ if cfg.Mode == "" {
+ cfg.Mode = "development"
+ }
+ if cfg.Level == "" {
+ cfg.Level = "debug"
+ }
+}
diff --git a/plugins/logger/plugin.go b/plugins/logger/plugin.go
index 7fc464b6..08fc2454 100644
--- a/plugins/logger/plugin.go
+++ b/plugins/logger/plugin.go
@@ -20,11 +20,21 @@ type ZapLogger struct {
// Init logger service.
func (z *ZapLogger) Init(cfg config.Configurer) error {
const op = errors.Op("config_plugin_init")
+ var err error
+ // if not configured, configure with default params
if !cfg.Has(PluginName) {
- return errors.E(op, errors.Disabled)
+ z.cfg = &Config{}
+ z.cfg.InitDefault()
+
+ z.base, err = z.cfg.BuildLogger()
+ if err != nil {
+ return errors.E(op, errors.Disabled, err)
+ }
+
+ return nil
}
- err := cfg.UnmarshalKey(PluginName, &z.cfg)
+ err = cfg.UnmarshalKey(PluginName, &z.cfg)
if err != nil {
return errors.E(op, errors.Disabled, err)
}
diff --git a/tests/plugins/logger/configs/.rr-no-logger.yaml b/tests/plugins/logger/configs/.rr-no-logger.yaml
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/plugins/logger/configs/.rr-no-logger.yaml
diff --git a/tests/plugins/logger/configs/.rr-no-logger2.yaml b/tests/plugins/logger/configs/.rr-no-logger2.yaml
new file mode 100644
index 00000000..1211b7ad
--- /dev/null
+++ b/tests/plugins/logger/configs/.rr-no-logger2.yaml
@@ -0,0 +1,21 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../http/client.php echo pipes"
+ user: ""
+ group: ""
+ env:
+ "RR_HTTP": "true"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+http:
+ debug: true
+ address: 127.0.0.1:18945
+ max_request_size: 1024
+ pool:
+ num_workers: 2
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s \ No newline at end of file
diff --git a/tests/plugins/logger/.rr.yaml b/tests/plugins/logger/configs/.rr.yaml
index 5ab359d3..5ab359d3 100644
--- a/tests/plugins/logger/.rr.yaml
+++ b/tests/plugins/logger/configs/.rr.yaml
diff --git a/tests/plugins/logger/logger_test.go b/tests/plugins/logger/logger_test.go
index 63f233ee..5be76b7a 100644
--- a/tests/plugins/logger/logger_test.go
+++ b/tests/plugins/logger/logger_test.go
@@ -8,7 +8,10 @@ import (
endure "github.com/spiral/endure/pkg/container"
"github.com/spiral/roadrunner/v2/plugins/config"
+ "github.com/spiral/roadrunner/v2/plugins/http"
"github.com/spiral/roadrunner/v2/plugins/logger"
+ "github.com/spiral/roadrunner/v2/plugins/rpc"
+ "github.com/spiral/roadrunner/v2/plugins/server"
"github.com/stretchr/testify/assert"
)
@@ -19,22 +22,136 @@ func TestLogger(t *testing.T) {
}
// config plugin
vp := &config.Viper{}
- vp.Path = ".rr.yaml"
+ vp.Path = "configs/.rr.yaml"
vp.Prefix = "rr"
- err = container.Register(vp)
+
+ err = container.RegisterAll(
+ vp,
+ &Plugin{},
+ &logger.ZapLogger{},
+ )
+ assert.NoError(t, err)
+
+ err = container.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ errCh, err := container.Serve()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // stop by CTRL+C
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+
+ stopCh := make(chan struct{}, 1)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
+
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case e := <-errCh:
+ assert.NoError(t, e.Error)
+ assert.NoError(t, container.Stop())
+ return
+ case <-c:
+ err = container.Stop()
+ assert.NoError(t, err)
+ return
+ case <-stopCh:
+ assert.NoError(t, container.Stop())
+ return
+ }
+ }
+ }()
+
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+func TestLoggerNoConfig(t *testing.T) {
+ container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel))
+ if err != nil {
+ t.Fatal(err)
+ }
+ // config plugin
+ vp := &config.Viper{}
+ vp.Path = "configs/.rr-no-logger.yaml"
+ vp.Prefix = "rr"
+
+ err = container.RegisterAll(
+ vp,
+ &Plugin{},
+ &logger.ZapLogger{},
+ )
+ assert.NoError(t, err)
+
+ err = container.Init()
if err != nil {
t.Fatal(err)
}
- err = container.Register(&Plugin{})
+ errCh, err := container.Serve()
if err != nil {
t.Fatal(err)
}
- err = container.Register(&logger.ZapLogger{})
+ // stop by CTRL+C
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+
+ stopCh := make(chan struct{}, 1)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
+
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case e := <-errCh:
+ assert.NoError(t, e.Error)
+ assert.NoError(t, container.Stop())
+ return
+ case <-c:
+ err = container.Stop()
+ assert.NoError(t, err)
+ return
+ case <-stopCh:
+ assert.NoError(t, container.Stop())
+ return
+ }
+ }
+ }()
+
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+// Should no panic
+func TestLoggerNoConfig2(t *testing.T) {
+ container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel))
if err != nil {
t.Fatal(err)
}
+ // config plugin
+ vp := &config.Viper{}
+ vp.Path = "configs/.rr-no-logger2.yaml"
+ vp.Prefix = "rr"
+
+ err = container.RegisterAll(
+ vp,
+ &rpc.Plugin{},
+ &logger.ZapLogger{},
+ &http.Plugin{},
+ &server.Plugin{},
+ )
+ assert.NoError(t, err)
err = container.Init()
if err != nil {