Skip to content

Commit

Permalink
feat: add basic api for log (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Aug 30, 2022
1 parent 771ae2a commit 6c0aa7f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
89 changes: 71 additions & 18 deletions pkg/xlog/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,81 @@

package xlog

import "go.uber.org/zap"

// defaultLogger default logger
// Biz Log
// debug=true as default, will be
var defaultLogger = Config{
Name: "default",
Debug: true,
}.Build()

// jupiterLogger frame logger
var jupiterLogger = Config{
Name: "jupiter",
Debug: true,
}.Build()

// Jupiter returns framework logger
func Jupiter() *zap.Logger {
func Jupiter() *Logger {
return jupiterLogger
}

// Default returns default logger
func Default() *zap.Logger {
func Default() *Logger {
return defaultLogger
}

// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Debug(msg string, fields ...Field) {
defaultLogger.Debug(msg, fields...)
}

// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Info(msg string, fields ...Field) {
defaultLogger.Info(msg, fields...)
}

// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Warn(msg string, fields ...Field) {
defaultLogger.Warn(msg, fields...)
}

// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Error(msg string, fields ...Field) {
defaultLogger.Error(msg, fields...)
}

// DPanic logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
// If the logger is in development mode, it then panics (DPanic means
// "development panic"). This is useful for catching errors that are
// recoverable, but shouldn't ever happen.
func DPanic(msg string, fields ...Field) {
defaultLogger.DPanic(msg, fields...)
}

// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func Panic(msg string, fields ...Field) {
defaultLogger.Panic(msg, fields...)
}

// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func Fatal(msg string, fields ...Field) {
defaultLogger.Fatal(msg, fields...)
}

// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
func With(fields ...Field) *Logger {
return defaultLogger.With(fields...)
}

// WithOptions clones the current Logger, applies the supplied Options, and
// returns the resulting Logger. It's safe to use concurrently.
func WithOptions(opts ...Option) *Logger {
return defaultLogger.WithOptions(opts...)
}

// Named adds a new path segment to the logger's name. Segments are joined by
// periods. By default, Loggers are unnamed.
func Named(s string) *Logger {
return defaultLogger.Named(s)
}
13 changes: 13 additions & 0 deletions pkg/xlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Field = zap.Field
Level = zapcore.Level
Logger = zap.Logger
Option = zap.Option
)

var (
Expand Down Expand Up @@ -69,6 +70,18 @@ const (
defaultFlushInterval = 5 * time.Second
)

// defaultLogger default logger for biz.
var defaultLogger = Config{
Name: "default",
Debug: true,
}.Build()

// jupiterLogger frame logger
var jupiterLogger = Config{
Name: "jupiter",
Debug: true,
}.Build()

func newLogger(config *Config) *zap.Logger {
zapOptions := make([]zap.Option, 0)
zapOptions = append(zapOptions, zap.AddStacktrace(zap.DPanicLevel))
Expand Down

0 comments on commit 6c0aa7f

Please sign in to comment.