Skip to content

Commit

Permalink
add log format
Browse files Browse the repository at this point in the history
  • Loading branch information
DrmagicE committed Dec 15, 2020
1 parent fa94dc9 commit 1d728ee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
7 changes: 0 additions & 7 deletions cmd/gmqttd/command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,11 @@ func NewStartCmd() *cobra.Command {
if useDefault {
l.Warn("config file not exist, use default configration")
}
hook := server.Hooks{
OnBasicAuth: func(ctx context.Context, client server.Client, req *server.ConnectRequest) (err error) {
fmt.Println(req.Options.SessionExpiry)
return nil
},
}
s := server.New(
server.WithConfig(c),
server.WithTCPListener(tcpListeners...),
server.WithWebsocketServer(websockets...),
server.WithLogger(l),
server.WithHook(hook),
)
err = s.Init()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/gmqttd/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ plugins:
addr: 8084
log:
level: info # debug | info | warn | error
format: text # json | text



Expand Down
51 changes: 39 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"fmt"
"io/ioutil"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -22,23 +24,27 @@ type Configuration interface {
yaml.Unmarshaler
}

// RegisterDefaultPluginConfig registers the default configuration for the given plugin name.
// RegisterDefaultPluginConfig registers the default configuration for the given plugin.
func RegisterDefaultPluginConfig(name string, config Configuration) {
if _, ok := defaultPluginConfig[name]; ok {
panic(fmt.Sprintf("duplicated default config for %s plugin", name))
}
defaultPluginConfig[name] = config

}

// DefaultConfig return the default configuration.
// If config file is not provided, gmqttd will start with DefaultConfig.
// Command-line flags will override the configuration.
func DefaultConfig() Config {
c := Config{
Listeners: DefaultListeners,
MQTT: DefaultMQTTConfig,
Log: LogConfig{
Level: "info",
Level: "info",
Format: "text",
},
PidFile: getDefaultPidFile(),
Plugins: make(PluginConfig),
Plugins: make(pluginConfig),
Persistence: DefaultPersistenceConfig,
TopicAliasManager: DefaultTopicAliasManager,
}
Expand All @@ -65,12 +71,27 @@ var DefaultListeners = []*ListenerConfig{

// LogConfig is use to configure the log behaviors.
type LogConfig struct {
// Level is the log level. Possible values: debug, info, warn, error
Level string
// Format is the log format. Possible values: json, text
Format string
}

func (l LogConfig) Validate() error {
if l.Level != "debug" && l.Level != "info" && l.Level != "warn" && l.Level != "error" {
return fmt.Errorf("invalid log level: %s", l.Level)
}
if l.Format != "json" && l.Format != "text" {
return fmt.Errorf("invalid log format: %s", l.Format)
}
return nil
}

type PluginConfig map[string]Configuration
// pluginConfig stores the plugin default configuration, key by the plugin name.
// If the plugin has default configuration, it should call RegisterDefaultPluginConfig in it's init function to register.
type pluginConfig map[string]Configuration

func (p PluginConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (p pluginConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
for _, v := range p {
err := unmarshal(v)
if err != nil {
Expand All @@ -86,7 +107,7 @@ type Config struct {
MQTT MQTT `yaml:"mqtt,omitempty"`
Log LogConfig `yaml:"log"`
PidFile string `yaml:"pid_file"`
Plugins PluginConfig `yaml:"plugins"`
Plugins pluginConfig `yaml:"plugins"`
Persistence Persistence `yaml:"persistence"`
TopicAliasManager TopicAliasManager `yaml:"topic_alias_manager"`
}
Expand Down Expand Up @@ -116,7 +137,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
raw.MQTT = DefaultMQTTConfig
}
if len(raw.Plugins) == 0 {
raw.Plugins = make(PluginConfig)
raw.Plugins = make(pluginConfig)
for name, v := range defaultPluginConfig {
raw.Plugins[name] = v
}
Expand All @@ -140,6 +161,7 @@ func (c Config) Validate() error {
if err != nil {
return err
}

for _, conf := range c.Plugins {
err := conf.Validate()
if err != nil {
Expand Down Expand Up @@ -175,9 +197,14 @@ func (c Config) GetLogger(config LogConfig) (l *zap.Logger, err error) {
if err != nil {
return
}
var core zapcore.Core
if config.Format == "json" {
core = zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), os.Stdout, logLevel)
}
if config.Format == "text" {
core = zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), os.Stdout, logLevel)
}

lc := zap.NewDevelopmentConfig()
lc.Level = zap.NewAtomicLevelAt(logLevel)
l, err = lc.Build()
return l, nil
zaplog := zap.New(core, zap.AddStacktrace(zap.ErrorLevel), zap.AddCaller())
return zaplog, nil
}
1 change: 0 additions & 1 deletion server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ func (client *client) connectWithTimeOut() (ok bool) {
Code: codeErr.Code,
Properties: getErrorProperties(client, &codeErr.ErrorDetails),
}
fmt.Println(getErrorProperties(client, &codeErr.ErrorDetails))
return
}
// authentication success
Expand Down

0 comments on commit 1d728ee

Please sign in to comment.