From fb4a86ef358cc5c3ff267d535d09a22287972cf4 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Thu, 11 Jul 2013 19:42:30 +0000 Subject: [PATCH 1/2] Add ability to send log output to a file Using PACKER_LOG=1 causes all the log output to be sent to Stderr. This change maintains that backward compatility. Anything other than "1" will be treated as a filename which will have logging appended to that file. This is useful, for example, to always have debugging available without cluttering up stdout (and without having to redirect stderr all the time). --- packer.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packer.go b/packer.go index 04acf0ff9..c9a1e867b 100644 --- a/packer.go +++ b/packer.go @@ -14,12 +14,25 @@ import ( ) func main() { - if os.Getenv("PACKER_LOG") == "" { + switch packer_log := os.Getenv("PACKER_LOG"); packer_log { + case "": // If we don't have logging explicitly enabled, then disable it log.SetOutput(ioutil.Discard) - } else { - // Logging is enabled, make sure it goes to stderr + case "1": + // Legacy logging is enabled, make sure it goes to stderr log.SetOutput(os.Stderr) + default: + { + // Use a file for logging + file, err := os.OpenFile(packer_log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) + if err == nil { + log.SetOutput(file) + } else { + // Problem opening the file, fail back to Stderr + log.SetOutput(os.Stderr) + log.Printf("Could not open %s for logging (%s). Using stderr instead.", packer_log, err.Error()) + } + } } // If there is no explicit number of Go threads to use, then set it From bad2bfc4e5f31729356cc90eda72e8d91a9f3fe5 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Sat, 13 Jul 2013 15:51:27 +0000 Subject: [PATCH 2/2] 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. --- packer.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packer.go b/packer.go index c9a1e867b..17a80b075 100644 --- a/packer.go +++ b/packer.go @@ -14,23 +14,22 @@ import ( ) func main() { - switch packer_log := os.Getenv("PACKER_LOG"); packer_log { - case "": + // Setup logging if PACKER_LOG is set. + // 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 log.SetOutput(ioutil.Discard) - case "1": - // Legacy logging is enabled, make sure it goes to stderr - log.SetOutput(os.Stderr) - default: - { - // Use a file for logging - file, err := os.OpenFile(packer_log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) + } else { + if log_path := os.Getenv("PACKER_LOG_PATH"); log_path == "" { + log.SetOutput(os.Stderr) + } else { + file, err := os.OpenFile(log_path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) if err == nil { log.SetOutput(file) } else { // Problem opening the file, fail back to 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()) } } }