Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
log support rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jul 14, 2020
1 parent c33058d commit 4d37b4b
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 26 deletions.
1 change: 0 additions & 1 deletion apkmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/pkg/errors"
"github.com/qiniu/log"
"github.com/shogo82148/androidbinary/apk"
)

Expand Down
4 changes: 3 additions & 1 deletion cmdctrl/cmdctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ package cmdctrl
import (
"errors"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
"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")
Expand Down
1 change: 0 additions & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"log"
"net"
"strings"
"time"
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 (
Expand Down
6 changes: 2 additions & 4 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"image/jpeg"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/qiniu/log"
)

type Hub struct {
Expand Down
35 changes: 35 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 (
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions minitouch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"io"
"io/ioutil"
"net"

"github.com/qiniu/log"
)

type toucher struct {
Expand Down
1 change: 0 additions & 1 deletion requirements.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"os"
"regexp"
"runtime"
Expand Down
1 change: 0 additions & 1 deletion tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"math"
"net/url"
"strconv"
Expand Down
1 change: 0 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
Expand Down
1 change: 0 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"image"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
Expand Down

0 comments on commit 4d37b4b

Please sign in to comment.