Add PACKER_LOG_PATH for logging to a file
When the environment variables PACKER_LOG and PACKER_LOG_PATH are both set the log output will be appended to the PACKER_LOG_PATH file.
This commit is contained in:
parent
fb4a86ef35
commit
bad2bfc4e5
19
packer.go
19
packer.go
|
@ -14,23 +14,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
switch packer_log := os.Getenv("PACKER_LOG"); packer_log {
|
// Setup logging if PACKER_LOG is set.
|
||||||
case "":
|
// Log to PACKER_LOG_PATH if it is set, otherwise default to stderr.
|
||||||
|
if os.Getenv("PACKER_LOG") == "" {
|
||||||
// If we don't have logging explicitly enabled, then disable it
|
// If we don't have logging explicitly enabled, then disable it
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
case "1":
|
} else {
|
||||||
// Legacy logging is enabled, make sure it goes to stderr
|
if log_path := os.Getenv("PACKER_LOG_PATH"); log_path == "" {
|
||||||
log.SetOutput(os.Stderr)
|
log.SetOutput(os.Stderr)
|
||||||
default:
|
} else {
|
||||||
{
|
file, err := os.OpenFile(log_path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
||||||
// Use a file for logging
|
|
||||||
file, err := os.OpenFile(packer_log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.SetOutput(file)
|
log.SetOutput(file)
|
||||||
} else {
|
} else {
|
||||||
// Problem opening the file, fail back to Stderr
|
// Problem opening the file, fail back to Stderr
|
||||||
log.SetOutput(os.Stderr)
|
log.SetOutput(os.Stderr)
|
||||||
log.Printf("Could not open %s for logging (%s). Using stderr instead.", packer_log, err.Error())
|
log.Printf("Could not open %s for logging (%s). Using stderr instead.", log_path, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue