@@ -5,17 +5,16 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"io/ioutil"
8
+ "net"
9
+ "net/http"
8
10
"os"
9
11
"path"
12
+ "path/filepath"
10
13
"strings"
11
14
"time"
12
15
13
- "net"
14
- "net/http"
15
-
16
16
"github.com/actiontech/dtle/driver/common"
17
17
"github.com/actiontech/dtle/g"
18
- "github.com/pkg/errors"
19
18
20
19
"github.com/hashicorp/go-hclog"
21
20
"github.com/hashicorp/nomad/drivers/shared/eventer"
@@ -26,6 +25,9 @@ import (
26
25
"github.com/julienschmidt/httprouter"
27
26
gnatsd "github.com/nats-io/nats-server/v2/server"
28
27
stand "github.com/nats-io/nats-streaming-server/server"
28
+ "github.com/pkg/errors"
29
+
30
+ "gopkg.in/natefinch/lumberjack.v2"
29
31
)
30
32
31
33
const (
80
82
hclspec .NewLiteral (`""` )),
81
83
"memory" : hclspec .NewAttr ("memory" , "string" , false ),
82
84
"big_tx_max_jobs" : hclspec .NewAttr ("big_tx_max_jobs" , "number" , false ),
85
+ "log_file" : hclspec .NewDefault (hclspec .NewAttr ("log_file" , "string" , false ),
86
+ hclspec .NewLiteral (`"/var/log/dtle"` )),
83
87
})
84
88
85
89
// taskConfigSpec is the hcl specification for the driver config section of
@@ -227,13 +231,14 @@ type Driver struct {
227
231
228
232
stand * stand.StanServer
229
233
apiServer * httprouter.Router
234
+ setupApiServerFn func (logger g.LoggerType , driverConfig * DriverConfig ) error
230
235
231
236
config * DriverConfig
232
237
233
238
storeManager * common.StoreManager
234
239
}
235
240
236
- func NewDriver (logger g.LoggerType ) drivers. DriverPlugin {
241
+ func NewDriver (logger g.LoggerType ) * Driver {
237
242
logger = logger .Named (g .PluginName )
238
243
logger .Info ("dtle NewDriver" )
239
244
@@ -299,41 +304,79 @@ type DriverConfig struct {
299
304
StatsCollectionInterval int `codec:"stats_collection_interval"`
300
305
PublishMetrics bool `codec:"publish_metrics"`
301
306
LogLevel string `codec:"log_level"`
307
+ LogFile string `codec:"log_file"`
302
308
UiDir string `codec:"ui_dir"`
303
309
RsaPrivateKeyPath string `codec:"rsa_private_key_path"`
304
310
CertFilePath string `codec:"cert_file_path"`
305
311
KeyFilePath string `codec:"key_file_path"`
306
312
Memory string `codec:"memory"`
307
313
}
308
314
315
+ func (d * Driver ) setupLogger () (err error ) {
316
+ err = d .SetLogLevel (d .config .LogLevel )
317
+ if err != nil {
318
+ return err
319
+ }
320
+
321
+ if d .config .LogFile == "" {
322
+ d .logger .Info ("use nomad logger" , "level" , d .config .LogLevel )
323
+ } else {
324
+ err = os .MkdirAll (filepath .Dir (d .config .LogFile ), 0755 )
325
+ if err != nil {
326
+ return err
327
+ }
328
+
329
+ logFileName := d .config .LogFile
330
+ if strings .HasSuffix (logFileName , "/" ) {
331
+ logFileName += "dtle.log"
332
+ }
333
+
334
+ rotateFile := & lumberjack.Logger {
335
+ Filename : logFileName ,
336
+ MaxSize : 512 , // MB
337
+ Compress : true ,
338
+ }
339
+
340
+ d .logger .Info ("switching to dtle logger" , "file" , d .config .LogFile , "level" , d .config .LogLevel )
341
+
342
+ d .logger = hclog .New (& hclog.LoggerOptions {
343
+ Name : "" ,
344
+ Level : hclog .Info ,
345
+ Output : rotateFile ,
346
+ })
347
+ g .Logger = d .logger
348
+
349
+ err = d .SetLogLevel (d .config .LogLevel )
350
+ if err != nil {
351
+ return err
352
+ }
353
+ }
354
+
355
+ return nil
356
+ }
357
+
309
358
func (d * Driver ) SetConfig (c * base.Config ) (err error ) {
310
359
if c != nil && c .AgentConfig != nil {
311
360
d .nomadConfig = c .AgentConfig .Driver
312
361
d .logger .Info ("SetConfig 1" , "DriverConfig" , c .AgentConfig .Driver )
313
362
}
314
363
315
- var dconfig DriverConfig
364
+ d . config = new ( DriverConfig )
316
365
if len (c .PluginConfig ) != 0 {
317
- if err := base .MsgPackDecode (c .PluginConfig , & dconfig ); err != nil {
366
+ if err := base .MsgPackDecode (c .PluginConfig , d . config ); err != nil {
318
367
return err
319
368
}
320
369
}
321
370
322
- d .config = & dconfig
323
371
d .logger .Info ("SetConfig 2" , "config" , d .config )
324
372
325
- logLevel := hclog .LevelFromString (d .config .LogLevel )
326
- if logLevel == hclog .NoLevel {
327
- return fmt .Errorf ("invalid log level %v" , d .config .LogLevel )
328
- }
329
- d .logger .SetLevel (logLevel )
330
- d .logger .Info ("log level was set" , "level" , logLevel .String ())
373
+ err = d .setupLogger ()
331
374
332
375
//if dconfig.Memory != "" {
333
376
// g.MemAvailable = TODO
334
377
//}
335
- if dconfig .BigTxMaxJobs != 0 {
336
- g .BigTxMaxJobs = dconfig .BigTxMaxJobs
378
+ if d . config .BigTxMaxJobs != 0 {
379
+ g .BigTxMaxJobs = d . config .BigTxMaxJobs
337
380
}
338
381
d .logger .Info ("BigTxMaxJobs is set" , "value" , g .BigTxMaxJobs )
339
382
@@ -369,7 +412,7 @@ func (d *Driver) SetConfig(c *base.Config) (err error) {
369
412
}
370
413
}()
371
414
} else {
372
- apiErr := setupApiServerFn (d .logger , d .config )
415
+ apiErr := d . setupApiServerFn (d .logger , d .config )
373
416
if apiErr != nil {
374
417
d .logger .Error ("error in SetupApiServer" , "err" , err ,
375
418
"apiAddr" , d .config .ApiAddr , "nomadAddr" , d .config .NomadAddr )
@@ -420,12 +463,6 @@ func (d *Driver) loopCleanRelayDir() {
420
463
}
421
464
}
422
465
423
- var setupApiServerFn func (logger g.LoggerType , driverConfig * DriverConfig ) error
424
-
425
- func RegisterSetupApiServerFn (fn func (logger g.LoggerType , driverConfig * DriverConfig ) error ) {
426
- setupApiServerFn = fn
427
- }
428
-
429
466
func (d * Driver ) TaskConfigSchema () (* hclspec.Spec , error ) {
430
467
return taskConfigSpec , nil
431
468
}
@@ -812,3 +849,17 @@ func (d *Driver) Shutdown() {
812
849
d .logger .Info ("Driver.Shutdown" )
813
850
d .signalShutdown ()
814
851
}
852
+
853
+ func (d * Driver ) SetLogLevel (level string ) error {
854
+ logLevel := hclog .LevelFromString (level )
855
+ if logLevel == hclog .NoLevel {
856
+ return fmt .Errorf ("log level should be TRACE, DEBUG or INFO, got %v" , level )
857
+ }
858
+ d .logger .SetLevel (logLevel )
859
+
860
+ return nil
861
+ }
862
+
863
+ func (d * Driver ) SetSetupApiServerFn (fn func (logger g.LoggerType , driverConfig * DriverConfig ) (err error )) {
864
+ d .setupApiServerFn = fn
865
+ }
0 commit comments