Skip to content

Commit

Permalink
新增支持路径设置选项
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderivan committed Jan 9, 2019
1 parent 419960d commit 11d39e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ convenient log package

// 配置logger,如果不配置时默认为控制台输出,等级为DEBG
logger.SetLogger(`{"Console": {"level": "DEBG"}`)
// 配置详细见 下文
// 配置说明见下文

// 设置完成后,即可在控制台和日志文件app.log中看到如下输出
logger.Trace("this is Trace")
Expand Down Expand Up @@ -45,7 +45,7 @@ logger当前支持控制台、文件、网络3种方式适配器输出,可以
通过调用logger.SetLogger(config string)方法设置参数,config支持json配置,也支持指定内容为json配置的文件路径,例如:
```go
// 通过配置参数直接配置
logger.SetLogger(`{"Console": {"level": "DEBG"}`)
logger.SetLogger(`{"Console": {"level": "DEBG"}}`)
// 通过配置文件配置
logger.SetLogger("/home/log.json")

Expand Down
17 changes: 16 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type LocalLogger struct {
appName string
callDepth int
timeFormat string
usePath string
}

func NewLogger(depth ...int) *LocalLogger {
Expand Down Expand Up @@ -190,6 +191,11 @@ func (this *LocalLogger) DelLogger(adapterName string) error {
return nil
}

// 设置日志起始路径
func (this *LocalLogger) SetLogPathTrim(trimPath string) {
this.usePath = trimPath
}

func (this *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level int) {
for _, l := range this.outputs {
if l.name == AdapterConn {
Expand Down Expand Up @@ -220,9 +226,14 @@ func (this *LocalLogger) writeMsg(logLevel int, msg string, v ...interface{}) er
}
when := time.Now()
_, file, lineno, ok := runtime.Caller(this.callDepth)
var strim string = "src/"
if this.usePath != "" {
strim = this.usePath
}
if ok {

src = strings.Replace(
fmt.Sprintf("%s:%d", stringTrim(file, "src/"), lineno), "%2e", ".", -1)
fmt.Sprintf("%s:%d", stringTrim(file, strim), lineno), "%2e", ".", -1)
}

msgSt.Level = levelPrefix[logLevel]
Expand Down Expand Up @@ -315,6 +326,10 @@ func Reset() {
defaultLogger.Reset()
}

func SetLogPathTrim(trimPath string) {
defaultLogger.SetLogPathTrim(trimPath)
}

// param 可以是log配置文件名,也可以是log配置内容,默认DEBUG输出到控制台
func SetLogger(param ...string) error {
if 0 == len(param) {
Expand Down

0 comments on commit 11d39e1

Please sign in to comment.