-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.read config with env (prod,test,dev).
2.fixed custom loger formater #37
- Loading branch information
yoyofx
committed
Aug 14, 2020
1 parent
08732bc
commit 92047b6
Showing
26 changed files
with
234 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package Env | ||
|
||
const ( | ||
Dev = "dev" | ||
Prod = "prod" | ||
Test = "test" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package xlog | ||
|
||
type ILogger interface { | ||
Debug(format string, a ...interface{}) | ||
Info(format string, a ...interface{}) | ||
Warning(format string, a ...interface{}) | ||
Error(format string, a ...interface{}) | ||
|
||
SetCustomLogFormat(logFormatterFunc func(logInfo LogInfo) string) | ||
SetDateFormat(format string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package xlog | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yoyofx/yoyogo/Abstractions/Platform/ConsoleColors" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
type XLogger struct { | ||
logger *log.Logger | ||
dateFormat string | ||
class string | ||
logFormatter func(LogInfo) string | ||
} | ||
|
||
var LoggerDefaultDateFormat = "2006/01/02 15:04:05.00" | ||
|
||
type LogLevel int | ||
|
||
// MessageLevel | ||
const ( | ||
NOTSET = iota | ||
DEBUG = LogLevel(10 * iota) // DEBUG = 10 | ||
INFO = LogLevel(10 * iota) // INFO = 20 | ||
WARNING = LogLevel(10 * iota) // WARNING = 30 | ||
ERROR = LogLevel(10 * iota) // ERROR = 40 | ||
) | ||
|
||
var LevelString = map[LogLevel]string{ | ||
DEBUG: "DEBUG", | ||
INFO: "INFO", | ||
WARNING: ConsoleColors.Yellow("WARNING"), | ||
ERROR: ConsoleColors.Red("ERROR"), | ||
} | ||
|
||
func defaultLogFormater(logInfo LogInfo) string { | ||
outLog := fmt.Sprintf(ConsoleColors.Yellow("[yoyogo] ")+"[%s] [%s] [%s] [%s] , %s", | ||
logInfo.StartTime, logInfo.Level, logInfo.Class, logInfo.Host, logInfo.Message) | ||
return outLog | ||
} | ||
|
||
func (log *XLogger) SetCustomLogFormat(logFormatterFunc func(logInfo LogInfo) string) { | ||
log.logFormatter = logFormatterFunc | ||
} | ||
|
||
func (log *XLogger) SetDateFormat(format string) { | ||
log.dateFormat = format | ||
} | ||
|
||
func NewXLogger() *XLogger { | ||
logger := NewLoggerWith(log.New(os.Stdout, "", 0)) | ||
return logger | ||
} | ||
|
||
func NewLoggerWith(log *log.Logger) *XLogger { | ||
logger := &XLogger{logger: log, dateFormat: LoggerDefaultDateFormat} | ||
logger.SetCustomLogFormat(defaultLogFormater) | ||
return logger | ||
} | ||
|
||
func GetXLogger(class string) ILogger { | ||
logger := NewXLogger() | ||
logger.class = class | ||
return logger | ||
} | ||
|
||
func (log *XLogger) log(level LogLevel, format string, a ...interface{}) { | ||
hostName, _ := os.Hostname() | ||
message := format | ||
if len(a[0].([]interface{})) > 0 { | ||
message = fmt.Sprintf(format, a...) | ||
} | ||
|
||
start := time.Now() | ||
info := LogInfo{ | ||
StartTime: start.Format(log.dateFormat), | ||
Level: LevelString[level], | ||
Class: log.class, | ||
Host: hostName, | ||
Message: message, | ||
} | ||
log.logger.Println(log.logFormatter(info)) | ||
} | ||
|
||
func (log *XLogger) Debug(format string, a ...interface{}) { | ||
log.log(DEBUG, format, a) | ||
} | ||
|
||
func (log *XLogger) Info(format string, a ...interface{}) { | ||
log.log(INFO, format, a) | ||
} | ||
|
||
func (log *XLogger) Warning(format string, a ...interface{}) { | ||
log.log(WARNING, format, a) | ||
} | ||
|
||
func (log *XLogger) Error(format string, a ...interface{}) { | ||
log.log(ERROR, format, a) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package xlog | ||
|
||
type LogInfo struct { | ||
StartTime string | ||
Level string | ||
Class string | ||
Host string | ||
Message string | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
application: | ||
name: demo_dev | ||
metadata: "develop" | ||
server: | ||
address: ":8080" | ||
type: "fasthttp" | ||
address: ":8080" | ||
max_request_size: 2096157 | ||
static: | ||
patten: "/" | ||
webroot: "./Static" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
application: | ||
name: demo_prod | ||
metadata: "prod Env" | ||
server: | ||
address: ":8081" | ||
type: "fasthttp" | ||
address: ":8080" | ||
max_request_size: 2096157 | ||
static: | ||
patten: "/" | ||
webroot: "./Static" |
Oops, something went wrong.