Add persistent logging: syslog for unix, file for windows (#6)
This commit is contained in:
21
persistentlog/syslog.go
Normal file
21
persistentlog/syslog.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// +build !windows,!nacl,!plan9
|
||||
|
||||
package persistentlog
|
||||
|
||||
import (
|
||||
"log/syslog"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
logSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
||||
)
|
||||
|
||||
// AddPersistentLogHook configures persisting logs in syslog
|
||||
func AddPersistentLogHook() {
|
||||
hook, err := logSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "browserpass")
|
||||
|
||||
if err == nil {
|
||||
log.AddHook(hook)
|
||||
} else {
|
||||
log.Warn("Unable to connect to syslog, logs will NOT be persisted")
|
||||
}
|
||||
}
|
10
persistentlog/unsupported.go
Normal file
10
persistentlog/unsupported.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// +build nacl,plan9
|
||||
|
||||
package persistentlog
|
||||
|
||||
import log "github.com/sirupsen/logrus"
|
||||
|
||||
// AddPersistentLogHook configures persisting logs, not supported on these systems
|
||||
func AddPersistentLogHook() {
|
||||
log.Warn("Persistent logging is not implemented on this OS")
|
||||
}
|
28
persistentlog/windows.go
Normal file
28
persistentlog/windows.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// +build windows
|
||||
|
||||
package persistentlog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rifflock/lfshook"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddPersistentLogHook configures persisting logs in a file
|
||||
func AddPersistentLogHook() {
|
||||
appDataPath := os.Getenv("LOCALAPPDATA")
|
||||
if appDataPath == "" {
|
||||
log.Warn("Unable to determine %%APPDATA%% folder location, logs will NOT be persisted")
|
||||
return
|
||||
}
|
||||
logFolderPath := filepath.Join(appDataPath, "browserpass")
|
||||
if err := os.MkdirAll(logFolderPath, os.ModePerm); err != nil {
|
||||
log.Warn("Unable to create browserpass folder in %%APPDATA%%, logs will NOT be persisted")
|
||||
return
|
||||
}
|
||||
logFilePath := filepath.Join(logFolderPath, "browserpass.log")
|
||||
log.Debug("Logs will being written to: ", logFilePath)
|
||||
log.AddHook(lfshook.NewHook(logFilePath, &log.TextFormatter{FullTimestamp: true}))
|
||||
}
|
Reference in New Issue
Block a user