Skip to content

Commit d977e97

Browse files
committed
develop successful v1.1.6🚀
1 parent 610c087 commit d977e97

12 files changed

+425
-101
lines changed

async.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2020 HigKer
2+
// Open Source: MIT License
3+
// Author: SDing <deen.job@qq.com>
4+
// Date: 2020/5/1 - 12:53 上午
5+
6+
package logker
7+
8+
9+
const (
10+
// Log message format wildcards
11+
// {level} == logging level
12+
// {time} == time
13+
// {position} == runtime caller info
14+
// {message} == logging message
15+
// OutPut: [INFO] 2006-01-02 13:05.0006 MP - Position: test.go|main.test:21 - Message: news
16+
DefaultFormat = "{level} - Date: {time} {position} - Message: {message}" //This was modified for v 1.1.5
17+
)
18+
19+
type Async interface {
20+
sendMsg(msg *message)
21+
begin()
22+
}
23+
24+
type AsyncTask struct {
25+
queueSize int64
26+
logQueue chan *message
27+
}
28+
29+
// Initializes The Producer Channel Buffer
30+
func InitAsync(queueSize int64) *AsyncTask {
31+
return &AsyncTask{queueSize: queueSize, logQueue: make(chan *message, queueSize)}
32+
}

async_console.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2020 HigKer
2+
// Open Source: MIT License
3+
// Author: SDing <deen.job@qq.com>
4+
// Date: 2020/4/30 - 9:16 下午
5+
6+
package logker
7+
8+
import (
9+
"github.com/fatih/color"
10+
"strings"
11+
)
12+
13+
// Channel Buffer Size
14+
const (
15+
_ = iota
16+
Qs1w = 10000 * iota
17+
Qs2w
18+
Qs3w
19+
Qs4w
20+
Qs5w
21+
Qs6w
22+
)
23+
24+
// send pack msg to log queue
25+
func (c *console) sendMsg(msg *message) {
26+
c.asyncTask.logQueue <- msg
27+
}
28+
29+
func (c *console) begin() {
30+
go c.asyncOutPutTask()
31+
}
32+
33+
func (c *console) asyncOutPutTask() {
34+
for {
35+
msg := <-c.asyncTask.logQueue
36+
switch msg.level {
37+
case DEBUG:
38+
// blue color of log message.
39+
// format log message output console.
40+
color.Blue(c.parsePacket(msg))
41+
case INFO:
42+
color.Green(c.parsePacket(msg))
43+
case WARNING:
44+
color.Yellow(c.parsePacket(msg))
45+
case ERROR:
46+
color.Red(c.parsePacket(msg))
47+
default:
48+
// Log Level Type Error
49+
// Program automatically set to debug
50+
color.Red("-----------------------------------------------------------------")
51+
color.Red("!!!Log Level Type Error,Program automatically set to debug!!!")
52+
color.Red("-----------------------------------------------------------------")
53+
c.logLevel = DEBUG
54+
// recursion
55+
c.sendMsg(c.pack(ERROR,"Log Level Type Error,Program automatically set to debug!!!"))
56+
}
57+
}
58+
}
59+
60+
// ReplaceOurCustomMessageFormatIdentifier
61+
// This function was added at 23:50:28 on April 19, 2020 in v1.1.5
62+
func (c *console) parsePacket(msg *message) string {
63+
str := msg.format
64+
str = strings.Replace(str, "{level}", msg.level.toStr(), -1)
65+
str = strings.Replace(str, "{time}", msg.logTime, -1)
66+
str = strings.Replace(str, "{position}", msg.caller, -1)
67+
str = strings.Replace(str, "{message}", msg.msg, -1)
68+
return str + "\n"
69+
}
File renamed without changes.

async_file.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2020 HigKer
2+
// Open Source: MIT License
3+
// Author: SDing <deen.job@qq.com>
4+
// Date: 2020/5/1 - 1:04 上午
5+
6+
package logker
7+
8+
import (
9+
"strings"
10+
)
11+
12+
const (
13+
// Log file size
14+
// You can also customize. Unit is kb.
15+
MB10 int64 = 10 * 1024 * 1024
16+
MB100 int64 = 100 * 1024 * 1024
17+
GB1 int64 = 10 * MB100
18+
)
19+
20+
// send pack msg to log queue
21+
func (f *fileLog) sendMsg(msg *message) {
22+
f.asyncTask.logQueue <- msg
23+
}
24+
25+
func (f *fileLog) begin() {
26+
go f.asyncOutPutTask()
27+
}
28+
29+
func (f *fileLog) asyncOutPutTask() {
30+
for {
31+
msg := <-f.asyncTask.logQueue
32+
if f.checkSize() {
33+
// division file
34+
f.divisionLogFile(plain)
35+
}
36+
_, _ = f.file.WriteString(f.parsePacket(msg))
37+
// 如果是error单独文件开启就执行
38+
// zh_CN: 检测error独立文件输出开关是否开启 如果开启就往error独立文件输出内容
39+
// usa_EN: Check whether the error independent file output switch is turned on.
40+
// If it is turned on, output the content to the error independent file
41+
if msg.level == ERROR {
42+
if f.checkErrSize() {
43+
// division error file
44+
f.divisionLogFile(major)
45+
}
46+
if f.isEnableErr() {
47+
_, _ = f.errFile.WriteString(f.parsePacket(msg))
48+
}
49+
}
50+
}
51+
}
52+
53+
// ReplaceOurCustomMessageFormatIdentifier
54+
// This function was added at 23:50:28 on April 19, 2020 in v1.1.5
55+
func (f *fileLog) parsePacket(msg *message) string {
56+
str := msg.format
57+
str = strings.Replace(str, "{level}", msg.level.toStr(), -1)
58+
str = strings.Replace(str, "{time}", msg.logTime, -1)
59+
str = strings.Replace(str, "{position}", msg.caller, -1)
60+
str = strings.Replace(str, "{message}", msg.msg, -1)
61+
return str + "\n"
62+
}

async_record.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

caller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// Runtime caller skip
1515
const (
16-
SKIP = 2
16+
SKIP = 1
1717
)
1818

1919
func buildCallerStr(skip int) string {

console.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"fmt"
1111
"strings"
12-
"testing"
1312
)
1413

1514
// console Logger
@@ -27,24 +26,7 @@ type console struct {
2726
asyncTask *AsyncTask
2827
}
2928

30-
// Build console Logger
31-
func NewClog(lev level, zone logTimeZone, formatting string, at *AsyncTask, t *testing.T) (Logger, error) {
32-
consoleLog := &console{
33-
// Level: logger Level
34-
logLevel: lev,
35-
// Zone : logger Time Zone
36-
timeZone: zone,
37-
tz: nil,
38-
asyncTask: at,
39-
}
40-
if err := verify(formatting); err != nil {
41-
return nil, err
42-
}
43-
consoleLog.formatting = formatting
44-
consoleLog.initTime()
45-
consoleLog.asyncTask.begin(t)
46-
return consoleLog, nil
47-
}
29+
4830

4931
func (c *console) initTime() {
5032
// set customize time zone
@@ -59,22 +41,22 @@ func (c *console) isEnableLevel(lev level) bool {
5941

6042
func (c *console) Info(value string, args ...interface{}) {
6143
if c.isEnableLevel(INFO) {
62-
c.asyncTask.sendMsg(c.pack(INFO, fmt.Sprintf(value, args...)))
44+
c.sendMsg(c.pack(INFO, fmt.Sprintf(value, args...)))
6345
}
6446
}
6547
func (c *console) Debug(value string, args ...interface{}) {
6648
if c.isEnableLevel(DEBUG) {
67-
c.asyncTask.sendMsg(c.pack(DEBUG, fmt.Sprintf(value, args...)))
49+
c.sendMsg(c.pack(DEBUG, fmt.Sprintf(value, args...)))
6850
}
6951
}
7052
func (c *console) Error(value string, args ...interface{}) {
7153
if c.isEnableLevel(ERROR) {
72-
c.asyncTask.sendMsg(c.pack(ERROR, fmt.Sprintf(value, args...)))
54+
c.sendMsg(c.pack(ERROR, fmt.Sprintf(value, args...)))
7355
}
7456
}
7557
func (c *console) Warning(value string, args ...interface{}) {
7658
if c.isEnableLevel(WARNING) {
77-
c.asyncTask.sendMsg(c.pack(WARNING, fmt.Sprintf(value, args...)))
59+
c.sendMsg(c.pack(WARNING, fmt.Sprintf(value, args...)))
7860
}
7961
}
8062

console_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func TestVerifyString(t *testing.T) {
1818

1919
func TestConsole_Debug(t *testing.T) {
2020
defaults := "{level} - Date: {time} {position} - Message: {message}"
21-
task := InitAsyncTask(Qs1w)
22-
logger, e := NewClog(ERROR, Shanghai, defaults, task, t)
21+
task := InitAsync(Qs1w)
22+
logger, e := NewClog(ERROR, Shanghai, defaults, task)
2323
if e != nil {
2424
t.Log(e)
2525
}

0 commit comments

Comments
 (0)