@@ -16,6 +16,7 @@ import (
1616 "github.com/yext/edward/config"
1717 "github.com/yext/edward/edward"
1818 "github.com/yext/edward/home"
19+ "github.com/yext/edward/output"
1920 "github.com/yext/edward/services"
2021 "github.com/yext/edward/updates"
2122)
@@ -36,6 +37,21 @@ var RootCmd = &cobra.Command{
3637Build, start and manage service instances with a single command.` ,
3738 SilenceUsage : true ,
3839 PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
40+ if redirectLogs {
41+ logger = log .New (os .Stdout , fmt .Sprintf ("%v >" , os .Args ), log .Ldate | log .Ltime | log .Lmicroseconds | log .Lshortfile )
42+ }
43+ if logFile != "" {
44+ prefix := "edward"
45+ if len (args ) > 0 {
46+ prefix = fmt .Sprintf ("%s %s" , cmd .Use , args [0 ])
47+ }
48+ logger = log .New (& lumberjack.Logger {
49+ Filename : logFile ,
50+ MaxSize : 50 , // megabytes
51+ MaxBackups : 30 ,
52+ MaxAge : 1 , //days
53+ }, fmt .Sprintf ("%s > " , prefix ), log .Ldate | log .Ltime | log .Lmicroseconds | log .Lshortfile )
54+ }
3955 // Begin logging
4056 logger .Printf ("=== Edward v%v ===\n " , common .EdwardVersion )
4157 logger .Printf ("Args: %v\n " , os .Args )
@@ -53,7 +69,7 @@ Build, start and manage service instances with a single command.`,
5369
5470 var err error
5571 if command != "generate" {
56- edwardClient , err = edward .NewClientWithConfig (configPath , common .EdwardVersion )
72+ edwardClient , err = edward .NewClientWithConfig (configPath , common .EdwardVersion , logger )
5773 if err != nil {
5874 return errors .WithStack (err )
5975 }
@@ -76,6 +92,13 @@ Build, start and manage service instances with a single command.`,
7692 // Populate the Edward executable with this binary
7793 edwardClient .EdwardExecutable = os .Args [0 ]
7894
95+ // Let the client know about the log file for starting runners
96+ edwardClient .LogFile = logFile
97+
98+ if redirectLogs {
99+ edwardClient .Follower = output .NewNonLiveFollower ()
100+ }
101+
79102 if command != "stop" {
80103 // Check for legacy pidfiles and error out if any are found
81104 for _ , service := range edwardClient .ServiceMap () {
@@ -112,12 +135,17 @@ func Execute() {
112135 fmt .Printf ("%+v" , err )
113136 }
114137
138+ logPrefix := "edward"
139+ if len (os .Args ) > 1 {
140+ logPrefix = fmt .Sprintf ("edward %v >" , os .Args [1 :])
141+ }
142+
115143 logger = log .New (& lumberjack.Logger {
116144 Filename : filepath .Join (home .EdwardConfig .EdwardLogDir , "edward.log" ),
117145 MaxSize : 50 , // megabytes
118146 MaxBackups : 30 ,
119147 MaxAge : 1 , //days
120- }, fmt . Sprintf ( "%v >" , os . Args ) , log .Ldate | log .Ltime | log .Lmicroseconds | log .Lshortfile )
148+ }, logPrefix , log .Ldate | log .Ltime | log .Lmicroseconds | log .Lshortfile )
121149
122150 for _ , arg := range os .Args {
123151 if arg == "--generate-bash-completion" {
@@ -127,16 +155,29 @@ func Execute() {
127155 }
128156
129157 if err := RootCmd .Execute (); err != nil {
158+ logger .Printf ("Error: %v\n " , err )
130159 os .Exit (1 )
131160 }
132161}
133162
134163var configPath string
164+ var redirectLogs bool
165+ var logFile string
135166
136167func init () {
137168 cobra .OnInitialize (initConfig )
138169
170+ RootCmd .PersistentFlags ().StringVar (& logFile , "logfile" , "" , "Write logs to `PATH`" )
139171 RootCmd .PersistentFlags ().StringVarP (& configPath , "config" , "c" , "" , "Use service configuration file at `PATH`" )
172+ RootCmd .PersistentFlags ().BoolVar (& redirectLogs , "redirect_logs" , false , "Redirect edward logs to the console" )
173+ err := RootCmd .PersistentFlags ().MarkHidden ("redirect_logs" )
174+ if err != nil {
175+ panic (err )
176+ }
177+ err = RootCmd .PersistentFlags ().MarkHidden ("logfile" )
178+ if err != nil {
179+ panic (err )
180+ }
140181}
141182
142183// initConfig reads in config file and ENV variables if set.
@@ -148,7 +189,7 @@ func initConfig() {
148189 // Find home directory.
149190 home , err := homedir .Dir ()
150191 if err != nil {
151- fmt .Println (err )
192+ fmt .Println ("initConfig: error finding home dir:" , err )
152193 os .Exit (1 )
153194 }
154195
@@ -167,7 +208,7 @@ func initConfig() {
167208
168209func checkUpdateAvailable (checkUpdateChan chan interface {}) {
169210 defer close (checkUpdateChan )
170- updateAvailable , latestVersion , err := updates .UpdateAvailable ("yext" , "edward" , common .EdwardVersion , filepath .Join (home .EdwardConfig .Dir , ".updatecache " ), logger )
211+ updateAvailable , latestVersion , err := updates .UpdateAvailable ("yext" , "edward" , common .EdwardVersion , filepath .Join (home .EdwardConfig .Dir , ".cache/version " ), logger )
171212 if err != nil {
172213 logger .Println ("Error checking for updates:" , err )
173214 return
0 commit comments