utils/log: support max days of keeping log files

This commit is contained in:
fatedier 2016-05-03 21:41:57 +08:00
parent d7797cbd18
commit c10321ead6
8 changed files with 25 additions and 10 deletions

View File

@ -6,6 +6,7 @@ server_port = 7000
log_file = ./frpc.log log_file = ./frpc.log
# debug, info, warn, error # debug, info, warn, error
log_level = info log_level = info
log_max_days = 3
# for authentication # for authentication
auth_token = 123 auth_token = 123

View File

@ -8,6 +8,7 @@ vhost_http_port = 80
log_file = ./frps.log log_file = ./frps.log
# debug, info, warn, error # debug, info, warn, error
log_level = info log_level = info
log_max_days = 3
# ssh is the proxy name, client will use this name and auth_token to connect to server # ssh is the proxy name, client will use this name and auth_token to connect to server
[ssh] [ssh]

View File

@ -88,7 +88,7 @@ func main() {
client.ServerPort = serverPort client.ServerPort = serverPort
} }
log.InitLog(client.LogWay, client.LogFile, client.LogLevel) log.InitLog(client.LogWay, client.LogFile, client.LogLevel, client.LogMaxDays)
// wait until all control goroutine exit // wait until all control goroutine exit
var wait sync.WaitGroup var wait sync.WaitGroup

View File

@ -90,7 +90,7 @@ func main() {
server.BindPort = bindPort server.BindPort = bindPort
} }
log.InitLog(server.LogWay, server.LogFile, server.LogLevel) log.InitLog(server.LogWay, server.LogFile, server.LogLevel, server.LogMaxDays)
l, err := conn.Listen(server.BindAddr, server.BindPort) l, err := conn.Listen(server.BindAddr, server.BindPort)
if err != nil { if err != nil {
@ -98,6 +98,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// create vhost if VhostHttpPort != 0
if server.VhostHttpPort != 0 { if server.VhostHttpPort != 0 {
vhostListener, err := conn.Listen(server.BindAddr, server.VhostHttpPort) vhostListener, err := conn.Listen(server.BindAddr, server.VhostHttpPort)
if err != nil { if err != nil {

View File

@ -28,6 +28,7 @@ var (
LogFile string = "console" LogFile string = "console"
LogWay string = "console" LogWay string = "console"
LogLevel string = "info" LogLevel string = "info"
LogMaxDays int64 = 3
HeartBeatInterval int64 = 20 HeartBeatInterval int64 = 20
HeartBeatTimeout int64 = 90 HeartBeatTimeout int64 = 90
) )
@ -69,6 +70,11 @@ func LoadConf(confFile string) (err error) {
LogLevel = tmpStr LogLevel = tmpStr
} }
tmpStr, ok = conf.Get("common", "log_max_days")
if ok {
LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
}
var authToken string var authToken string
tmpStr, ok = conf.Get("common", "auth_token") tmpStr, ok = conf.Get("common", "auth_token")
if ok { if ok {

View File

@ -32,6 +32,7 @@ var (
LogFile string = "console" LogFile string = "console"
LogWay string = "console" // console or file LogWay string = "console" // console or file
LogLevel string = "info" LogLevel string = "info"
LogMaxDays int64 = 3
HeartBeatTimeout int64 = 90 HeartBeatTimeout int64 = 90
UserConnTimeout int64 = 10 UserConnTimeout int64 = 10
@ -82,6 +83,11 @@ func LoadConf(confFile string) (err error) {
LogLevel = tmpStr LogLevel = tmpStr
} }
tmpStr, ok = conf.Get("common", "log_max_days")
if ok {
LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
}
// servers // servers
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {

View File

@ -113,8 +113,8 @@ func (p *ProxyServer) Start() (err error) {
// set timeout // set timeout
time.AfterFunc(time.Duration(UserConnTimeout)*time.Second, func() { time.AfterFunc(time.Duration(UserConnTimeout)*time.Second, func() {
p.Lock() p.Lock()
defer p.Unlock()
element := p.userConnList.Front() element := p.userConnList.Front()
p.Unlock()
if element == nil { if element == nil {
return return
} }

View File

@ -15,6 +15,7 @@
package log package log
import ( import (
"fmt"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
) )
@ -26,24 +27,24 @@ func init() {
Log.SetLogFuncCallDepth(Log.GetLogFuncCallDepth() + 1) Log.SetLogFuncCallDepth(Log.GetLogFuncCallDepth() + 1)
} }
func InitLog(logWay string, logFile string, logLevel string) { func InitLog(logWay string, logFile string, logLevel string, maxdays int64) {
SetLogFile(logWay, logFile) SetLogFile(logWay, logFile, maxdays)
SetLogLevel(logLevel) SetLogLevel(logLevel)
} }
// logWay: such as file or console // logWay: file or console
func SetLogFile(logWay string, logFile string) { func SetLogFile(logWay string, logFile string, maxdays int64) {
if logWay == "console" { if logWay == "console" {
Log.SetLogger("console", "") Log.SetLogger("console", "")
} else { } else {
Log.SetLogger("file", `{"filename": "`+logFile+`"}`) params := fmt.Sprintf(`{"filename": "%s", "maxdays": %d}`, logFile, maxdays)
Log.SetLogger("file", params)
} }
} }
// value: error, warning, info, debug // value: error, warning, info, debug
func SetLogLevel(logLevel string) { func SetLogLevel(logLevel string) {
level := 4 // warning level := 4 // warning
switch logLevel { switch logLevel {
case "error": case "error":
level = 3 level = 3
@ -56,7 +57,6 @@ func SetLogLevel(logLevel string) {
default: default:
level = 4 level = 4
} }
Log.SetLevel(level) Log.SetLevel(level)
} }