diff options
author | Valery Piashchynski <[email protected]> | 2021-02-16 18:02:37 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-16 18:02:37 +0300 |
commit | bb9dd8d3f46da089217e61efc3f058cfbba5ede3 (patch) | |
tree | e942aeb03e0f1bc862c0d726160f886ccc9b65d0 | |
parent | 69622100a1c95656f977638f75d8cea81afc0d4a (diff) | |
parent | 201befde78cdefad40261f223b8a086df496cf07 (diff) |
Merge pull request #546 from spiral/logs_config_optional
feat(logger): Logger plugin now optional (config)
-rwxr-xr-x | .rr.yaml | 8 | ||||
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rwxr-xr-x | pkg/pool/static_pool_test.go | 2 | ||||
-rw-r--r-- | plugins/logger/config.go | 12 | ||||
-rw-r--r-- | plugins/logger/plugin.go | 14 | ||||
-rw-r--r-- | tests/plugins/logger/configs/.rr-no-logger.yaml | 0 | ||||
-rw-r--r-- | tests/plugins/logger/configs/.rr-no-logger2.yaml | 21 | ||||
-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.go | 125 |
9 files changed, 175 insertions, 8 deletions
@@ -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/CHANGELOG.md b/CHANGELOG.md index 1f82a8f1..ff21738b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG vNext ------------------- - Add support for the overwriting `.rr.yaml` keys with values (ref: https://roadrunner.dev/docs/intro-config) +- Make logger plugin optional to define in the config. Default values: level -> `debug`, mode -> `development` v2.0.0-RC.2 (11.02.2021) ------------------- diff --git a/pkg/pool/static_pool_test.go b/pkg/pool/static_pool_test.go index 44f5936c..c8b3616c 100755 --- a/pkg/pool/static_pool_test.go +++ b/pkg/pool/static_pool_test.go @@ -437,7 +437,7 @@ func Test_Static_Pool_Destroy_And_Close_While_Wait(t *testing.T) { t.Errorf("error executing payload: error %v", err) } }() - time.Sleep(time.Millisecond * 10) + time.Sleep(time.Millisecond * 100) p.Destroy(ctx) _, err = p.Exec(payload.Payload{Body: []byte("100")}) 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..7f378026 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.ErrorLevel)) + 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.ErrorLevel)) 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 { |