mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
added logLevel flag
This commit is contained in:
parent
ad90df7fe2
commit
09e331075a
18
main.go
18
main.go
@ -6,6 +6,7 @@ import (
|
||||
"github.com/foomo/ContentServer/server"
|
||||
"github.com/foomo/ContentServer/server/log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -24,6 +25,19 @@ var contentServer string
|
||||
|
||||
var protocol = flag.String("protocol", PROTOCOL_TCP, "what protocol to server for")
|
||||
var address = flag.String("address", "127.0.0.1:8081", "address to bind host:port")
|
||||
var logLevelOptions = []string{
|
||||
log.LOG_LEVEL_NAME_ERROR,
|
||||
log.LOG_LEVEL_NAME_RECORD,
|
||||
log.LOG_LEVEL_NAME_WARNING,
|
||||
log.LOG_LEVEL_NAME_NOTICE,
|
||||
log.LOG_LEVEL_NAME_DEBUG}
|
||||
|
||||
var logLevel = flag.String(
|
||||
"logLevel",
|
||||
log.LOG_LEVEL_NAME_RECORD,
|
||||
fmt.Sprintf(
|
||||
"one of %s",
|
||||
strings.Join(logLevelOptions, ", ")))
|
||||
|
||||
func exitUsage(code int) {
|
||||
fmt.Printf("Usage: %s http(s)://your-content-server/path/to/content.json\n", os.Args[0])
|
||||
@ -33,15 +47,15 @@ func exitUsage(code int) {
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log.SetLogLevel(log.LOG_LEVEL_DEBUG)
|
||||
if len(flag.Args()) == 1 {
|
||||
fmt.Println(*protocol, *address, flag.Arg(0))
|
||||
//server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php")
|
||||
log.SetLogLevel(log.GetLogLevelByName(*logLevel))
|
||||
switch *protocol {
|
||||
case PROTOCOL_TCP:
|
||||
server.RunSocketServer(flag.Arg(0), *address)
|
||||
break
|
||||
case PROTOCOL_HTTP:
|
||||
//server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php")
|
||||
fmt.Println("http server does not work yet - use tcp instead")
|
||||
break
|
||||
default:
|
||||
|
||||
@ -11,10 +11,15 @@ var numErrors int64 = 0
|
||||
|
||||
const (
|
||||
LOG_LEVEL_ERROR = 0
|
||||
LOG_LEVEL_NAME_ERROR = "error"
|
||||
LOG_LEVEL_RECORD = 1
|
||||
LOG_LEVEL_NAME_RECORD = "record"
|
||||
LOG_LEVEL_WARNING = 2
|
||||
LOG_LEVEL_NAME_WARNING = "warning"
|
||||
LOG_LEVEL_NOTICE = 3
|
||||
LOG_LEVEL_NAME_NOTICE = "notice"
|
||||
LOG_LEVEL_DEBUG = 4
|
||||
LOG_LEVEL_NAME_DEBUG = "debug"
|
||||
)
|
||||
|
||||
var prefices = map[int]string{
|
||||
@ -25,6 +30,23 @@ var prefices = map[int]string{
|
||||
LOG_LEVEL_DEBUG: "debug : ",
|
||||
}
|
||||
|
||||
var logLevelMap = map[string]int{
|
||||
LOG_LEVEL_NAME_ERROR: LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_NAME_RECORD: LOG_LEVEL_RECORD,
|
||||
LOG_LEVEL_NAME_WARNING: LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_NAME_NOTICE: LOG_LEVEL_NOTICE,
|
||||
LOG_LEVEL_NAME_DEBUG: LOG_LEVEL_DEBUG,
|
||||
}
|
||||
|
||||
func GetLogLevelByName(name string) int {
|
||||
if level, ok := logLevelMap[name]; ok {
|
||||
return level
|
||||
} else {
|
||||
return LOG_LEVEL_RECORD
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func log(msg string, level int) string {
|
||||
if level <= logLevel {
|
||||
prefix := time.Now().Format(time.RFC3339) + " " + prefices[level]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user