Skip to content

Commit

Permalink
Add --log_path commandline arg to control log file path
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Jan 23, 2023
1 parent 0f1ef42 commit ab61bd8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions cmd/commons/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func SetCommonFlags(command *cobra.Command) {

command.Flags().StringP("config", "c", "", "Set config file (yaml)")
command.Flags().String("instance_id", "", "Set instance ID")
command.Flags().String("log_path", "", "Set log file path")

command.Flags().String("host", "", "Set iRODS host")
command.Flags().Int("port", 1247, "Set iRODS port")
Expand Down Expand Up @@ -229,6 +230,14 @@ func ProcessCommonFlags(command *cobra.Command, args []string) (*commons.Config,
}
}

logPathFlag := command.Flags().Lookup("log_path")
if logPathFlag != nil {
logPath := logPathFlag.Value.String()
if len(logPath) > 0 {
config.LogPath = logPath
}
}

dataRootFlag := command.Flags().Lookup("data_root")
if dataRootFlag != nil {
dataRoot := dataRootFlag.Value.String()
Expand Down
14 changes: 10 additions & 4 deletions cmd/commons/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (w *NilWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func (w *NilWriter) Close() (err error) {
return nil
}

func ReportChildProcessError() {
fmt.Fprintln(os.Stderr, InterProcessCommunicationFinishError)
}
Expand Down Expand Up @@ -182,14 +186,16 @@ func ChildProcessReadConfigViaSTDIN() (*commons.Config, io.WriteCloser, error) {
}

// output to log file
var logWriter io.WriteCloser
logFilePath := config.GetLogFilePath()
if len(logFilePath) > 0 {
if len(logFilePath) > 0 && logFilePath != "-" {
logWriter, childLogFilePath := getLogWriterForChildProcess(logFilePath)
log.SetOutput(logWriter)

logger.Infof("Logging to %s", childLogFilePath)
return config, logWriter, nil
} else {
var nilWriter NilWriter
log.SetOutput(&nilWriter)
return config, &nilWriter, nil
}

return config, logWriter, nil
}
5 changes: 5 additions & 0 deletions commons/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ func (config *Config) GetInstanceDataRootDirPath() string {
// MakeLogDir makes a log dir required
func (config *Config) MakeLogDir() error {
logFilePath := config.GetLogFilePath()
if logFilePath == "-" {
// skip
return nil
}

logDirPath := filepath.Dir(logFilePath)
err := config.makeDir(logDirPath)
if err != nil {
Expand Down

0 comments on commit ab61bd8

Please sign in to comment.