Skip to content

Commit

Permalink
v0.0.6 版本完成
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Mar 5, 2020
1 parent cc15fe8 commit 8ce70b6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 10 deletions.
7 changes: 5 additions & 2 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
## ✒ 未来版本的新特性 (Features in future version)

### v0.0.6
### v0.0.7
* 尝试丢弃标准库的 log 实现,自己实现日志的输出
* 支持关闭文件信息的获取,避免 runtime.Caller 方法的调用,提高性能

### v0.0.6
* 支持按照文件大小自动划分日志文件
* 修复 nextFilename 中随机数生成重复的问题,设置了纳秒时钟作为种子
* ~~结合上面几点,以 “并发、缓冲” 为特点进行设计,技术使用 writer 接口进行包装~~
> 取消这个特性是因为,经过实验,性能并没有改善多少,两个方案,一个是使用队列进行缓冲,
> 开启一个 Go 协程写出数据,一个是不缓冲,直接开启多个 Go 协程进行写出数据。
> 第一个方案中,性能反而下降了一倍,估计和内存分配有关,最重要的就是,如果队列还有缓冲数据,
> 但是程序崩了,就有可能导致数据丢失,日志往往就需要最新最后的数据,而丢失的也正是最新最后的数据,
> 所以这个方案直接否决。第二个方案中,性能几乎没有提升,而且导致日志输出的顺序不固定,也有可能丢失。
> 综合上述,取消这个并发化日志输出的特性。
* 支持按照文件大小自动划分日志文件
### v0.0.5
* 支持将日志输出到文件
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## ✒ 历史版本的特性介绍 (Features in old version)

### v0.0.6
> 此版本发布于 2020-03-05
* 支持按照文件大小自动划分日志文件
* 修复 nextFilename 中随机数生成重复的问题,设置了纳秒时钟作为种子

### v0.0.5
> 此版本发布于 2020-03-04
* 支持将日志输出到文件
Expand Down
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module your_project_name
go 1.14

require (
github.com/FishGoddess/logit v0.0.5
github.com/FishGoddess/logit v0.0.6
)
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module your_project_name
go 1.14

require (
github.com/FishGoddess/logit v0.0.5
github.com/FishGoddess/logit v0.0.6
)
```

Expand Down
17 changes: 17 additions & 0 deletions _examples/log_to_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit/wrapper"
)

func main() {
Expand All @@ -45,4 +46,20 @@ func main() {
// See logit.NewDurationRollingLogger.
logger = logit.NewDayRollingLogger("D:/", logit.DebugLevel)
logger.Info("Today is Friday!!!")

// NewSizeRollingLogger creates a file size rolling logger with given limitedSize.
// You should appoint a directory to store all log files generated in this time.
// Notice that limitedSize must not less than minLimitedSize (generally 64 KB), see wrapper.minLimitedSize.
// Check wrapper.KB, wrapper.MB, wrapper.GB to know what unit you gonna to use.
// Also, default filename of log file is like "20200304-145246-45.log", see nextFilename.
// If you want to appoint another filename, check this and do it by this way.
// See wrapper.NewSizeRollingFile (it is an implement of io.writer).
logger = logit.NewSizeRollingLogger("D:/", 64*wrapper.KB, logit.DebugLevel)
logger.Info("file size???")

// NewDayRollingLogger creates a file size rolling logger.
// You should appoint a directory to store all log files generated in this time.
// Default means limitedSize is 64 MB. See NewSizeRollingLogger.
logger = logit.NewDefaultSizeRollingLogger("D:/", logit.DebugLevel)
logger.Info("64 MB rolling!!!")
}
18 changes: 17 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,24 @@ Package logit provides an easy way to use foundation for your logging operations
logger = logit.NewDayRollingLogger("D:/", logit.DebugLevel)
logger.Info("Today is Friday!!!")
// NewSizeRollingLogger creates a file size rolling logger with given limitedSize.
// You should appoint a directory to store all log files generated in this time.
// Notice that limitedSize must not less than minLimitedSize (generally 64 KB), see wrapper.minLimitedSize.
// Check wrapper.KB, wrapper.MB, wrapper.GB to know what unit you gonna to use.
// Also, default filename of log file is like "20200304-145246-45.log", see nextFilename.
// If you want to appoint another filename, check this and do it by this way.
// See wrapper.NewSizeRollingFile (it is an implement of io.writer).
logger = logit.NewSizeRollingLogger("D:/", 64*wrapper.KB, logit.DebugLevel)
logger.Info("file size???")
// NewDayRollingLogger creates a file size rolling logger.
// You should appoint a directory to store all log files generated in this time.
// Default means limitedSize is 64 MB. See NewSizeRollingLogger.
logger = logit.NewDefaultSizeRollingLogger("D:/", logit.DebugLevel)
logger.Info("64 MB rolling!!!")
*/
package logit // import "github.com/FishGoddess/logit"

// Version is the version string representation of the "logit" package.
const Version = "0.0.5"
const Version = "0.0.6"
30 changes: 25 additions & 5 deletions logger_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ import (
// PrefixOfLogFile is the prefix of log file.
const PrefixOfLogFile = ".log"

// random is a generator for random number.
var random = rand.New(rand.NewSource(time.Now().UnixNano()))

// nextFilename creates a time-relative filename with given now time.
// Also, it uses random number to ensure this filename is available.
// The filename will be like "20200304-145246-45.log".
// Notice that directory stores all log files generated in this time.
func nextFilename(directory string) func(now time.Time) string {
return func(now time.Time) string {
name := now.Format("20060102-150405") + "-" + strconv.Itoa(rand.Intn(100)) + PrefixOfLogFile
name := now.Format("20060102-150405") + "-" + strconv.Itoa(random.Intn(1000)) + PrefixOfLogFile
return path.Join(directory, name)
}
}
Expand All @@ -58,13 +61,12 @@ func NewFileLogger(logFile string, level LoggerLevel) *Logger {

// NewDurationRollingLogger creates a duration rolling logger with given duration.
// You should appoint a directory to store all log files generated in this time.
// Notice that duration must not less than minDuration (generally time.Second), see wrapper.minDuration.
// Also, default filename of log file is like "20200304-145246-45.log", see wrapper.NewFilename.
// Notice that duration must not less than minDuration (generally one second), see wrapper.minDuration.
// Also, default filename of log file is like "20200304-145246-45.log", see nextFilename.
// If you want to appoint another filename, check this and do it by this way.
// See wrapper.NewDurationRollingFile (it is an implement of io.writer).
func NewDurationRollingLogger(directory string, duration time.Duration, level LoggerLevel) *Logger {
file := wrapper.NewDurationRollingFile(duration, nextFilename(directory))
return NewLogger(file, level)
return NewLogger(wrapper.NewDurationRollingFile(duration, nextFilename(directory)), level)
}

// NewDayRollingLogger creates a day rolling logger.
Expand All @@ -73,3 +75,21 @@ func NewDurationRollingLogger(directory string, duration time.Duration, level Lo
func NewDayRollingLogger(directory string, level LoggerLevel) *Logger {
return NewDurationRollingLogger(directory, 24*time.Hour, level)
}

// NewSizeRollingLogger creates a file size rolling logger with given limitedSize.
// You should appoint a directory to store all log files generated in this time.
// Notice that limitedSize must not less than minLimitedSize (generally 64 KB), see wrapper.minLimitedSize.
// Check wrapper.KB, wrapper.MB, wrapper.GB to know what unit you gonna to use.
// Also, default filename of log file is like "20200304-145246-45.log", see nextFilename.
// If you want to appoint another filename, check this and do it by this way.
// See wrapper.NewSizeRollingFile (it is an implement of io.writer).
func NewSizeRollingLogger(directory string, limitedSize int64, level LoggerLevel) *Logger {
return NewLogger(wrapper.NewSizeRollingFile(limitedSize, nextFilename(directory)), level)
}

// NewDayRollingLogger creates a file size rolling logger.
// You should appoint a directory to store all log files generated in this time.
// Default means limitedSize is 64 MB. See NewSizeRollingLogger.
func NewDefaultSizeRollingLogger(directory string, level LoggerLevel) *Logger {
return NewSizeRollingLogger(directory, 64*wrapper.MB, level)
}
26 changes: 26 additions & 0 deletions logger_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package logit
import (
"testing"
"time"

"github.com/FishGoddess/logit/wrapper"
)

// 测试创建标准输出的日志记录器
Expand Down Expand Up @@ -66,3 +68,27 @@ func TestNewDayRollingLogger(t *testing.T) {
time.Sleep(time.Second)
logger.Info("2. info!!!!!!!!%d", time.Now().Unix())
}

// 测试按照文件大小自动划分日志文件的日志记录器
func TestNewSizeRollingLogger(t *testing.T) {

logger := NewSizeRollingLogger("Z:/", 64*wrapper.KB, DebugLevel)
for i := 0; i < 1000; i++ {
logger.Debug("debug...%d", i)
logger.Info("info...%d", i)
logger.Warn("warn...%d", i)
logger.Error("error...%d", i)
}
}

// 测试按照默认文件大小自动划分日志文件的日志记录器
func TestNewDefaultSizeRollingLogger(t *testing.T) {

logger := NewDefaultSizeRollingLogger("Z:/", DebugLevel)
for i := 0; i < 1000; i++ {
logger.Debug("debug...%d", i)
logger.Info("info...%d", i)
logger.Warn("warn...%d", i)
logger.Error("error...%d", i)
}
}

0 comments on commit 8ce70b6

Please sign in to comment.