Improve startup error reporting when ran as Windows service (#4291)

This commit is contained in:
Vlasta Hajek
2019-08-28 23:34:44 +02:00
committed by Daniel Nelson
parent 8c6ddcd7fa
commit 7ec54d4be9
8 changed files with 295 additions and 33 deletions

View File

@@ -150,8 +150,6 @@ func runAgent(ctx context.Context,
inputFilters []string,
outputFilters []string,
) error {
// Setup default logging. This may need to change after reading the config
// file, but we can configure it to use our logger implementation now.
log.Printf("I! Starting Telegraf %s", version)
// If no other options are specified, load the config file and run.
@@ -195,6 +193,7 @@ func runAgent(ctx context.Context,
logConfig := logger.LogConfig{
Debug: ag.Config.Agent.Debug || *fDebug,
Quiet: ag.Config.Agent.Quiet || *fQuiet,
LogTarget: ag.Config.Agent.LogTarget,
Logfile: ag.Config.Agent.Logfile,
RotationInterval: ag.Config.Agent.LogfileRotationInterval,
RotationMaxSize: ag.Config.Agent.LogfileRotationMaxSize,
@@ -429,18 +428,28 @@ func main() {
// may not have an interactive session, e.g. installing from Ansible.
if *fService != "" {
if *fConfig != "" {
(*svcConfig).Arguments = []string{"--config", *fConfig}
svcConfig.Arguments = []string{"--config", *fConfig}
}
if *fConfigDirectory != "" {
(*svcConfig).Arguments = append((*svcConfig).Arguments, "--config-directory", *fConfigDirectory)
svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", *fConfigDirectory)
}
//set servicename to service cmd line, to have a custom name after relaunch as a service
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)
err := service.Control(s, *fService)
if err != nil {
log.Fatal("E! " + err.Error())
}
os.Exit(0)
} else {
winlogger, err := s.Logger(nil)
if err == nil {
//When in service mode, register eventlog target andd setup default logging to eventlog
logger.RegisterEventLogger(winlogger)
logger.SetupLogging(logger.LogConfig{LogTarget: logger.LogTargetEventlog})
}
err = s.Run()
if err != nil {
log.Println("E! " + err.Error())
}