Skip to content

Commit

Permalink
增加日志库
Browse files Browse the repository at this point in the history
  • Loading branch information
pingguoxueyuan committed Apr 1, 2018
1 parent 6d6da78 commit d54fa1c
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 53 deletions.
1 change: 1 addition & 0 deletions listen15/homework/calc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func transPostExpress(express string) (postExpress []string, err error) {
LABEL:
for i < len(express) {
switch {
//35+27
case express[i] >= '0' && express[i] <= '9':
var number []byte
for ; i < len(express); i++ {
Expand Down
Empty file removed listen15/test.go
Empty file.
43 changes: 27 additions & 16 deletions listen17/user_server/main.go
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
}
98 changes: 98 additions & 0 deletions logger/console.go
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() {

}
18 changes: 18 additions & 0 deletions logger/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ func getLevelText(level int) string {
}
return "UNKNOWN"
}

func getLogLevel(level string) int {
switch level {
case "debug":
return LogLevelDebug
case "trace":
return LogLevelTrace
case "info":
return LogLevelInfo
case "warn":
return LogLevelWarn
case "error":
return LogLevelError
case "fatal":
return LogLevelFatal
}
return LogLevelDebug
}
144 changes: 108 additions & 36 deletions logger/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,62 @@ package logger
import (
"fmt"
"os"
"path"
"time"
"strconv"
)

//2018/3/26 0:01.383 DEBUG logDebug.go:29 this is a debug log
//2006-01-02 15:04:05.999
type FileLogger struct {
level int
logPath string
logName string
file *os.File
warnFile *os.File
level int
logPath string
logName string
file *os.File
warnFile *os.File
LogDataChan chan *LogData
}

func NewFileLogger(level int, logPath, logName string) LogInterface {
logger := &FileLogger{
level: level,
logPath: logPath,
logName: logName,
func NewFileLogger(config map[string]string) (log LogInterface, err error) {
logPath, ok := config["log_path"]
if !ok {
err = fmt.Errorf("not found log_path ")
return
}

logName, ok := config["log_name"]
if !ok {
err = fmt.Errorf("not found log_name ")
return
}

logLevel, ok := config["log_level"]
if !ok {
err = fmt.Errorf("not found log_level ")
return
}

logChanSize, ok := config["log_chan_size"]
if !ok {
logChanSize = "50000"
}

chanSize, err := strconv.Atoi(logChanSize)
if err != nil {
chanSize = 50000
}

level := getLogLevel(logLevel)
log = &FileLogger{
level: level,
logPath: logPath,
logName: logName,
LogDataChan: make(chan *LogData, chanSize),
}

logger.init()
return logger
log.Init()
return
}

func (f *FileLogger) init() {
func (f *FileLogger) Init() {

filename := fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
Expand All @@ -46,6 +76,19 @@ func (f *FileLogger) init() {
}

f.warnFile = file
go f.writeLogBackground()
}

func (f *FileLogger) writeLogBackground() {
for logData := range f.LogDataChan {
var file *os.File = f.file
if logData.WarnAndFatal {
file = f.warnFile
}

fmt.Fprintf(file, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
}

func (f *FileLogger) SetLevel(level int) {
Expand All @@ -55,45 +98,74 @@ func (f *FileLogger) SetLevel(level int) {
f.level = level
}

func (f *FileLogger) writeLog(file *os.File, level int, format string, args ...interface{}) {
if f.level > level {
func (f *FileLogger) Debug(format string, args ...interface{}) {
if f.level > LogLevelDebug {
return
}

now := time.Now()
nowStr := now.Format("2006-01-02 15:04:05.999")
levelStr := getLevelText(level)

fileName, funcName, lineNo := GetLineInfo()
fileName = path.Base(fileName)
funcName = path.Base(funcName)
msg := fmt.Sprintf(format, args...)

fmt.Fprintf(file, "%s %s (%s:%s:%d) %s\n", nowStr, levelStr, fileName, funcName, lineNo, msg)
}

func (f *FileLogger) Debug(format string, args ...interface{}) {
f.writeLog(f.file, LogLevelDebug, format, args...)
logData := writeLog(LogLevelDebug, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Trace(format string, args ...interface{}) {
f.writeLog(f.file, LogLevelTrace, format, args...)
if f.level > LogLevelTrace {
return
}
logData := writeLog(LogLevelTrace, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Info(format string, args ...interface{}) {
f.writeLog(f.file, LogLevelInfo, format, args...)
if f.level > LogLevelInfo {
return
}
logData := writeLog(LogLevelInfo, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Warn(format string, args ...interface{}) {
f.writeLog(f.warnFile, LogLevelWarn, format, args...)
if f.level > LogLevelWarn {
return
}

logData := writeLog(LogLevelWarn, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Error(format string, args ...interface{}) {
f.writeLog(f.warnFile, LogLevelError, format, args...)
if f.level > LogLevelError {
return
}

logData := writeLog(LogLevelError, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Fatal(format string, args ...interface{}) {
f.writeLog(f.warnFile, LogLevelFatal, format, args...)
if f.level > LogLevelFatal {
return
}

logData := writeLog(LogLevelFatal, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}

func (f *FileLogger) Close() {
Expand Down
1 change: 1 addition & 0 deletions logger/log_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

type LogInterface interface {
Init()
SetLevel(level int)
Debug(format string, args ...interface{})
Trace(format string, args ...interface{})
Expand Down
Loading

0 comments on commit d54fa1c

Please sign in to comment.