diff --git a/config-logger-main.go b/config-logger-main.go index ef3da5339..cf639f2f3 100644 --- a/config-logger-main.go +++ b/config-logger-main.go @@ -17,7 +17,7 @@ package main import ( - "fmt" + "runtime" "github.com/minio/cli" "github.com/minio/minio-xl/pkg/probe" @@ -32,42 +32,31 @@ var configLoggerCmd = cli.Command{ minio config {{.Name}} - {{.Usage}} USAGE: - minio config {{.Name}} + minio config {{.Name}} OPERATION [ARGS...] -`, -} + OPERATION = add | list | remove -// Inherit at one place -type config struct { - *configV2 -} +EXAMPLES: + 1. Configure new mongo logger. + $ minio config {{.Name}} add mongo localhost:28710 mydb mylogger -func (c *config) IsFileLoggingEnabled() bool { - if c.FileLogger.Filename != "" { - return true - } - return false -} + 2. Configure new syslog logger. NOTE: syslog logger is not supported on windows. + $ minio config {{.Name}} add syslog localhost:554 udp -func (c *config) IsSysloggingEnabled() bool { - if c.SyslogLogger.Network != "" && c.SyslogLogger.Addr != "" { - return true - } - return false -} + 3. Configure new file logger. "/var/log" should be writable by user. + $ minio config {{.Name}} add file /var/log/minio.log -func (c *config) IsMongoLoggingEnabled() bool { - if c.MongoLogger.Addr != "" && c.MongoLogger.DB != "" && c.MongoLogger.Collection != "" { - return true - } - return false + 4. List currently configured logger. + $ minio config {{.Name}} list + + 5. Remove/Reset a configured logger. + $ minio config {{.Name}} remove mongo +`, } -func (c *config) String() string { - str := fmt.Sprintf("Mongo -> Addr: %s, DB: %s, Collection: %s\n", c.MongoLogger.Addr, c.MongoLogger.DB, c.MongoLogger.Collection) - str = str + fmt.Sprintf("Syslog -> Addr: %s, Network: %s\n", c.SyslogLogger.Addr, c.SyslogLogger.Network) - str = str + fmt.Sprintf("File -> Filename: %s", c.FileLogger.Filename) - return str +// Inherit at one place +type config struct { + *configV2 } func mainConfigLogger(ctx *cli.Context) { @@ -83,6 +72,9 @@ func mainConfigLogger(ctx *cli.Context) { enableLog2Mongo(&config{conf}, args.Tail()) } if args.Get(0) == "syslog" { + if runtime.GOOS == "windows" { + fatalIf(probe.NewError(errInvalidArgument), "Syslog is not supported on windows.", nil) + } enableLog2Syslog(&config{conf}, args.Tail()) } if args.Get(0) == "file" { @@ -99,6 +91,9 @@ func mainConfigLogger(ctx *cli.Context) { fatalIf(err.Trace(), "Unable to save config.", nil) } if args.Get(0) == "syslog" { + if runtime.GOOS == "windows" { + fatalIf(probe.NewError(errInvalidArgument), "Syslog is not supported on windows.", nil) + } conf.SyslogLogger.Network = "" conf.SyslogLogger.Addr = "" err := saveConfig(conf) @@ -111,7 +106,11 @@ func mainConfigLogger(ctx *cli.Context) { } } if ctx.Args().Get(0) == "list" { - Println(&config{conf}) + if globalJSONFlag { + Println(conf.JSON()) + return + } + Println(conf) } } diff --git a/logger-syslog-hook.go b/logger-syslog-hook_linux.go similarity index 100% rename from logger-syslog-hook.go rename to logger-syslog-hook_linux.go diff --git a/logger-syslog-hook_windows.go b/logger-syslog-hook_windows.go new file mode 100644 index 000000000..949f2d04b --- /dev/null +++ b/logger-syslog-hook_windows.go @@ -0,0 +1,25 @@ +// +build windows + +/* + * Minio Cloud Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import "github.com/minio/minio-xl/pkg/probe" + +func log2Syslog(network, raddr string) *probe.Error { + return probe.NewError(errSysLogNotSupported) +} diff --git a/server-config.go b/server-config.go index 7918a8bde..ec565fd3d 100644 --- a/server-config.go +++ b/server-config.go @@ -17,7 +17,9 @@ package main import ( + "encoding/json" "errors" + "fmt" "os" "os/user" "path/filepath" @@ -54,6 +56,58 @@ type configV2 struct { } `json:"fileLogger"` } +func (c *configV2) IsFileLoggingEnabled() bool { + if c.FileLogger.Filename != "" { + return true + } + return false +} + +func (c *configV2) IsSysloggingEnabled() bool { + if c.SyslogLogger.Network != "" && c.SyslogLogger.Addr != "" { + return true + } + return false +} + +func (c *configV2) IsMongoLoggingEnabled() bool { + if c.MongoLogger.Addr != "" && c.MongoLogger.DB != "" && c.MongoLogger.Collection != "" { + return true + } + return false +} + +func (c *configV2) String() string { + str := fmt.Sprintf("Mongo -> Addr: %s, DB: %s, Collection: %s\n", c.MongoLogger.Addr, c.MongoLogger.DB, c.MongoLogger.Collection) + str = str + fmt.Sprintf("Syslog -> Addr: %s, Network: %s\n", c.SyslogLogger.Addr, c.SyslogLogger.Network) + str = str + fmt.Sprintf("File -> Filename: %s", c.FileLogger.Filename) + return str +} + +func (c *configV2) JSON() string { + type logger struct { + MongoLogger struct { + Addr string `json:"addr"` + DB string `json:"db"` + Collection string `json:"collection"` + } `json:"mongoLogger"` + SyslogLogger struct { + Network string `json:"network"` + Addr string `json:"addr"` + } `json:"syslogLogger"` + FileLogger struct { + Filename string `json:"filename"` + } `json:"fileLogger"` + } + loggerBytes, err := json.Marshal(logger{ + MongoLogger: c.MongoLogger, + SyslogLogger: c.SyslogLogger, + FileLogger: c.FileLogger, + }) + fatalIf(probe.NewError(err), "Unable to marshal logger struct into JSON.", nil) + return string(loggerBytes) +} + // getConfigPath get users config path func getConfigPath() (string, *probe.Error) { if customConfigPath != "" { diff --git a/server-main.go b/server-main.go index 1ff26d031..431b76cff 100644 --- a/server-main.go +++ b/server-main.go @@ -168,8 +168,30 @@ func parsePercentToInt(s string, bitSize int) (int64, *probe.Error) { } return p, nil } +func setLogger(conf *configV2) *probe.Error { + if conf.IsMongoLoggingEnabled() { + err := log2Mongo(conf.MongoLogger.Addr, conf.MongoLogger.DB, conf.MongoLogger.Collection) + if err != nil { + return err.Trace() + } + } + if conf.IsSysloggingEnabled() { + err := log2Syslog(conf.SyslogLogger.Network, conf.SyslogLogger.Addr) + if err != nil { + return err.Trace() + } + } + if conf.IsFileLoggingEnabled() { + err := log2File(conf.FileLogger.Filename) + if err != nil { + return err.Trace() + } + } + return nil +} -func getAuth() (*configV2, *probe.Error) { +// Generates config if it doesn't exist, otherwise returns back the saved ones. +func getConfig() (*configV2, *probe.Error) { if err := createConfigPath(); err != nil { return nil, err.Trace() } @@ -208,12 +230,15 @@ func (a accessKeys) JSON() string { return string(b) } -// fetchAuth first time authorization -func fetchAuth() *probe.Error { - conf, err := getAuth() +// initServer initialize server +func initServer() *probe.Error { + conf, err := getConfig() if err != nil { return err.Trace() } + if err := setLogger(conf); err != nil { + return err.Trace() + } if conf != nil { if globalJSONFlag { Println(accessKeys{conf}.JSON()) @@ -257,8 +282,8 @@ func checkServerSyntax(c *cli.Context) { func serverMain(c *cli.Context) { checkServerSyntax(c) - perr := fetchAuth() - fatalIf(perr.Trace(), "Failed to generate keys for minio.", nil) + perr := initServer() + fatalIf(perr.Trace(), "Failed to read config for minio.", nil) certFile := c.GlobalString("cert") keyFile := c.GlobalString("key") diff --git a/typed-errors.go b/typed-errors.go index 555148766..562460f23 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -18,6 +18,9 @@ package main import "errors" +// errSysLogNotSupported - this message is only meaningful on windows +var errSysLogNotSupported = errors.New("Syslog logger not supported on windows") + // errInvalidArgument means that input argument is invalid. var errInvalidArgument = errors.New("Invalid arguments specified")