From 4d37b4bbb8908f14d284ed6378a0534584555900 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Tue, 14 Jul 2020 09:42:26 +0800 Subject: [PATCH] log support rotate --- apkmanager.go | 1 - cmdctrl/cmdctrl.go | 4 +++- dns.go | 1 - go.mod | 10 ++++------ httpserver.go | 6 ++---- hub.go | 1 - logger/logger.go | 35 +++++++++++++++++++++++++++++++++++ main.go | 18 ++++++++++++------ minitouch.go | 2 -- requirements.go | 1 - tunnelproxy.go | 1 - update.go | 1 - utils.go | 1 - 13 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 logger/logger.go diff --git a/apkmanager.go b/apkmanager.go index 6201238..69f6288 100644 --- a/apkmanager.go +++ b/apkmanager.go @@ -7,7 +7,6 @@ import ( "time" "github.com/pkg/errors" - "github.com/qiniu/log" "github.com/shogo82148/androidbinary/apk" ) diff --git a/cmdctrl/cmdctrl.go b/cmdctrl/cmdctrl.go index 470f666..51a6846 100644 --- a/cmdctrl/cmdctrl.go +++ b/cmdctrl/cmdctrl.go @@ -6,7 +6,6 @@ package cmdctrl import ( "errors" "io" - "log" "os" "os/exec" "runtime" @@ -14,10 +13,13 @@ import ( "sync" "syscall" "time" + + "github.com/openatx/atx-agent/logger" ) var ( debug = true + log = logger.Default ErrAlreadyRunning = errors.New("already running") ErrAlreadyStopped = errors.New("already stopped") diff --git a/dns.go b/dns.go index a07db2f..8a6cbc0 100644 --- a/dns.go +++ b/dns.go @@ -2,7 +2,6 @@ package main import ( "context" - "log" "net" "strings" "time" diff --git a/go.mod b/go.mod index 507cc13..48144ab 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/openatx/atx-agent require ( + github.com/BurntSushi/toml v0.3.1 // indirect github.com/DeanThompson/syncmap v0.0.0-20170515023643-05cfe1984971 github.com/alecthomas/kingpin v2.2.6+incompatible github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect @@ -20,7 +21,6 @@ require ( github.com/go-stack/stack v1.8.0 // indirect github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect github.com/google/go-querystring v1.0.0 // indirect - github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/handlers v1.4.2 github.com/gorilla/mux v1.7.4 github.com/gorilla/websocket v1.4.0 @@ -38,19 +38,17 @@ require ( github.com/pierrec/lz4 v2.0.5+incompatible // indirect github.com/pkg/errors v0.8.1 github.com/prometheus/procfs v0.0.2 - github.com/qiniu/log v0.0.0-20140728010919-a304a74568d6 github.com/rs/cors v1.6.0 github.com/sevlyar/go-daemon v0.1.4 github.com/shogo82148/androidbinary v1.0.1 github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect - github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd // indirect - github.com/sirupsen/logrus v1.1.1 + github.com/shurcooL/vfsgen v0.0.0-20200627165143-92b8a710ab6c // indirect + github.com/sirupsen/logrus v1.6.0 github.com/stretchr/testify v1.4.0 github.com/ulikunitz/xz v0.5.5 // indirect - golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // indirect golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect - golang.org/x/sys v0.0.0-20181121002834-0cf1ed9e522b // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0 ) replace ( diff --git a/httpserver.go b/httpserver.go index dc0543f..e5fca4e 100644 --- a/httpserver.go +++ b/httpserver.go @@ -7,7 +7,6 @@ import ( "image/jpeg" "io" "io/ioutil" - "log" "net" "net/http" "os" @@ -23,7 +22,6 @@ import ( "github.com/openatx/atx-agent/jsonrpc" - "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/gorilla/websocket" "github.com/mholt/archiver" @@ -1157,8 +1155,8 @@ func (server *Server) initHTTPServer() { var handler = cors.New(cors.Options{ AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, }).Handler(m) - logHandler := handlers.LoggingHandler(os.Stdout, handler) - server.httpServer = &http.Server{Handler: logHandler} // url(/stop) need it. + // logHandler := handlers.LoggingHandler(os.Stdout, handler) + server.httpServer = &http.Server{Handler: handler} // url(/stop) need it. } func (s *Server) Serve(lis net.Listener) error { diff --git a/hub.go b/hub.go index db0de95..c75d4fc 100644 --- a/hub.go +++ b/hub.go @@ -9,7 +9,6 @@ import ( "time" "github.com/gorilla/websocket" - "github.com/qiniu/log" ) type Hub struct { diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..75bd281 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,35 @@ +package logger + +// import +import ( + + // "github.com/qiniu/log" + "github.com/sirupsen/logrus" + "gopkg.in/natefinch/lumberjack.v2" +) + +var Default *logrus.Logger + +func init() { + // Default = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile) + Default = logrus.New() + // logrus.Rep + Default.SetLevel(logrus.DebugLevel) +} + +func SetOutputFile(filename string) error { + // f, err := os.Create(filename) + // if err != nil { + // return err + // } + Default.SetOutput(&lumberjack.Logger{ + Filename: filename, + MaxSize: 100, // megabytes + MaxBackups: 3, + MaxAge: 1, //days + Compress: true, // disabled by default + }) + return nil + // Default = log.New(out, "", log.LstdFlags|log.Lshortfile) + // Default.SetOutputLevel(log.Ldebug) +} diff --git a/main.go b/main.go index 5e724a0..a745ff9 100644 --- a/main.go +++ b/main.go @@ -31,9 +31,9 @@ import ( "github.com/gorilla/websocket" "github.com/openatx/androidutils" "github.com/openatx/atx-agent/cmdctrl" + "github.com/openatx/atx-agent/logger" "github.com/openatx/atx-agent/subcmd" "github.com/pkg/errors" - "github.com/qiniu/log" "github.com/sevlyar/go-daemon" ) @@ -52,11 +52,12 @@ var ( owner = "openatx" repo = "atx-agent" listenPort int - daemonLogPath = "/sdcard/atx-agent.log" + daemonLogPath = "/sdcard/atx-agent.daemon.log" rotationPublisher = broadcast.NewBroadcaster(1) minicapSocketPath = "@minicap" minitouchSocketPath = "@minitouch" + log = logger.Default ) const ( @@ -381,7 +382,7 @@ func runDaemon() (cntxt *daemon.Context) { Umask: 022, } // log might be no auth - if f, err := os.OpenFile(daemonLogPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err == nil { + if f, err := os.OpenFile(daemonLogPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644); err == nil { // |os.O_APPEND f.Close() cntxt.LogFileName = daemonLogPath } @@ -396,6 +397,10 @@ func runDaemon() (cntxt *daemon.Context) { return cntxt } +func setupLogrotate() { + logger.SetOutputFile("/sdcard/atx-agent.log") +} + func stopSelf() { // kill previous daemon first log.Println("stop server self") @@ -594,11 +599,12 @@ func main() { return } defer cntxt.Release() - log.Print("- - - - - - - - - - - - - - -") - log.Print("daemon started") + log.Println("- - - - - - - - - - - - - - -") + log.Println("daemon started") + setupLogrotate() } - fmt.Printf("atx-agent version %s\n", version) + log.Printf("atx-agent version %s\n", version) lazyInit() // show ip diff --git a/minitouch.go b/minitouch.go index 66f3d80..5e40191 100644 --- a/minitouch.go +++ b/minitouch.go @@ -7,8 +7,6 @@ import ( "io" "io/ioutil" "net" - - "github.com/qiniu/log" ) type toucher struct { diff --git a/requirements.go b/requirements.go index 00ed975..126187a 100644 --- a/requirements.go +++ b/requirements.go @@ -1,7 +1,6 @@ package main import ( - "log" "os" "regexp" "runtime" diff --git a/tunnelproxy.go b/tunnelproxy.go index d6d9c48..3bd56a6 100644 --- a/tunnelproxy.go +++ b/tunnelproxy.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - "log" "math" "net/url" "strconv" diff --git a/update.go b/update.go index fd20774..ed7d4b9 100644 --- a/update.go +++ b/update.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "log" "net/http" "os" "path/filepath" diff --git a/utils.go b/utils.go index 1afe123..fc66210 100644 --- a/utils.go +++ b/utils.go @@ -9,7 +9,6 @@ import ( "image" "io" "io/ioutil" - "log" "net" "net/http" "os"