From 6aa6c55f07be018183f72c436578d4eb00a4a6f0 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 5 Oct 2020 21:54:38 +0800 Subject: [PATCH 1/2] logs Adapter --- pkg/adapter/logs/accesslog.go | 27 +++ pkg/adapter/logs/alils/alils.go | 5 + pkg/adapter/logs/es/es.go | 5 + pkg/adapter/logs/log.go | 346 ++++++++++++++++++++++++++++++++ pkg/adapter/logs/log_adapter.go | 69 +++++++ pkg/adapter/logs/logger.go | 38 ++++ pkg/adapter/logs/logger_test.go | 24 +++ 7 files changed, 514 insertions(+) create mode 100644 pkg/adapter/logs/accesslog.go create mode 100644 pkg/adapter/logs/alils/alils.go create mode 100644 pkg/adapter/logs/es/es.go create mode 100644 pkg/adapter/logs/log.go create mode 100644 pkg/adapter/logs/log_adapter.go create mode 100644 pkg/adapter/logs/logger.go create mode 100644 pkg/adapter/logs/logger_test.go diff --git a/pkg/adapter/logs/accesslog.go b/pkg/adapter/logs/accesslog.go new file mode 100644 index 00000000..cebee92b --- /dev/null +++ b/pkg/adapter/logs/accesslog.go @@ -0,0 +1,27 @@ +// Copyright 2014 beego Author. 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. + +package logs + +import ( + "github.com/astaxie/beego/pkg/core/logs" +) + +// AccessLogRecord struct for holding access log data. +type AccessLogRecord logs.AccessLogRecord + +// AccessLog - Format and print access log. +func AccessLog(r *AccessLogRecord, format string) { + logs.AccessLog((*logs.AccessLogRecord)(r), format) +} diff --git a/pkg/adapter/logs/alils/alils.go b/pkg/adapter/logs/alils/alils.go new file mode 100644 index 00000000..5abbc29f --- /dev/null +++ b/pkg/adapter/logs/alils/alils.go @@ -0,0 +1,5 @@ +package alils + +import ( + _ "github.com/astaxie/beego/pkg/core/logs/alils" +) diff --git a/pkg/adapter/logs/es/es.go b/pkg/adapter/logs/es/es.go new file mode 100644 index 00000000..e0759485 --- /dev/null +++ b/pkg/adapter/logs/es/es.go @@ -0,0 +1,5 @@ +package es + +import ( + _ "github.com/astaxie/beego/pkg/core/logs/es" +) diff --git a/pkg/adapter/logs/log.go b/pkg/adapter/logs/log.go new file mode 100644 index 00000000..6a7045fd --- /dev/null +++ b/pkg/adapter/logs/log.go @@ -0,0 +1,346 @@ +// Copyright 2014 beego Author. 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. + +// Package logs provide a general log interface +// Usage: +// +// import "github.com/astaxie/beego/logs" +// +// log := NewLogger(10000) +// log.SetLogger("console", "") +// +// > the first params stand for how many channel +// +// Use it like this: +// +// log.Trace("trace") +// log.Info("info") +// log.Warn("warning") +// log.Debug("debug") +// log.Critical("critical") +// +// more docs http://beego.me/docs/module/logs.md +package logs + +import ( + "log" + "time" + + "github.com/astaxie/beego/pkg/core/logs" +) + +// RFC5424 log message levels. +const ( + LevelEmergency = iota + LevelAlert + LevelCritical + LevelError + LevelWarning + LevelNotice + LevelInformational + LevelDebug +) + +// levelLogLogger is defined to implement log.Logger +// the real log level will be LevelEmergency +const levelLoggerImpl = -1 + +// Name for adapter with beego official support +const ( + AdapterConsole = "console" + AdapterFile = "file" + AdapterMultiFile = "multifile" + AdapterMail = "smtp" + AdapterConn = "conn" + AdapterEs = "es" + AdapterJianLiao = "jianliao" + AdapterSlack = "slack" + AdapterAliLS = "alils" +) + +// Legacy log level constants to ensure backwards compatibility. +const ( + LevelInfo = LevelInformational + LevelTrace = LevelDebug + LevelWarn = LevelWarning +) + +type newLoggerFunc func() Logger + +// Logger defines the behavior of a log provider. +type Logger interface { + Init(config string) error + WriteMsg(when time.Time, msg string, level int) error + Destroy() + Flush() +} + +var adapters = make(map[string]newLoggerFunc) +var levelPrefix = [LevelDebug + 1]string{"[M]", "[A]", "[C]", "[E]", "[W]", "[N]", "[I]", "[D]"} + +// Register makes a log provide available by the provided name. +// If Register is called twice with the same name or if driver is nil, +// it panics. +func Register(name string, log newLoggerFunc) { + logs.Register(name, func() logs.Logger { + return &oldToNewAdapter{ + old: log(), + } + }) +} + +// BeeLogger is default logger in beego application. +// it can contain several providers and log message into all providers. +type BeeLogger logs.BeeLogger + +const defaultAsyncMsgLen = 1e3 + +// NewLogger returns a new BeeLogger. +// channelLen means the number of messages in chan(used where asynchronous is true). +// if the buffering chan is full, logger adapters write to file or other way. +func NewLogger(channelLens ...int64) *BeeLogger { + return (*BeeLogger)(logs.NewLogger(channelLens...)) +} + +// Async set the log to asynchronous and start the goroutine +func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger { + (*logs.BeeLogger)(bl).Async(msgLen...) + return bl +} + +// SetLogger provides a given logger adapter into BeeLogger with config string. +// config need to be correct JSON as string: {"interval":360}. +func (bl *BeeLogger) SetLogger(adapterName string, configs ...string) error { + return (*logs.BeeLogger)(bl).SetLogger(adapterName, configs...) +} + +// DelLogger remove a logger adapter in BeeLogger. +func (bl *BeeLogger) DelLogger(adapterName string) error { + return (*logs.BeeLogger)(bl).DelLogger(adapterName) +} + +func (bl *BeeLogger) Write(p []byte) (n int, err error) { + return (*logs.BeeLogger)(bl).Write(p) +} + +// SetLevel Set log message level. +// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning), +// log providers will not even be sent the message. +func (bl *BeeLogger) SetLevel(l int) { + (*logs.BeeLogger)(bl).SetLevel(l) +} + +// GetLevel Get Current log message level. +func (bl *BeeLogger) GetLevel() int { + return (*logs.BeeLogger)(bl).GetLevel() +} + +// SetLogFuncCallDepth set log funcCallDepth +func (bl *BeeLogger) SetLogFuncCallDepth(d int) { + (*logs.BeeLogger)(bl).SetLogFuncCallDepth(d) +} + +// GetLogFuncCallDepth return log funcCallDepth for wrapper +func (bl *BeeLogger) GetLogFuncCallDepth() int { + return (*logs.BeeLogger)(bl).GetLogFuncCallDepth() +} + +// EnableFuncCallDepth enable log funcCallDepth +func (bl *BeeLogger) EnableFuncCallDepth(b bool) { + (*logs.BeeLogger)(bl).EnableFuncCallDepth(b) +} + +// set prefix +func (bl *BeeLogger) SetPrefix(s string) { + (*logs.BeeLogger)(bl).SetPrefix(s) +} + +// Emergency Log EMERGENCY level message. +func (bl *BeeLogger) Emergency(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Emergency(format, v...) +} + +// Alert Log ALERT level message. +func (bl *BeeLogger) Alert(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Alert(format, v...) +} + +// Critical Log CRITICAL level message. +func (bl *BeeLogger) Critical(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Critical(format, v...) +} + +// Error Log ERROR level message. +func (bl *BeeLogger) Error(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Error(format, v...) +} + +// Warning Log WARNING level message. +func (bl *BeeLogger) Warning(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Warning(format, v...) +} + +// Notice Log NOTICE level message. +func (bl *BeeLogger) Notice(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Notice(format, v...) +} + +// Informational Log INFORMATIONAL level message. +func (bl *BeeLogger) Informational(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Informational(format, v...) +} + +// Debug Log DEBUG level message. +func (bl *BeeLogger) Debug(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Debug(format, v...) +} + +// Warn Log WARN level message. +// compatibility alias for Warning() +func (bl *BeeLogger) Warn(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Warn(format, v...) +} + +// Info Log INFO level message. +// compatibility alias for Informational() +func (bl *BeeLogger) Info(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Info(format, v...) +} + +// Trace Log TRACE level message. +// compatibility alias for Debug() +func (bl *BeeLogger) Trace(format string, v ...interface{}) { + (*logs.BeeLogger)(bl).Trace(format, v...) +} + +// Flush flush all chan data. +func (bl *BeeLogger) Flush() { + (*logs.BeeLogger)(bl).Flush() +} + +// Close close logger, flush all chan data and destroy all adapters in BeeLogger. +func (bl *BeeLogger) Close() { + (*logs.BeeLogger)(bl).Close() +} + +// Reset close all outputs, and set bl.outputs to nil +func (bl *BeeLogger) Reset() { + (*logs.BeeLogger)(bl).Reset() +} + +// GetBeeLogger returns the default BeeLogger +func GetBeeLogger() *BeeLogger { + return (*BeeLogger)(logs.GetBeeLogger()) +} + +// GetLogger returns the default BeeLogger +func GetLogger(prefixes ...string) *log.Logger { + return logs.GetLogger(prefixes...) +} + +// Reset will remove all the adapter +func Reset() { + logs.Reset() +} + +// Async set the beelogger with Async mode and hold msglen messages +func Async(msgLen ...int64) *BeeLogger { + return (*BeeLogger)(logs.Async(msgLen...)) +} + +// SetLevel sets the global log level used by the simple logger. +func SetLevel(l int) { + logs.SetLevel(l) +} + +// SetPrefix sets the prefix +func SetPrefix(s string) { + logs.SetPrefix(s) +} + +// EnableFuncCallDepth enable log funcCallDepth +func EnableFuncCallDepth(b bool) { + logs.EnableFuncCallDepth(b) +} + +// SetLogFuncCall set the CallDepth, default is 4 +func SetLogFuncCall(b bool) { + logs.SetLogFuncCall(b) +} + +// SetLogFuncCallDepth set log funcCallDepth +func SetLogFuncCallDepth(d int) { + logs.SetLogFuncCallDepth(d) +} + +// SetLogger sets a new logger. +func SetLogger(adapter string, config ...string) error { + return logs.SetLogger(adapter, config...) +} + +// Emergency logs a message at emergency level. +func Emergency(f interface{}, v ...interface{}) { + logs.Emergency(f, v...) +} + +// Alert logs a message at alert level. +func Alert(f interface{}, v ...interface{}) { + logs.Alert(f, v...) +} + +// Critical logs a message at critical level. +func Critical(f interface{}, v ...interface{}) { + logs.Critical(f, v...) +} + +// Error logs a message at error level. +func Error(f interface{}, v ...interface{}) { + logs.Error(f, v...) +} + +// Warning logs a message at warning level. +func Warning(f interface{}, v ...interface{}) { + logs.Warning(f, v...) +} + +// Warn compatibility alias for Warning() +func Warn(f interface{}, v ...interface{}) { + logs.Warn(f, v...) +} + +// Notice logs a message at notice level. +func Notice(f interface{}, v ...interface{}) { + logs.Notice(f, v...) +} + +// Informational logs a message at info level. +func Informational(f interface{}, v ...interface{}) { + logs.Informational(f, v...) +} + +// Info compatibility alias for Warning() +func Info(f interface{}, v ...interface{}) { + logs.Info(f, v...) +} + +// Debug logs a message at debug level. +func Debug(f interface{}, v ...interface{}) { + logs.Debug(f, v...) +} + +// Trace logs a message at trace level. +// compatibility alias for Warning() +func Trace(f interface{}, v ...interface{}) { + logs.Trace(f, v...) +} diff --git a/pkg/adapter/logs/log_adapter.go b/pkg/adapter/logs/log_adapter.go new file mode 100644 index 00000000..ee517bf0 --- /dev/null +++ b/pkg/adapter/logs/log_adapter.go @@ -0,0 +1,69 @@ +// Copyright 2020 +// +// 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. + +package logs + +import ( + "time" + + "github.com/astaxie/beego/pkg/core/logs" +) + +type oldToNewAdapter struct { + old Logger +} + +func (o *oldToNewAdapter) Init(config string) error { + return o.old.Init(config) +} + +func (o *oldToNewAdapter) WriteMsg(lm *logs.LogMsg) error { + return o.old.WriteMsg(lm.When, lm.OldStyleFormat(), lm.Level) +} + +func (o *oldToNewAdapter) Destroy() { + o.old.Destroy() +} + +func (o *oldToNewAdapter) Flush() { + o.old.Flush() +} + +func (o *oldToNewAdapter) SetFormatter(f logs.LogFormatter) { + panic("unsupported operation, you should not invoke this method") +} + +type newToOldAdapter struct { + n logs.Logger +} + +func (n *newToOldAdapter) Init(config string) error { + return n.n.Init(config) +} + +func (n *newToOldAdapter) WriteMsg(when time.Time, msg string, level int) error { + return n.n.WriteMsg(&logs.LogMsg{ + When: when, + Msg: msg, + Level: level, + }) +} + +func (n *newToOldAdapter) Destroy() { + panic("implement me") +} + +func (n *newToOldAdapter) Flush() { + panic("implement me") +} diff --git a/pkg/adapter/logs/logger.go b/pkg/adapter/logs/logger.go new file mode 100644 index 00000000..419ac9c4 --- /dev/null +++ b/pkg/adapter/logs/logger.go @@ -0,0 +1,38 @@ +// Copyright 2014 beego Author. 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. + +package logs + +import ( + "github.com/astaxie/beego/pkg/core/logs" +) + +// ColorByStatus return color by http code +// 2xx return Green +// 3xx return White +// 4xx return Yellow +// 5xx return Red +func ColorByStatus(code int) string { + return logs.ColorByStatus(code) +} + +// ColorByMethod return color by http code +func ColorByMethod(method string) string { + return logs.ColorByMethod(method) +} + +// ResetColor return reset color +func ResetColor() string { + return logs.ResetColor() +} diff --git a/pkg/adapter/logs/logger_test.go b/pkg/adapter/logs/logger_test.go new file mode 100644 index 00000000..9f2cc5a5 --- /dev/null +++ b/pkg/adapter/logs/logger_test.go @@ -0,0 +1,24 @@ +// Copyright 2016 beego Author. 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. + +package logs + +import ( + "testing" +) + +func TestBeeLogger_Info(t *testing.T) { + log := NewLogger(1000) + log.SetLogger("file", `{"net":"tcp","addr":":7020"}`) +} From 8cc74652a2c9de34c987665c53ac116e9c7dc1b4 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 5 Oct 2020 22:45:48 +0800 Subject: [PATCH 2/2] Fix: adapter's controller must implement ControllerInterface --- pkg/adapter/controller.go | 10 ++++------ pkg/adapter/orm/db_alias.go | 11 +++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/pkg/adapter/controller.go b/pkg/adapter/controller.go index 010add64..c0616962 100644 --- a/pkg/adapter/controller.go +++ b/pkg/adapter/controller.go @@ -18,7 +18,6 @@ import ( "mime/multipart" "net/url" - "github.com/astaxie/beego/pkg/adapter/context" "github.com/astaxie/beego/pkg/adapter/session" webContext "github.com/astaxie/beego/pkg/server/web/context" @@ -61,14 +60,13 @@ func (p ControllerCommentsSlice) Swap(i, j int) { // http context, template and view, session and xsrf. type Controller web.Controller +func (c *Controller) Init(ctx *webContext.Context, controllerName, actionName string, app interface{}) { + (*web.Controller)(c).Init(ctx, controllerName, actionName, app) +} + // ControllerInterface is an interface to uniform all controller handler. type ControllerInterface web.ControllerInterface -// Init generates default values of controller operations. -func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) { - (*web.Controller)(c).Init((*webContext.Context)(ctx), controllerName, actionName, app) -} - // Prepare runs after Init before request function execution. func (c *Controller) Prepare() { (*web.Controller)(c).Prepare() diff --git a/pkg/adapter/orm/db_alias.go b/pkg/adapter/orm/db_alias.go index b1f1a724..523b6aee 100644 --- a/pkg/adapter/orm/db_alias.go +++ b/pkg/adapter/orm/db_alias.go @@ -27,12 +27,11 @@ type DriverType orm.DriverType // Enum the Database driver const ( - _ DriverType = iota // int enum type - DRMySQL = orm.DRMySQL - DRSqlite = orm.DRSqlite // sqlite - DROracle = orm.DROracle // oracle - DRPostgres = orm.DRPostgres // pgsql - DRTiDB = orm.DRTiDB // TiDB + DRMySQL = DriverType(orm.DRMySQL) + DRSqlite = DriverType(orm.DRSqlite) // sqlite + DROracle = DriverType(orm.DROracle) // oracle + DRPostgres = DriverType(orm.DRPostgres) // pgsql + DRTiDB = DriverType(orm.DRTiDB) // TiDB ) type DB orm.DB