summaryrefslogtreecommitdiff
path: root/plugins/logger/zap_logger.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-10-28 16:05:39 +0300
committerGitHub <[email protected]>2020-10-28 16:05:39 +0300
commitde53c3ad12a8afb379610f87399373c4d0626ef6 (patch)
treed4ed0d2a879fc9515062b77fc54ec80e66cf0054 /plugins/logger/zap_logger.go
parent47a570c220a36ae7b770ea594a41637fa31fc8e8 (diff)
parent175c02e501a3b5110f8882599d5d033fde5bf81b (diff)
Merge pull request #378 from spiral/feature/loggingv2.0.0-alpha14
Feature/logging
Diffstat (limited to 'plugins/logger/zap_logger.go')
-rw-r--r--plugins/logger/zap_logger.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/plugins/logger/zap_logger.go b/plugins/logger/zap_logger.go
new file mode 100644
index 00000000..8c1739f2
--- /dev/null
+++ b/plugins/logger/zap_logger.go
@@ -0,0 +1,68 @@
+package logger
+
+import (
+ "github.com/spiral/endure"
+ "github.com/spiral/roadrunner/v2/plugins/config"
+ "go.uber.org/zap"
+)
+
+// ServiceName declares service name.
+const ServiceName = "logs"
+
+type LogFactory interface {
+ // GlobalLogger returns global log instance.
+ GlobalLogger() *zap.Logger
+
+ // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params.
+ NamedLogger(name string) *zap.Logger
+}
+
+// ZapLogger manages zap logger.
+type ZapLogger struct {
+ base *zap.Logger
+ cfg Config
+ channels ChannelConfig
+}
+
+// Init logger service.
+func (z *ZapLogger) Init(cfg config.Provider) (err error) {
+ err = cfg.UnmarshalKey(ServiceName, &z.cfg)
+ if err != nil {
+ return err
+ }
+
+ err = cfg.UnmarshalKey(ServiceName, &z.channels)
+ if err != nil {
+ return err
+ }
+
+ z.base, err = z.cfg.BuildLogger()
+ return err
+}
+
+// DefaultLogger returns default logger.
+func (z *ZapLogger) DefaultLogger() (*zap.Logger, error) {
+ return z.base, nil
+}
+
+// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params.
+func (z *ZapLogger) NamedLogger(name string) (*zap.Logger, error) {
+ if cfg, ok := z.channels.Channels[name]; ok {
+ return cfg.BuildLogger()
+ }
+
+ return z.base.Named(name), nil
+}
+
+// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params.
+func (z *ZapLogger) ServiceLogger(n endure.Named) (*zap.Logger, error) {
+ return z.NamedLogger(n.Name())
+}
+
+// Provides declares factory methods.
+func (z *ZapLogger) Provides() []interface{} {
+ return []interface{}{
+ z.DefaultLogger,
+ z.ServiceLogger,
+ }
+}