aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-02-23 17:58:17 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-02-23 17:58:17 +0100
commit84273d6128b7c3c9d31c2f3a1453107a78e949a9 (patch)
treeb19600e603b4a774359c1540cb9c93c3ecd7d840 /main.go
parentad7380c0eeaa8165067a43734d925a1ccc9187e1 (diff)
Added logfile support
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'main.go')
-rw-r--r--main.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/main.go b/main.go
index 668738c..f9b32f3 100644
--- a/main.go
+++ b/main.go
@@ -178,6 +178,12 @@ func main() {
Value: defaultLogLevel,
},
cli.StringFlag{
+ Name: "logfile",
+ Value: "stderr",
+ Usage: "filename where logs will be redirected (default stderr)\n\t",
+ EnvVar: "XDS_LOGFILENAME",
+ },
+ cli.StringFlag{
Name: "url, u",
EnvVar: "XDS_AGENT_URL",
Value: "localhost:8800",
@@ -206,8 +212,8 @@ func main() {
// Add --config option to all commands to support --config option either before or after command verb
// IOW support following both syntaxes:
- // xds-cli exec --config myprj.conf ...
- // xds-cli --config myprj.conf exec ...
+ // xds-cli exec --config myPrj.conf ...
+ // xds-cli --config myPrj.conf exec ...
for i, cmd := range app.Commands {
if len(cmd.Flags) > 0 {
app.Commands[i].Flags = append(cmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"})
@@ -277,6 +283,17 @@ func main() {
}
Log.Formatter = &logrus.TextFormatter{}
+ if ctx.String("logfile") != "stderr" {
+ logFile, _ := common.ResolveEnvVar(ctx.String("logfile"))
+ fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
+ if err != nil {
+ msgErr := fmt.Sprintf("Cannot create log file %s", logFile)
+ return cli.NewExitError(msgErr, 1)
+ }
+ Log.Infof("Logging to file: %s", logFile)
+ Log.Out = fdL
+ }
+
Log.Infof("%s version: %s", AppName, app.Version)
earlyDisplay()
Log.Debugf("\nEnvironment: %v\n", os.Environ())