-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d6da78
commit d54fa1c
Showing
10 changed files
with
360 additions
and
53 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
Empty file.
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,23 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/pingguoxueyuan/gostudy/listen17/log" | ||
"time" | ||
|
||
"github.com/pingguoxueyuan/gostudy/logger" | ||
) | ||
|
||
func main() { | ||
/* | ||
file := log.NewFileLog("c:/a.log") | ||
file.LogDebug("this is a debug log") | ||
file.LogWarn("this is a warn log") | ||
*/ | ||
func initLogger(name, logPath, logName string, level string) (err error) { | ||
m := make(map[string]string, 8) | ||
m["log_path"] = logPath | ||
m["log_name"] = "user_server" | ||
m["log_level"] = level | ||
err = logger.InitLogger(name, m) | ||
if err != nil { | ||
return | ||
} | ||
|
||
logger.Debug("init logger success") | ||
return | ||
} | ||
|
||
/* | ||
console := log.NewConsoleLog("xxxx") | ||
console.LogConsoleDebug("this is a console log") | ||
console.LogConsoleWarn("this is a warn log") | ||
*/ | ||
//log := log.NewFileLog("c:/a.log") | ||
log := log.NewConsoleLog("xxxx") | ||
log.LogDebug("this is a debug file") | ||
log.LogWarn("this is a warn log") | ||
func Run() { | ||
for { | ||
logger.Debug("user server is running") | ||
time.Sleep(time.Second) | ||
} | ||
} | ||
|
||
func main() { | ||
initLogger("file", "c:/logs/", "user_server", "debug") | ||
Run() | ||
return | ||
} |
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,98 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type ConsoleLogger struct { | ||
level int | ||
} | ||
|
||
func NewConsoleLogger(config map[string]string) (log LogInterface, err error) { | ||
logLevel, ok := config["log_level"] | ||
if !ok { | ||
err = fmt.Errorf("not found log_level ") | ||
return | ||
} | ||
|
||
level := getLogLevel(logLevel) | ||
log = &ConsoleLogger{ | ||
level: level, | ||
} | ||
return | ||
} | ||
|
||
func (c *ConsoleLogger) Init() { | ||
|
||
} | ||
|
||
func (c *ConsoleLogger) SetLevel(level int) { | ||
if level < LogLevelDebug || level > LogLevelFatal { | ||
level = LogLevelDebug | ||
} | ||
|
||
c.level = level | ||
} | ||
|
||
func (c *ConsoleLogger) Debug(format string, args ...interface{}) { | ||
if c.level > LogLevelDebug { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelDebug, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
|
||
func (c *ConsoleLogger) Trace(format string, args ...interface{}) { | ||
if c.level > LogLevelTrace { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelTrace, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
func (c *ConsoleLogger) Info(format string, args ...interface{}) { | ||
if c.level > LogLevelInfo { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelInfo, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
|
||
func (c *ConsoleLogger) Warn(format string, args ...interface{}) { | ||
if c.level > LogLevelWarn { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelWarn, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
|
||
func (c *ConsoleLogger) Error(format string, args ...interface{}) { | ||
if c.level > LogLevelError { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelError, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
func (c *ConsoleLogger) Fatal(format string, args ...interface{}) { | ||
if c.level > LogLevelFatal { | ||
return | ||
} | ||
|
||
logData := writeLog(LogLevelFatal, format, args...) | ||
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr, | ||
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message) | ||
} | ||
|
||
func (c *ConsoleLogger) Close() { | ||
|
||
} |
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
Oops, something went wrong.