summaryrefslogtreecommitdiff
path: root/logger
diff options
authorJames Hunt <james.hunt@ubuntu.com>2015-03-06 11:05:39 +0000
committerJames Hunt <james.hunt@ubuntu.com>2015-03-06 11:05:39 +0000
commit4c8f8d93935b607a04f785df3b020497252c3184 (patch)
treee81b4a71b96301a2aa43aff7b68473c4658d45bf /logger
parent8dcb5bb76e4e125b411c17b750ab2b9ccc79c0ff (diff)
* Add basic logging functionality:
- Introduce a customer global logger such that all log.* messages are logged to syslog. - Modified all calls to panic() to first write to the logger. - Changed error handling for critical system commands to ensure the errors are logged first.
Diffstat (limited to 'logger')
-rw-r--r--logger/logger.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/logger/logger.go b/logger/logger.go
new file mode 100644
index 0000000000..6a7b15f4ae
--- /dev/null
+++ b/logger/logger.go
@@ -0,0 +1,74 @@
+package logger
+
+import (
+ "fmt"
+ "log"
+ "log/syslog"
+ "os"
+ "runtime/debug"
+ "strings"
+)
+
+// Name appearing in syslog / journald entries.
+const syslogName = "SnappyLogger"
+
+// SnappyLogger logging object
+type SnappyLogger struct {
+ systemLog *syslog.Writer
+ disabled bool
+}
+
+// Write implements the io.Writer interface
+func (l *SnappyLogger) Write(bytes []byte) (int, error) {
+ str := string(bytes)
+ length := len(str)
+
+ // Now, log to the systemlog.
+ if err := l.systemLog.Info(str); err != nil {
+ return 0, err
+ }
+
+ return length, nil
+}
+
+// New create a new SnappyLogger
+func New() *SnappyLogger {
+ var err error
+
+ l := new(SnappyLogger)
+
+ l.systemLog, err = syslog.New(syslog.LOG_NOTICE|syslog.LOG_LOCAL0, syslogName)
+
+ // By default, fail hard if any aspect of the logger fails.
+ // However, this behaviour can be overriden since if logging
+ // fails, it would otherwise not be possible to upgrade a system
+ // (for example to fix a logger bug).
+ l.disabled = os.Getenv("SNAPPY_LOGGING_DISABLE") != ""
+
+ if err != nil && !l.disabled {
+ panic(fmt.Sprintf("error connecting to system logger: %v", err))
+ }
+
+ return l
+}
+
+// LogError log the specified error (if set), then return it to be dealt with by
+// higher-level parts of the system.
+func LogError(err error) error {
+
+ if err == nil {
+ return nil
+ }
+
+ stack := debug.Stack()
+
+ log.Printf("A snappy error occurred: %v\n", err)
+
+ log.Printf("Stack trace:\n")
+
+ for _, line := range strings.Split(string(stack), "\n") {
+ log.Printf("%s\n", line)
+ }
+
+ return err
+}