forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
2,376 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*Package log 是kratos日志库. | ||
一、主要功能: | ||
1. 日志打印到本地 | ||
2. 日志打印到标准输出 | ||
3. verbose日志实现,参考glog实现,可通过设置不同verbose级别,默认不开启 | ||
二、日志配置 | ||
1. 默认配置 | ||
目前日志已经实现默认配置。可以直接使用以下方式: | ||
log.Init(nil) | ||
2. 启动参数 or 环境变量 | ||
启动参数 环境变量 说明 | ||
log.stdout LOG_STDOUT 是否开启标准输出 | ||
log.dir LOG_DIR 文件日志路径 | ||
log.v LOG_V verbose日志级别 | ||
log.module LOG_MODULE 可单独配置每个文件的verbose级别:file=1,file2=2 | ||
log.filter LOG_FILTER 配置需要过滤的字段:field1,field2 | ||
3. 配置文件 | ||
但是如果有特殊需要可以走一下格式配置: | ||
[log] | ||
family = "xxx-service" | ||
dir = "/data/log/xxx-service/" | ||
stdout = true | ||
vLevel = 3 | ||
filter = ["fileld1", "field2"] | ||
[log.module] | ||
"dao_user" = 2 | ||
"servic*" = 1 | ||
三、配置说明 | ||
1.log | ||
family 项目名,默认读环境变量$APPID | ||
studout 标准输出,prod环境不建议开启 | ||
filter 配置需要过滤掉的字段,以“***”替换 | ||
dir 文件日志地址,prod环境不建议开启 | ||
v 开启verbose级别日志,可指定全局级别 | ||
2. log.module | ||
可单独配置每个文件的verbose级别 | ||
*/ | ||
package log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/bilibili/Kratos/pkg/log/internal/filewriter" | ||
) | ||
|
||
// level idx | ||
const ( | ||
_infoIdx = iota | ||
_warnIdx | ||
_errorIdx | ||
_totalIdx | ||
) | ||
|
||
var _fileNames = map[int]string{ | ||
_infoIdx: "info.log", | ||
_warnIdx: "warning.log", | ||
_errorIdx: "error.log", | ||
} | ||
|
||
// FileHandler . | ||
type FileHandler struct { | ||
render Render | ||
fws [_totalIdx]*filewriter.FileWriter | ||
} | ||
|
||
// NewFile crete a file logger. | ||
func NewFile(dir string, bufferSize, rotateSize int64, maxLogFile int) *FileHandler { | ||
// new info writer | ||
newWriter := func(name string) *filewriter.FileWriter { | ||
var options []filewriter.Option | ||
if rotateSize > 0 { | ||
options = append(options, filewriter.MaxSize(rotateSize)) | ||
} | ||
if maxLogFile > 0 { | ||
options = append(options, filewriter.MaxFile(maxLogFile)) | ||
} | ||
w, err := filewriter.New(filepath.Join(dir, name), options...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return w | ||
} | ||
handler := &FileHandler{ | ||
render: newPatternRender("[%D %T] [%L] [%S] %M"), | ||
} | ||
for idx, name := range _fileNames { | ||
handler.fws[idx] = newWriter(name) | ||
} | ||
return handler | ||
} | ||
|
||
// Log loggint to file . | ||
func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) { | ||
d := make(map[string]interface{}, 10+len(args)) | ||
for _, arg := range args { | ||
d[arg.Key] = arg.Value | ||
} | ||
// add extra fields | ||
addExtraField(ctx, d) | ||
d[_time] = time.Now().Format(_timeFormat) | ||
var w io.Writer | ||
switch lv { | ||
case _warnLevel: | ||
w = h.fws[_warnIdx] | ||
case _errorLevel: | ||
w = h.fws[_errorIdx] | ||
default: | ||
w = h.fws[_infoIdx] | ||
} | ||
h.render.Render(w, d) | ||
w.Write([]byte("\n")) | ||
} | ||
|
||
// Close log handler | ||
func (h *FileHandler) Close() error { | ||
for _, fw := range h.fws { | ||
// ignored error | ||
fw.Close() | ||
} | ||
return nil | ||
} | ||
|
||
// SetFormat set log format | ||
func (h *FileHandler) SetFormat(format string) { | ||
h.render = newPatternRender(format) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
COPY FROM: https://github.com/uber-go/zap | ||
|
||
Copyright (c) 2016-2017 Uber Technologies, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package core | ||
|
||
import "strconv" | ||
|
||
const _size = 1024 // by default, create 1 KiB buffers | ||
|
||
// NewBuffer is new buffer | ||
func NewBuffer(_size int) *Buffer { | ||
return &Buffer{bs: make([]byte, 0, _size)} | ||
} | ||
|
||
// Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so | ||
// the only way to construct one is via a Pool. | ||
type Buffer struct { | ||
bs []byte | ||
pool Pool | ||
} | ||
|
||
// AppendByte writes a single byte to the Buffer. | ||
func (b *Buffer) AppendByte(v byte) { | ||
b.bs = append(b.bs, v) | ||
} | ||
|
||
// AppendString writes a string to the Buffer. | ||
func (b *Buffer) AppendString(s string) { | ||
b.bs = append(b.bs, s...) | ||
} | ||
|
||
// AppendInt appends an integer to the underlying buffer (assuming base 10). | ||
func (b *Buffer) AppendInt(i int64) { | ||
b.bs = strconv.AppendInt(b.bs, i, 10) | ||
} | ||
|
||
// AppendUint appends an unsigned integer to the underlying buffer (assuming | ||
// base 10). | ||
func (b *Buffer) AppendUint(i uint64) { | ||
b.bs = strconv.AppendUint(b.bs, i, 10) | ||
} | ||
|
||
// AppendBool appends a bool to the underlying buffer. | ||
func (b *Buffer) AppendBool(v bool) { | ||
b.bs = strconv.AppendBool(b.bs, v) | ||
} | ||
|
||
// AppendFloat appends a float to the underlying buffer. It doesn't quote NaN | ||
// or +/- Inf. | ||
func (b *Buffer) AppendFloat(f float64, bitSize int) { | ||
b.bs = strconv.AppendFloat(b.bs, f, 'f', -1, bitSize) | ||
} | ||
|
||
// Len returns the length of the underlying byte slice. | ||
func (b *Buffer) Len() int { | ||
return len(b.bs) | ||
} | ||
|
||
// Cap returns the capacity of the underlying byte slice. | ||
func (b *Buffer) Cap() int { | ||
return cap(b.bs) | ||
} | ||
|
||
// Bytes returns a mutable reference to the underlying byte slice. | ||
func (b *Buffer) Bytes() []byte { | ||
return b.bs | ||
} | ||
|
||
// String returns a string copy of the underlying byte slice. | ||
func (b *Buffer) String() string { | ||
return string(b.bs) | ||
} | ||
|
||
// Reset resets the underlying byte slice. Subsequent writes re-use the slice's | ||
// backing array. | ||
func (b *Buffer) Reset() { | ||
b.bs = b.bs[:0] | ||
} | ||
|
||
// Write implements io.Writer. | ||
func (b *Buffer) Write(bs []byte) (int, error) { | ||
b.bs = append(b.bs, bs...) | ||
return len(bs), nil | ||
} | ||
|
||
// TrimNewline trims any final "\n" byte from the end of the buffer. | ||
func (b *Buffer) TrimNewline() { | ||
if i := len(b.bs) - 1; i >= 0 { | ||
if b.bs[i] == '\n' { | ||
b.bs = b.bs[:i] | ||
} | ||
} | ||
} | ||
|
||
// Free returns the Buffer to its Pool. | ||
// | ||
// Callers must not retain references to the Buffer after calling Free. | ||
func (b *Buffer) Free() { | ||
b.pool.put(b) | ||
} |
Oops, something went wrong.