diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-02 11:23:08 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-02 12:26:36 +0300 |
| commit | 6e1af993924bc7bebe898b403962db5a6b3505d1 (patch) | |
| tree | e4f124dfc917bc96a669e8598400775b2ca5fc25 | |
| parent | 764ef99a3d779a0db1fb60679292af52425ba2f6 (diff) | |
Client default log dir is ~/log
| -rw-r--r-- | cmd/dcat/main.go | 5 | ||||
| -rw-r--r-- | cmd/dgrep/main.go | 5 | ||||
| -rw-r--r-- | cmd/dmap/main.go | 5 | ||||
| -rw-r--r-- | cmd/dserver/main.go | 3 | ||||
| -rw-r--r-- | cmd/dtail/main.go | 5 | ||||
| -rw-r--r-- | internal/config/config.go | 11 | ||||
| -rw-r--r-- | internal/io/dlog/dlog.go | 27 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/strategy.go | 27 | ||||
| -rw-r--r-- | internal/io/dlog/source.go | 19 | ||||
| -rw-r--r-- | internal/source/source.go | 19 |
10 files changed, 83 insertions, 43 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go index 43549b3..21946f6 100644 --- a/cmd/dcat/main.go +++ b/cmd/dcat/main.go @@ -10,6 +10,7 @@ import ( "github.com/mimecast/dtail/internal/config" "github.com/mimecast/dtail/internal/io/dlog" "github.com/mimecast/dtail/internal/io/signal" + "github.com/mimecast/dtail/internal/source" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" ) @@ -30,7 +31,7 @@ func main() { flag.IntVar(&args.SSHPort, "port", config.DefaultSSHPort, "SSH server port") flag.StringVar(&args.ConfigFile, "cfg", "", "Config file path") flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method") - flag.StringVar(&args.LogDir, "logDir", "", "Log dir") + flag.StringVar(&args.LogDir, "logDir", "~/log", "Log dir") flag.StringVar(&args.LogLevel, "logLevel", "", "Log level") flag.StringVar(&args.PrivateKeyPathFile, "key", "", "Path to private key") flag.StringVar(&args.ServersStr, "servers", "", "Remote servers to connect") @@ -50,7 +51,7 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, dlog.CLIENT, config.Common.LogLevel) + dlog.Start(ctx, &wg, source.Client, config.Common.LogLevel) client, err := clients.NewCatClient(args) if err != nil { diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go index 36efe4e..4f21a9e 100644 --- a/cmd/dgrep/main.go +++ b/cmd/dgrep/main.go @@ -10,6 +10,7 @@ import ( "github.com/mimecast/dtail/internal/config" "github.com/mimecast/dtail/internal/io/dlog" "github.com/mimecast/dtail/internal/io/signal" + "github.com/mimecast/dtail/internal/source" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" ) @@ -32,7 +33,7 @@ func main() { flag.IntVar(&args.SSHPort, "port", config.DefaultSSHPort, "SSH server port") flag.StringVar(&args.ConfigFile, "cfg", "", "Config file path") flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method") - flag.StringVar(&args.LogDir, "logDir", "", "Log dir") + flag.StringVar(&args.LogDir, "logDir", "~/log", "Log dir") flag.StringVar(&args.LogLevel, "logLevel", "", "Log level") flag.StringVar(&args.PrivateKeyPathFile, "key", "", "Path to private key") flag.StringVar(&args.RegexStr, "regex", ".", "Regular expression") @@ -54,7 +55,7 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, dlog.CLIENT, args.LogLevel) + dlog.Start(ctx, &wg, source.Client, args.LogLevel) if grep != "" { args.RegexStr = grep diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go index b895964..cc8a158 100644 --- a/cmd/dmap/main.go +++ b/cmd/dmap/main.go @@ -11,6 +11,7 @@ import ( "github.com/mimecast/dtail/internal/io/dlog" "github.com/mimecast/dtail/internal/io/signal" "github.com/mimecast/dtail/internal/omode" + "github.com/mimecast/dtail/internal/source" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" ) @@ -36,7 +37,7 @@ func main() { flag.IntVar(&args.Timeout, "timeout", 0, "Max time dtail server will collect data until disconnection") flag.StringVar(&args.ConfigFile, "cfg", "", "Config file path") flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method") - flag.StringVar(&args.LogDir, "logDir", "", "Log dir") + flag.StringVar(&args.LogDir, "logDir", "~/log", "Log dir") flag.StringVar(&args.LogLevel, "logLevel", "", "Log level") flag.StringVar(&args.PrivateKeyPathFile, "key", "", "Path to private key") flag.StringVar(&args.ServersStr, "servers", "", "Remote servers to connect") @@ -57,7 +58,7 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, dlog.CLIENT, config.Common.LogLevel) + dlog.Start(ctx, &wg, source.Client, config.Common.LogLevel) client, err := clients.NewMaprClient(args, queryStr, clients.DefaultMode) if err != nil { diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go index e77bc21..a44d577 100644 --- a/cmd/dserver/main.go +++ b/cmd/dserver/main.go @@ -16,6 +16,7 @@ import ( "github.com/mimecast/dtail/internal/config" "github.com/mimecast/dtail/internal/io/dlog" "github.com/mimecast/dtail/internal/server" + "github.com/mimecast/dtail/internal/source" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" ) @@ -67,7 +68,7 @@ func main() { var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, dlog.SERVER, config.Common.LogLevel) + dlog.Start(ctx, &wg, source.Server, config.Common.LogLevel) if config.ServerRelaxedAuthEnable { dlog.Server.Fatal("SSH relaxed-auth mode enabled") diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go index 0794f96..95a0427 100644 --- a/cmd/dtail/main.go +++ b/cmd/dtail/main.go @@ -17,6 +17,7 @@ import ( "github.com/mimecast/dtail/internal/io/dlog" "github.com/mimecast/dtail/internal/io/signal" "github.com/mimecast/dtail/internal/omode" + "github.com/mimecast/dtail/internal/source" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" ) @@ -51,7 +52,7 @@ func main() { flag.IntVar(&shutdownAfter, "shutdownAfter", 3600*24, "Shutdown after so many seconds") flag.StringVar(&args.ConfigFile, "cfg", "", "Config file path") flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method") - flag.StringVar(&args.LogDir, "logDir", "", "Log dir") + flag.StringVar(&args.LogDir, "logDir", "~/log", "Log dir") flag.StringVar(&args.LogLevel, "logLevel", "", "Log level") flag.StringVar(&args.PrivateKeyPathFile, "key", "", "Path to private key") flag.StringVar(&args.RegexStr, "regex", ".", "Regular expression") @@ -95,7 +96,7 @@ func main() { var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, dlog.CLIENT, config.Common.LogLevel) + dlog.Start(ctx, &wg, source.Client, config.Common.LogLevel) if pprof > -1 { // For debugging purposes only diff --git a/internal/config/config.go b/internal/config/config.go index 3d05a11..6d4730a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -84,9 +84,16 @@ func (c *configInitializer) transformConfig(args *Args, additionalArgs []string, client *ClientConfig, server *ServerConfig, common *CommonConfig) (*ClientConfig, *ServerConfig, *CommonConfig) { if args.LogDir != "" { common.LogDir = args.LogDir - if common.LogStrategy == "" { - common.LogStrategy = "daily" + } + if strings.Contains(common.LogDir, "~/") { + homeDir, err := os.UserHomeDir() + if err != nil { + panic(err) } + common.LogDir = strings.ReplaceAll(common.LogDir, "~/", fmt.Sprintf("%s/", homeDir)) + } + if common.LogStrategy == "" { + common.LogStrategy = "daily" } if args.LogLevel != "" { diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go index bc9b2f8..3de3120 100644 --- a/internal/io/dlog/dlog.go +++ b/internal/io/dlog/dlog.go @@ -17,6 +17,7 @@ import ( "github.com/mimecast/dtail/internal/io/dlog/loggers" "github.com/mimecast/dtail/internal/io/pool" "github.com/mimecast/dtail/internal/protocol" + "github.com/mimecast/dtail/internal/source" ) // Client is the log handler for the client packages. @@ -32,7 +33,7 @@ var mutex sync.Mutex var started bool // Start logger(s). -func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source, logLevel string) { +func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source.Source, logLevel string) { mutex.Lock() defer mutex.Unlock() @@ -44,17 +45,17 @@ func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source, logLev level := newLevel(logLevel) switch sourceProcess { - case CLIENT: + case source.Client: // This is a DTail client process running. impl := loggers.FOUT - Client = New(CLIENT, CLIENT, level, impl, strategy) - Server = New(CLIENT, SERVER, level, impl, strategy) + Client = New(source.Client, source.Client, level, impl, strategy) + Server = New(source.Client, source.Server, level, impl, strategy) Common = Client - case SERVER: + case source.Server: // This is a DTail server process running. impl := loggers.FILE - Client = New(SERVER, CLIENT, level, impl, strategy) - Server = New(SERVER, SERVER, level, impl, strategy) + Client = New(source.Server, source.Client, level, impl, strategy) + Server = New(source.Server, source.Server, level, impl, strategy) Common = Server } @@ -75,10 +76,10 @@ func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source, logLev type DLog struct { logger loggers.Logger // Is this a DTail server or client process logging? - sourceProcess source + sourceProcess source.Source // Is this a DTail server or client package logging? In serverless mode // the client can also execute code from the server package. - sourcePackage source + sourcePackage source.Source // Max log level to log. maxLevel level // Current hostname. @@ -86,7 +87,7 @@ type DLog struct { } // New creates a new DTail logger. -func New(sourceProcess, sourcePackage source, maxLevel level, impl loggers.Impl, strategy loggers.Strategy) *DLog { +func New(sourceProcess, sourcePackage source.Source, maxLevel level, impl loggers.Impl, strategy loggers.Strategy) *DLog { hostname, err := os.Hostname() if err != nil { panic(err) @@ -120,7 +121,7 @@ func (d *DLog) log(level level, args []interface{}) string { now := time.Now() switch d.sourceProcess { - case CLIENT: + case source.Client: sb.WriteString(d.sourcePackage.String()) sb.WriteString(protocol.FieldDelimiter) sb.WriteString(d.hostname) @@ -179,7 +180,7 @@ func (d *DLog) Warn(args ...interface{}) string { } func (d *DLog) Info(args ...interface{}) string { - if d.sourcePackage == SERVER && d.sourceProcess != CLIENT { + if d.sourcePackage == source.Server && d.sourceProcess != source.Client { // This can be dtail client in serverless mode. In this case log all // info server messages as verbose. return d.log(VERBOSE, args) @@ -215,7 +216,7 @@ func (d *DLog) Raw(message string) string { func (d *DLog) Mapreduce(table string, data map[string]interface{}) string { args := make([]interface{}, len(data)+1) - if d.sourceProcess == SERVER { + if d.sourceProcess == source.Server { // level|date-time|process|caller|cpus|goroutines|cgocalls|loadavg|uptime|MAPREDUCE:TABLE|key=value|... var loadAvg string diff --git a/internal/io/dlog/loggers/strategy.go b/internal/io/dlog/loggers/strategy.go new file mode 100644 index 0000000..a1b9355 --- /dev/null +++ b/internal/io/dlog/loggers/strategy.go @@ -0,0 +1,27 @@ +package loggers + +import ( + "os" + "path/filepath" +) + +type Rotation int + +const ( + DailyRotation Rotation = iota + SignalRotation Rotation = iota +) + +type Strategy struct { + Rotation Rotation + FileBase string +} + +func GetStrategy(name string) Strategy { + switch name { + case "daily": + return Strategy{DailyRotation, ""} + default: + return Strategy{SignalRotation, filepath.Base(os.Args[0])} + } +} diff --git a/internal/io/dlog/source.go b/internal/io/dlog/source.go deleted file mode 100644 index 265885e..0000000 --- a/internal/io/dlog/source.go +++ /dev/null @@ -1,19 +0,0 @@ -package dlog - -type source int - -const ( - CLIENT source = iota - SERVER source = iota -) - -func (s source) String() string { - switch s { - case CLIENT: - return "CLIENT" - case SERVER: - return "SERVER" - } - - panic("Unknown log source type") -} diff --git a/internal/source/source.go b/internal/source/source.go new file mode 100644 index 0000000..bf1c880 --- /dev/null +++ b/internal/source/source.go @@ -0,0 +1,19 @@ +package source + +type Source int + +const ( + Client Source = iota + Server Source = iota +) + +func (s Source) String() string { + switch s { + case Client: + return "Client" + case Server: + return "Server" + } + + panic("Unknown log source type") +} |
