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
2 parents e00ce27 + 8ce70b6 commit d985882
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 46 deletions.
13 changes: 11 additions & 2 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
## ✒ 未来版本的新特性 (Features in future version)

### v0.0.6
### v0.0.7
* 尝试丢弃标准库的 log 实现,自己实现日志的输出
* 支持关闭文件信息的获取,避免 runtime.Caller 方法的调用,提高性能
* 结合上面几点,以 “并发、缓冲” 为特点进行设计,技术使用 writer 接口进行包装

### 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
7 changes: 5 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

* Level-based logging, and there are four levels to use
* Enable or disable Logger, you can disable or switch to a higher level in your production environment
* Log file supports, and it will split the file by duration automatically.
* Log file supports, and you can customer the name of your log file.
* Duration rolling supports, which means it will roll to a new log file by duration automatically, such as one day one log file.
* File size rolling supports, which means it will roll to a new log file by file size automatically, such as one 64 MB one log file.

_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to get more information._

Expand All @@ -32,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 Expand Up @@ -74,6 +76,7 @@ func main() {
* [enable_disable](./_examples/enable_disable.go)
* [change_log_level](./_examples/change_log_level.go)
* [log_to_file](./_examples/log_to_file.go)
* [wrapper](./_examples/wrapper.go)

_Check more examples in [_examples](./_examples)._

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

* 支持日志级别控制,目前一共有四个日志级别
* 支持开启或者关闭日志功能,线上环境可以关闭或调高日志级别
* 支持记录日志到文件中,并且可以按照时间间隔进行自动分割
* 支持记录日志到文件中,自定义日志文件名
* 支持按照时间间隔进行自动划分日志文件,比如每一天划分一个日志文件
* 支持按照文件大小进行自动划分日志文件,比如每 64 MB 划分一个日志文件

_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_

Expand All @@ -32,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 Expand Up @@ -74,6 +76,7 @@ func main() {
* [enable_disable](./_examples/enable_disable.go)
* [change_log_level](./_examples/change_log_level.go)
* [log_to_file](./_examples/log_to_file.go)
* [wrapper](./_examples/wrapper.go)

_更多使用案例请查看 [_examples](./_examples) 目录。_

Expand Down
23 changes: 23 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 @@ -39,4 +40,26 @@ func main() {
// See wrapper.NewDurationRollingFile (it is an implement of io.writer).
logger = logit.NewDurationRollingLogger("D:/", 24*time.Hour, logit.DebugLevel)
logger.Info("Rolling!!!")

// NewDayRollingLogger creates a day rolling logger.
// You should appoint a directory to store all log files generated in this time.
// 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!!!")
}
48 changes: 48 additions & 0 deletions _examples/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 Ye Zi Jie. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: fish
// Email: [email protected]
// Created at 2020/03/05 17:30:21

package main

import (
"time"

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

func main() {

// 1. DurationRollingFile is a time sensitive file.
durationRollingFile := wrapper.NewDurationRollingFile(24*time.Hour, func(now time.Time) string {
return "D:/" + now.Format("20060102-150405") + ".txt"
})
defer durationRollingFile.Close()

// You can use it like using os.File!
durationRollingFile.Write([]byte("Hello!"))

// =================================================================================

// 2. SizeRollingFile is a file size sensitive file.
sizeRollingFile := wrapper.NewSizeRollingFile(64*wrapper.KB, func(now time.Time) string {
return "D:/" + now.Format("20060102150405.000") + ".log"
})
defer sizeRollingFile.Close()

// You can use it like using os.File!
sizeRollingFile.Write([]byte("Hello!"))
}
24 changes: 23 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,30 @@ Package logit provides an easy way to use foundation for your logging operations
logger = logit.NewDurationRollingLogger("D:/", time.Second, logit.DebugLevel)
logger.Info("Rolling!!!")
// NewDayRollingLogger creates a day rolling logger.
// You should appoint a directory to store all log files generated in this time.
// 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!!!")
*/
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"
53 changes: 47 additions & 6 deletions logger_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@
package logit

import (
"math/rand"
"os"
"path"
"strconv"
"time"

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

// 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(random.Intn(1000)) + PrefixOfLogFile
return path.Join(directory, name)
}
}

// NewStdoutLogger returns a Logger holder with given logger level.
func NewStdoutLogger(level LoggerLevel) *Logger {
return NewLogger(os.Stdout, level)
Expand All @@ -42,13 +61,35 @@ 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, func(now time.Time) string {
return path.Join(directory, wrapper.NewFilename(now))
})
return NewLogger(file, level)
return NewLogger(wrapper.NewDurationRollingFile(duration, nextFilename(directory)), level)
}

// NewDayRollingLogger creates a day rolling logger.
// You should appoint a directory to store all log files generated in this time.
// See NewDurationRollingLogger.
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)
}
39 changes: 38 additions & 1 deletion 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 All @@ -47,11 +49,46 @@ func TestNewFileLogger(t *testing.T) {
logger = NewFileLogger("https://test.io", DebugLevel)
}

// 创建随时间间隔滚动的文件日志记录器
// 测试创建随时间间隔滚动的文件日志记录器
func TestNewDurationRollingLogger(t *testing.T) {

logger := NewDurationRollingLogger("Z:/", time.Second, DebugLevel)
for i := 0; i < 10; i++ {
logger.Info("1. info!!!!!!!!%d", time.Now().Unix())
time.Sleep(time.Second)
logger.Info("2. info!!!!!!!!%d", time.Now().Unix())
}
}

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

logger := NewDayRollingLogger("Z:/", DebugLevel)
logger.Info("1. info!!!!!!!!%d", time.Now().Unix())
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)
}
}
15 changes: 13 additions & 2 deletions wrapper/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ Package wrapper provides some writers to extend your Logger.
// DurationRollingFile is a time sensitive file.
file := NewDurationRollingFile(24 * time.Hour, func(now time.Time) string {
return "D:/" + currentTime.Format("20060102-150405") + ".txt"
return "D:/" + now.Format("20060102-150405") + ".txt"
})
defer file.Close()
// You can use it like using os.File!
file.Write([]byte("Hello!")
file.Write([]byte("Hello!"))
2. SizeRollingFile:
// SizeRollingFile is a file size sensitive file.
file := NewSizeRollingFile(64*KB, func (now time.Time) string {
return "D:/" + now.Format("20060102150405.000") + ".log"
})
defer file.Close()
// You can use it like using os.File!
file.Write([]byte("Hello!"))
*/
package wrapper // import "github.com/FishGoddess/logit/wrapper"
Loading

0 comments on commit d985882

Please sign in to comment.