From a528352e28d2e2da73b77636863266f4d4749461 Mon Sep 17 00:00:00 2001 From: Henry Camacho Date: Sun, 26 Mar 2017 17:33:40 -0500 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20LumberJack=20logging=20?= =?UTF-8?q?and=20=E2=80=94log=20argument=20like=20caddy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - I don’t think the way I am doing logging is correct, however it was the only way I could figure to get the package level customizations and flags I wanted while leveraging lumberjack. - altered tunnel end point interface to AJ’s specification in issue #18 - added an InitLogging function package level, that is called by main to init. --- html/admin/js/app.js | 4 +-- main.go | 40 ++++++++++++++++++++++----- rvpn/genericlistener/api_interface.go | 2 +- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/html/admin/js/app.js b/html/admin/js/app.js index 595f99c..3f31357 100644 --- a/html/admin/js/app.js +++ b/html/admin/js/app.js @@ -74,7 +74,7 @@ app.controller('statusController', function ($scope, $http) { console.log("statusController"); $scope.status_search = ""; - var api = '/api/com.daplie.rvpn/status' + var api = '/api/com.daplie.tunnel/status' $scope.updateView = function() { $http.get(api).then(function(response) { @@ -96,7 +96,7 @@ app.controller('serverController', function ($scope, $http) { $scope.servers_trigger_details = []; $scope.filtered - var api = '/api/com.daplie.rvpn/servers' + var api = '/api/com.daplie.tunnel/servers' $scope.updateView = function() { $http.get(api).then(function(response) { diff --git a/main.go b/main.go index 0ca1e81..53449ff 100644 --- a/main.go +++ b/main.go @@ -5,19 +5,24 @@ import ( "crypto/tls" "flag" "fmt" + "io/ioutil" "log" "os" "time" "github.com/spf13/viper" + "io" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/genericlistener" + lumberjack "gopkg.in/natefinch/lumberjack.v2" ) var ( loginfo *log.Logger logdebug *log.Logger logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + logfile = "stdout" argWssClientListener string argGenericBinding int argServerBinding string @@ -35,11 +40,37 @@ var ( serverName string ) +func init() { + flag.StringVar(&logfile, "log", logfile, "Log file (or stdout/stderr; empty for none)") +} + +var logoutput io.Writer + //Main -- main entry point func main() { flag.Parse() - loginfo = log.New(os.Stdout, "INFO: main: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: main:", logFlags) + switch logfile { + case "stdout": + logoutput = os.Stdout + case "stderr": + logoutput = os.Stderr + case "": + logoutput = ioutil.Discard + default: + logoutput = &lumberjack.Logger{ + Filename: logfile, + MaxSize: 100, + MaxAge: 120, + MaxBackups: 10, + } + } + + // send the output io.Writing to the other packages + genericlistener.InitLogging(logoutput) + + loginfo = log.New(logoutput, "INFO: main: ", logFlags) + logdebug = log.New(logoutput, "DEBUG: main:", logFlags) + viper.SetConfigName("go-rvpn-server") viper.AddConfigPath("./") err := viper.ReadInConfig() @@ -61,11 +92,6 @@ func main() { loginfo.Println("startup") - loginfo.Println(viper.Get("rvpn.genericlisteners")) - loginfo.Println(viper.Get("rvpn.domains")) - - fmt.Println("-=-=-=-=-=-=-=-=-=-=") - certbundle, err := tls.LoadX509KeyPair("certs/fullchain.pem", "certs/privkey.pem") if err != nil { loginfo.Println(err) diff --git a/rvpn/genericlistener/api_interface.go b/rvpn/genericlistener/api_interface.go index b788a83..19690ba 100644 --- a/rvpn/genericlistener/api_interface.go +++ b/rvpn/genericlistener/api_interface.go @@ -13,7 +13,7 @@ import ( ) const ( - endPointPrefix = "/api/com.daplie.rvpn/" + endPointPrefix = "/api/com.daplie.tunnel/" ) var connectionTable *Table