Log config file parsing errors properly

closes #1344
This commit is contained in:
Cameron Sparr 2016-10-12 15:37:51 +01:00
parent a84ce5d5cb
commit b00ad65b08
4 changed files with 24 additions and 20 deletions

View File

@ -2,6 +2,8 @@
### Release Notes ### Release Notes
- Telegraf now supports two new types of plugins: processors & aggregators.
- On systemd Telegraf will no longer redirect it's stdout to /var/log/telegraf/telegraf.log. - On systemd Telegraf will no longer redirect it's stdout to /var/log/telegraf/telegraf.log.
On most systems, the logs will be directed to the systemd journal and can be On most systems, the logs will be directed to the systemd journal and can be
accessed by `journalctl -u telegraf.service`. Consult the systemd journal accessed by `journalctl -u telegraf.service`. Consult the systemd journal
@ -11,6 +13,7 @@ continue sending logs to /var/log/telegraf/telegraf.log.
### Features ### Features
- [#1726](https://github.com/influxdata/telegraf/issues/1726): Processor & Aggregator plugin support.
- [#1861](https://github.com/influxdata/telegraf/pull/1861): adding the tags in the graylog output plugin - [#1861](https://github.com/influxdata/telegraf/pull/1861): adding the tags in the graylog output plugin
- [#1732](https://github.com/influxdata/telegraf/pull/1732): Telegraf systemd service, log to journal. - [#1732](https://github.com/influxdata/telegraf/pull/1732): Telegraf systemd service, log to journal.
- [#1782](https://github.com/influxdata/telegraf/pull/1782): Allow numeric and non-string values for tag_keys. - [#1782](https://github.com/influxdata/telegraf/pull/1782): Allow numeric and non-string values for tag_keys.
@ -62,6 +65,8 @@ continue sending logs to /var/log/telegraf/telegraf.log.
- [#1836](https://github.com/influxdata/telegraf/pull/1836): Fix snmp table field initialization for non-automatic table. - [#1836](https://github.com/influxdata/telegraf/pull/1836): Fix snmp table field initialization for non-automatic table.
- [#1724](https://github.com/influxdata/telegraf/issues/1724): cgroups path being parsed as metric. - [#1724](https://github.com/influxdata/telegraf/issues/1724): cgroups path being parsed as metric.
- [#1886](https://github.com/influxdata/telegraf/issues/1886): Fix phpfpm fcgi client panic when URL does not exist. - [#1886](https://github.com/influxdata/telegraf/issues/1886): Fix phpfpm fcgi client panic when URL does not exist.
- [#1344](https://github.com/influxdata/telegraf/issues/1344): Fix config file parse error logging.
- [#1771](https://github.com/influxdata/telegraf/issues/1771): Delete nil fields in the metric maker.
## v1.0.1 [2016-09-26] ## v1.0.1 [2016-09-26]

View File

@ -192,7 +192,7 @@ func reloadLoop(stop chan struct{}, s service.Service) {
case *fUsage != "": case *fUsage != "":
if err := config.PrintInputConfig(*fUsage); err != nil { if err := config.PrintInputConfig(*fUsage); err != nil {
if err2 := config.PrintOutputConfig(*fUsage); err2 != nil { if err2 := config.PrintOutputConfig(*fUsage); err2 != nil {
log.Fatalf("%s and %s", err, err2) log.Fatalf("E! %s and %s", err, err2)
} }
} }
return return
@ -204,26 +204,25 @@ func reloadLoop(stop chan struct{}, s service.Service) {
c.InputFilters = inputFilters c.InputFilters = inputFilters
err := c.LoadConfig(*fConfig) err := c.LoadConfig(*fConfig)
if err != nil { if err != nil {
fmt.Println(err) log.Fatal("E! " + err.Error())
os.Exit(1)
} }
if *fConfigDirectory != "" { if *fConfigDirectory != "" {
err = c.LoadDirectory(*fConfigDirectory) err = c.LoadDirectory(*fConfigDirectory)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
} }
if len(c.Outputs) == 0 { if len(c.Outputs) == 0 {
log.Fatalf("Error: no outputs found, did you provide a valid config file?") log.Fatalf("E! Error: no outputs found, did you provide a valid config file?")
} }
if len(c.Inputs) == 0 { if len(c.Inputs) == 0 {
log.Fatalf("Error: no inputs found, did you provide a valid config file?") log.Fatalf("E! Error: no inputs found, did you provide a valid config file?")
} }
ag, err := agent.NewAgent(c) ag, err := agent.NewAgent(c)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
// Setup logging // Setup logging
@ -236,14 +235,14 @@ func reloadLoop(stop chan struct{}, s service.Service) {
if *fTest { if *fTest {
err = ag.Test() err = ag.Test()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
return return
} }
err = ag.Connect() err = ag.Connect()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
shutdown := make(chan struct{}) shutdown := make(chan struct{})
@ -274,7 +273,7 @@ func reloadLoop(stop chan struct{}, s service.Service) {
if *fPidfile != "" { if *fPidfile != "" {
f, err := os.Create(*fPidfile) f, err := os.Create(*fPidfile)
if err != nil { if err != nil {
log.Fatalf("Unable to create pidfile: %s", err) log.Fatalf("E! Unable to create pidfile: %s", err)
} }
fmt.Fprintf(f, "%d\n", os.Getpid()) fmt.Fprintf(f, "%d\n", os.Getpid())
@ -320,7 +319,7 @@ func main() {
prg := &program{} prg := &program{}
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
// Handle the -service flag here to prevent any issues with tooling that // Handle the -service flag here to prevent any issues with tooling that
// may not have an interactive session, e.g. installing from Ansible. // may not have an interactive session, e.g. installing from Ansible.
@ -330,7 +329,7 @@ func main() {
} }
err := service.Control(s, *fService) err := service.Control(s, *fService)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("E! " + err.Error())
} }
} else { } else {
err = s.Run() err = s.Run()

View File

@ -241,7 +241,7 @@ var header = `# Telegraf Configuration
debug = false debug = false
## Run telegraf in quiet mode (error log messages only). ## Run telegraf in quiet mode (error log messages only).
quiet = false quiet = false
## Specify the log file name. The empty string means to log to stdout. ## Specify the log file name. The empty string means to log to stderr.
logfile = "" logfile = ""
## Override default hostname, if empty use os.Hostname() ## Override default hostname, if empty use os.Hostname()

View File

@ -27,8 +27,8 @@ func (t *telegrafLog) Write(p []byte) (n int, err error) {
// debug will set the log level to DEBUG // debug will set the log level to DEBUG
// quiet will set the log level to ERROR // quiet will set the log level to ERROR
// logfile will direct the logging output to a file. Empty string is // logfile will direct the logging output to a file. Empty string is
// interpreted as stdout. If there is an error opening the file the // interpreted as stderr. If there is an error opening the file the
// logger will fallback to stdout. // logger will fallback to stderr.
func SetupLogging(debug, quiet bool, logfile string) { func SetupLogging(debug, quiet bool, logfile string) {
if debug { if debug {
wlog.SetLevel(wlog.DEBUG) wlog.SetLevel(wlog.DEBUG)
@ -41,17 +41,17 @@ func SetupLogging(debug, quiet bool, logfile string) {
if logfile != "" { if logfile != "" {
if _, err := os.Stat(logfile); os.IsNotExist(err) { if _, err := os.Stat(logfile); os.IsNotExist(err) {
if oFile, err = os.Create(logfile); err != nil { if oFile, err = os.Create(logfile); err != nil {
log.Printf("E! Unable to create %s (%s), using stdout", logfile, err) log.Printf("E! Unable to create %s (%s), using stderr", logfile, err)
oFile = os.Stdout oFile = os.Stderr
} }
} else { } else {
if oFile, err = os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY, os.ModeAppend); err != nil { if oFile, err = os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY, os.ModeAppend); err != nil {
log.Printf("E! Unable to append to %s (%s), using stdout", logfile, err) log.Printf("E! Unable to append to %s (%s), using stderr", logfile, err)
oFile = os.Stdout oFile = os.Stderr
} }
} }
} else { } else {
oFile = os.Stdout oFile = os.Stderr
} }
log.SetOutput(newTelegrafWriter(oFile)) log.SetOutput(newTelegrafWriter(oFile))