Commit f93a72c8 by yemin

no message

parents
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/logger.iml" filepath="$PROJECT_DIR$/.idea/logger.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
module "git.168cad.top/logger"
\ No newline at end of file
package log
import (
"bytes"
"fmt"
"runtime"
"strconv"
"strings"
"time"
)
var LogLevel = "DEBUG"
var LogColorful = true
var LogResponseLength int
var LogShowSql = true
var LogIndexNamePrefix = ""
const (
LevelDebug = "Debug"
LevelInfo = "Info"
LevelWarn = "Warn"
LevelError = "Error"
)
func DebugDo(f func()) {
if IsDebug() {
f()
}
}
func IsDebug() bool {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
}
return ok
}
func Debugf(format string, msg ...interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelDebug, file, line, fmt.Sprintf(format, msg...))
}
}
func Debug(msg interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelDebug, file, line, msg)
}
}
func Infof(format string, msg ...interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelInfo, file, line, fmt.Sprintf(format, msg...))
}
}
func Info(msg interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelInfo, file, line, msg)
}
}
func Warnf(format string, msg ...interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
case strings.ToLower(LevelWarn):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelWarn, file, line, fmt.Sprintf(format, msg...))
}
}
func Warn(msg interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
case strings.ToLower(LevelWarn):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelWarn, file, line, msg)
}
}
func Errorf(format string, msg ...interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
case strings.ToLower(LevelWarn):
ok = true
break
case strings.ToLower(LevelError):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelError, file, line, fmt.Sprintf(format, msg...))
}
}
func Error(msg interface{}) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(LevelInfo):
ok = true
break
case strings.ToLower(LevelWarn):
ok = true
break
case strings.ToLower(LevelError):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
print(LevelError, file, line, msg)
}
}
// Colors
const (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
magenta = "\033[35m"
cyan = "\033[36m"
white = "\033[37m"
blueBold = "\033[34;1m"
magentaBold = "\033[35;1m"
redBold = "\033[31;1m"
yellowBold = "\033[33;1m"
)
func print(tag, file string, line int, msg interface{}) {
str := "%s %s [%d] %s %d %s"
if LogColorful {
switch tag {
case LevelDebug:
str = "%s %s [%d] %s %d " + green + "%s" + reset
break
case LevelInfo:
str = "%s %s [%d] %s %d " + yellow + "%s" + reset
break
case LevelWarn:
str = "%s %s [%d] %s %d " + magenta + "%s" + reset
break
case LevelError:
str = "%s %s [%d] %s %d " + red + "%s" + reset
break
}
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, formatDateMillTime(time.Now()), tag, getGID(), file, line, msg))
}
func formatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
package main
func main() {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment