Skip to content

Commit

Permalink
add log adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLYC committed Jan 9, 2022
1 parent 048e859 commit 0a2d992
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 0 deletions.
30 changes: 30 additions & 0 deletions logging/log/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云-gopkg available.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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 log

import (
"log"

"logur.dev/logur"

"github.com/TencentBlueKing/gopkg/logging"
)

// SetLogger sets a logger to the logging package.
func SetLogger(name string, logger *log.Logger, level logur.Level) {
logging.SetLogger(name, New(logger, level))
}

// EnsureDefaultLogger will replace the default logger by log default logger.
func EnsureDefaultLogger(level logur.Level) {
SetLogger(logging.DefaultLoggerName, log.Default(), level)
}
24 changes: 24 additions & 0 deletions logging/log/log_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云-gopkg available.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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 log

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestLog(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Log Suite")
}
102 changes: 102 additions & 0 deletions logging/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云-gopkg available.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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 log

import (
"context"
"fmt"
"log"
"strings"

"logur.dev/logur"
)

// Logger is a Logur adapter for TEMPLATE.
type Logger struct {
level logur.Level
logger *log.Logger
}

// New returns a new Logur logger.
// If logger is nil, a default instance is created.
func New(logger *log.Logger, level logur.Level) *Logger {
return &Logger{
level: level,
logger: logger,
}
}

func (l *Logger) log(level logur.Level, prefix string, msg string, fields ...map[string]interface{}) {
if level < l.level {
return
}

fieldCount := 2
if len(fields) != 0 {
fieldCount += len(fields[0])
}

parts := make([]string, 0, fieldCount)
parts = append(parts, prefix, msg)

if len(fields) != 0 {
for key, value := range fields[0] {
parts = append(parts, fmt.Sprintf("%v=%v", key, value))
}
}

l.logger.Print(strings.Join(parts, " "))
}

// Trace implements the Logur Logger interface.
func (l *Logger) Trace(msg string, fields ...map[string]interface{}) {
l.log(logur.Trace, "TRACE", msg, fields...)
}

// Debug implements the Logur Logger interface.
func (l *Logger) Debug(msg string, fields ...map[string]interface{}) {
l.log(logur.Debug, "DEBUG", msg, fields...)
}

// Info implements the Logur Logger interface.
func (l *Logger) Info(msg string, fields ...map[string]interface{}) {
l.log(logur.Info, "INFO", msg, fields...)
}

// Warn implements the Logur Logger interface.
func (l *Logger) Warn(msg string, fields ...map[string]interface{}) {
l.log(logur.Warn, "WARN", msg, fields...)
}

// Error implements the Logur Logger interface.
func (l *Logger) Error(msg string, fields ...map[string]interface{}) {
l.log(logur.Error, "ERROR", msg, fields...)
}

func (l *Logger) TraceContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Trace(msg, fields...)
}

func (l *Logger) DebugContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Debug(msg, fields...)
}

func (l *Logger) InfoContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Info(msg, fields...)
}

func (l *Logger) WarnContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Warn(msg, fields...)
}

func (l *Logger) ErrorContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Error(msg, fields...)
}
88 changes: 88 additions & 0 deletions logging/log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云-gopkg available.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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 log

import (
"log"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"logur.dev/logur"
)

var _ = Describe("Logger", func() {
var (
output strings.Builder
logger *log.Logger
)

BeforeEach(func() {
output.Reset()
logger = log.New(&output, "", 0)
})

Context("Log", func() {
It("should log a message", func() {
l := New(logger, logur.Info)
l.log(logur.Info, "TEST", "log")
Expect(output.String()).To(Equal("TEST log\n"))
})

It("should not log a message", func() {
l := New(logger, logur.Error)
l.log(logur.Info, "TEST", "log")
Expect(output.String()).To(Equal(""))
})

It("should log a message with fields", func() {
l := New(logger, logur.Info)
l.log(logur.Info, "TEST", "log", map[string]interface{}{
"value": 123.456,
})
Expect(output.String()).To(Equal("TEST log value=123.456\n"))
})
})

Context("Log by level", func() {
var adaptedLogger *Logger

BeforeEach(func() {
adaptedLogger = New(logger, logur.Trace)
})

It("Trace", func() {
adaptedLogger.Trace("testing")
Expect(output.String()).To(Equal("TRACE testing\n"))
})

It("Debug", func() {
adaptedLogger.Debug("testing")
Expect(output.String()).To(Equal("DEBUG testing\n"))
})

It("Info", func() {
adaptedLogger.Info("testing")
Expect(output.String()).To(Equal("INFO testing\n"))
})

It("Warn", func() {
adaptedLogger.Warn("testing")
Expect(output.String()).To(Equal("WARN testing\n"))
})

It("Error", func() {
adaptedLogger.Error("testing")
Expect(output.String()).To(Equal("ERROR testing\n"))
})
})
})

0 comments on commit 0a2d992

Please sign in to comment.