Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhao committed Mar 6, 2019
1 parent 471a525 commit f98cd0a
Show file tree
Hide file tree
Showing 23 changed files with 2,376 additions and 7 deletions.
51 changes: 51 additions & 0 deletions pkg/log/doc.go
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
92 changes: 92 additions & 0 deletions pkg/log/file.go
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)
}
66 changes: 61 additions & 5 deletions pkg/log/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,44 @@ package log

import (
"context"
"time"

pkgerr "github.com/pkg/errors"
)

const (
_timeFormat = "2006-01-02T15:04:05.999999"

// log level defined in level.go.
_levelValue = "level_value"
// log level name: INFO, WARN...
_level = "level"
// log time.
_time = "time"
// log file.
_source = "source"
// common log filed.
_log = "log"
// app name.
_appID = "app_id"
// container ID.
_instanceID = "instance_id"
// uniq ID from trace.
_tid = "traceid"
// request time.
_ts = "ts"
// requester.
_caller = "caller"
// container environment: prod, pre, uat, fat.
_deplyEnv = "env"
// container area.
_zone = "zone"
// mirror flag
_mirror = "mirror"
// color.
_color = "color"
// cluster.
_cluster = "cluster"
)

// Handler is used to handle log events, outputting them to
Expand All @@ -28,19 +60,43 @@ type Handler interface {
Close() error
}

// Handlers .
type Handlers []Handler
func newHandlers(filters []string, handlers ...Handler) *Handlers {
set := make(map[string]struct{})
for _, k := range filters {
set[k] = struct{}{}
}
return &Handlers{filters: set, handlers: handlers}
}

// Handlers a bundle for hander with filter function.
type Handlers struct {
filters map[string]struct{}
handlers []Handler
}

// Log handlers logging.
func (hs Handlers) Log(c context.Context, lv Level, d ...D) {
for _, h := range hs {
var fn string
for i := range d {
if _, ok := hs.filters[d[i].Key]; ok {
d[i].Value = "***"
}
if d[i].Key == _source {
fn = d[i].Value.(string)
}
}
if fn == "" {
fn = funcName(4)
}
d = append(d, KV(_source, fn), KV(_time, time.Now()), KV(_levelValue, int(lv)), KV(_level, lv.String()))
for _, h := range hs.handlers {
h.Log(c, lv, d...)
}
}

// Close close resource.
func (hs Handlers) Close() (err error) {
for _, h := range hs {
for _, h := range hs.handlers {
if e := h.Close(); e != nil {
err = pkgerr.WithStack(e)
}
Expand All @@ -50,7 +106,7 @@ func (hs Handlers) Close() (err error) {

// SetFormat .
func (hs Handlers) SetFormat(format string) {
for _, h := range hs {
for _, h := range hs.handlers {
h.SetFormat(format)
}
}
21 changes: 21 additions & 0 deletions pkg/log/internal/LICENSE.txt
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.
97 changes: 97 additions & 0 deletions pkg/log/internal/buffer.go
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)
}
Loading

0 comments on commit f98cd0a

Please sign in to comment.