Skip to content

Commit

Permalink
add log interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed May 20, 2020
1 parent 2c5f290 commit 48f9ce0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 43 deletions.
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Package asteria is a logging library for go with module support.
*/
package asteria

21 changes: 21 additions & 0 deletions log/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package log

type Logger interface {
WithFields(c Fields) Logger
Emergency(v ...interface{})
Alert(v ...interface{})
Critical(v ...interface{})
Error(v ...interface{})
Warning(v ...interface{})
Notice(v ...interface{})
Info(v ...interface{})
Debug(v ...interface{})
Emergencyf(format string, v ...interface{})
Alertf(format string, v ...interface{})
Criticalf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Warningf(format string, v ...interface{})
Noticef(format string, v ...interface{})
Infof(format string, v ...interface{})
Debugf(format string, v ...interface{})
}
18 changes: 17 additions & 1 deletion log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ import (
)

type ContextLogger struct {
logger *Logger
logger *AsteriaLogger
context Fields
}

// WithFields 带有上下文信息的日志输出
func (logger *ContextLogger) WithFields(c Fields) Logger {
c2 := make(Fields)
for k, v := range logger.context {
c2[k] = v
}
for k, v := range c {
c2[k] = v
}

return &ContextLogger{
logger: logger.logger,
context: c2,
}
}

func (logger *ContextLogger) Emergency(v ...interface{}) {
logger.logger.Output(3, level.Emergency, logger.context, v...)
}
Expand Down
8 changes: 4 additions & 4 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ func Close() error {
}

// LogLevel 设置日志输出级别
func SetLevel(le level.Level) *Logger {
func SetLevel(le level.Level) *AsteriaLogger {
return Default().LogLevel(le)
}

// Formatter 设置日志格式化器
func SetFormatter(f formatter.Formatter) *Logger {
func SetFormatter(f formatter.Formatter) *AsteriaLogger {
return Default().Formatter(f)
}

// Writer 设置日志输出器
func SetWriter(w writer.Writer) *Logger {
func SetWriter(w writer.Writer) *AsteriaLogger {
return Default().Writer(w)
}

func WithFields(fields Fields) *ContextLogger {
func WithFields(fields Fields) Logger {
return Default().WithFields(fields)
}

Expand Down
1 change: 1 addition & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,4 @@ func TestLogger_Formatter(t *testing.T) {
assert.Equal(t, level.Warning, mockWriter.LastLevel)
assert.Regexp(t, regexp.MustCompile("^{.*}$"), mockWriter.LastMessage)
}

76 changes: 38 additions & 38 deletions log/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var loggers = make(Loggers)
var moduleLock sync.RWMutex

// Loggers is a map holds all loggers
type Loggers map[string]*Logger
type Loggers map[string]*AsteriaLogger

// All return all loggers
func All() Loggers {
Expand Down Expand Up @@ -99,8 +99,8 @@ func (loggers Loggers) LogWriter(w writer.Writer) {
defaultLogConfig.LogWriter = w
}

// Logger 日志对象
type Logger struct {
// AsteriaLogger 日志对象
type AsteriaLogger struct {
moduleName string
level level.Level
formatter formatter.Formatter
Expand All @@ -115,15 +115,15 @@ type Logger struct {
}

// AddFilter append a filter to logger
func (module *Logger) AddFilter(f ...FilterChain) {
func (module *AsteriaLogger) AddFilter(f ...FilterChain) {
module.lock.Lock()
defer module.lock.Unlock()

module.filters = append(module.filters, f...)
}

// Filters return all filters
func (module *Logger) Filters() []FilterChain {
func (module *AsteriaLogger) Filters() []FilterChain {
module.lock.RLock()
defer module.lock.RUnlock()

Expand Down Expand Up @@ -241,15 +241,15 @@ func GlobalFields(f func(c event.Fields)) {
}

// Module 获取指定模块的日志输出对象
func Module(moduleName string) *Logger {
func Module(moduleName string) *AsteriaLogger {
moduleLock.Lock()
defer moduleLock.Unlock()

if logger, ok := loggers[moduleName]; ok {
return logger
}

logger := &Logger{
logger := &AsteriaLogger{
moduleName: moduleName,
formatter: defaultLogConfig.LogFormatter,
writer: defaultLogConfig.LogWriter,
Expand All @@ -266,7 +266,7 @@ func Module(moduleName string) *Logger {
}

// Location set time location for module
func (module *Logger) Location(loc *time.Location) *Logger {
func (module *AsteriaLogger) Location(loc *time.Location) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

Expand All @@ -276,7 +276,7 @@ func (module *Logger) Location(loc *time.Location) *Logger {
}

// WithFileLine set whether output file & Line
func (module *Logger) WithFileLine(enable bool) *Logger {
func (module *AsteriaLogger) WithFileLine(enable bool) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

Expand All @@ -285,7 +285,7 @@ func (module *Logger) WithFileLine(enable bool) *Logger {
}

// DynamicModuleName set whether enable dynamic module name generate
func (module *Logger) DynamicModuleName(enable bool) *Logger {
func (module *AsteriaLogger) DynamicModuleName(enable bool) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

Expand All @@ -294,7 +294,7 @@ func (module *Logger) DynamicModuleName(enable bool) *Logger {
}

// GlobalFields set global fields
func (module *Logger) GlobalFields(f func(c event.Fields)) *Logger {
func (module *AsteriaLogger) GlobalFields(f func(c event.Fields)) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

Expand All @@ -303,7 +303,7 @@ func (module *Logger) GlobalFields(f func(c event.Fields)) *Logger {
return module
}

func (module *Logger) Output(callDepth int, le level.Level, userContext Fields, v ...interface{}) {
func (module *AsteriaLogger) Output(callDepth int, le level.Level, userContext Fields, v ...interface{}) {
if le > module.level {
return
}
Expand Down Expand Up @@ -364,12 +364,12 @@ func (module *Logger) Output(callDepth int, le level.Level, userContext Fields,
}

// Default 获取默认的模块日志
func Default() *Logger {
func Default() *AsteriaLogger {
return Module("main")
}

// LogLevel 设置日志输出级别
func (module *Logger) LogLevel(le level.Level) *Logger {
func (module *AsteriaLogger) LogLevel(le level.Level) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

Expand All @@ -379,119 +379,119 @@ func (module *Logger) LogLevel(le level.Level) *Logger {
}

// Formatter 设置日志格式化器
func (module *Logger) Formatter(f formatter.Formatter) *Logger {
func (module *AsteriaLogger) Formatter(f formatter.Formatter) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

module.formatter = f
return module
}

func (module *Logger) getFormatter() formatter.Formatter {
func (module *AsteriaLogger) getFormatter() formatter.Formatter {
module.lock.RLock()
defer module.lock.RUnlock()

return module.formatter
}

// Writer 设置日志输出器
func (module *Logger) Writer(w writer.Writer) *Logger {
func (module *AsteriaLogger) Writer(w writer.Writer) *AsteriaLogger {
module.lock.Lock()
defer module.lock.Unlock()

module.writer = w
return module
}

func (module *Logger) getWriter() writer.Writer {
func (module *AsteriaLogger) getWriter() writer.Writer {
module.lock.RLock()
defer module.lock.RUnlock()

return module.writer
}

// ReOpen reopen a log file
func (module *Logger) ReOpen() error {
func (module *AsteriaLogger) ReOpen() error {
return module.getWriter().ReOpen()
}

// Close close a log LogWriter
func (module *Logger) Close() error {
func (module *AsteriaLogger) Close() error {
return module.getWriter().Close()
}

// WithFields 带有上下文信息的日志输出
func (module *Logger) WithFields(c Fields) *ContextLogger {
func (module *AsteriaLogger) WithFields(c Fields) Logger {
return &ContextLogger{
logger: module,
context: c,
}
}

func (module *Logger) Emergency(v ...interface{}) {
func (module *AsteriaLogger) Emergency(v ...interface{}) {
module.Output(3, level.Emergency, nil, v...)
}

func (module *Logger) Alert(v ...interface{}) {
func (module *AsteriaLogger) Alert(v ...interface{}) {
module.Output(3, level.Alert, nil, v...)
}

func (module *Logger) Critical(v ...interface{}) {
func (module *AsteriaLogger) Critical(v ...interface{}) {
module.Output(3, level.Critical, nil, v...)
}

func (module *Logger) Error(v ...interface{}) {
func (module *AsteriaLogger) Error(v ...interface{}) {
module.Output(3, level.Error, nil, v...)
}

func (module *Logger) Warning(v ...interface{}) {
func (module *AsteriaLogger) Warning(v ...interface{}) {
module.Output(3, level.Warning, nil, v...)
}

func (module *Logger) Notice(v ...interface{}) {
func (module *AsteriaLogger) Notice(v ...interface{}) {
module.Output(3, level.Notice, nil, v...)
}

func (module *Logger) Info(v ...interface{}) {
func (module *AsteriaLogger) Info(v ...interface{}) {
module.Output(3, level.Info, nil, v...)
}

func (module *Logger) Debug(v ...interface{}) {
func (module *AsteriaLogger) Debug(v ...interface{}) {
module.Output(3, level.Debug, nil, v...)
}

func (module *Logger) Emergencyf(format string, v ...interface{}) {
func (module *AsteriaLogger) Emergencyf(format string, v ...interface{}) {
module.Output(3, level.Emergency, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Alertf(format string, v ...interface{}) {
func (module *AsteriaLogger) Alertf(format string, v ...interface{}) {
module.Output(3, level.Alert, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Criticalf(format string, v ...interface{}) {
func (module *AsteriaLogger) Criticalf(format string, v ...interface{}) {
module.Output(3, level.Critical, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Errorf(format string, v ...interface{}) {
func (module *AsteriaLogger) Errorf(format string, v ...interface{}) {
module.Output(3, level.Error, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Warningf(format string, v ...interface{}) {
func (module *AsteriaLogger) Warningf(format string, v ...interface{}) {
module.Output(3, level.Warning, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Noticef(format string, v ...interface{}) {
func (module *AsteriaLogger) Noticef(format string, v ...interface{}) {
module.Output(3, level.Notice, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Infof(format string, v ...interface{}) {
func (module *AsteriaLogger) Infof(format string, v ...interface{}) {
module.Output(3, level.Info, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Debugf(format string, v ...interface{}) {
func (module *AsteriaLogger) Debugf(format string, v ...interface{}) {
module.Output(3, level.Debug, nil, fmt.Sprintf(format, v...))
}

func (module *Logger) Print(v ...interface{}) {
func (module *AsteriaLogger) Print(v ...interface{}) {
module.Output(3, level.Debug, nil, v...)
}

0 comments on commit 48f9ce0

Please sign in to comment.