log

package
v1.0.0-rc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package log is used to control the log printing in MatrixOne.

Printing logs using the Log package enables the following functions:

  1. Uniform logger name
  2. Support search all logs by service's uuid.
  3. Support search the operation logs of all related processes by process ID, e.g. search all logs of that transaction by transaction.
  4. Sampling and printing of frequently output logs.
  5. One line of code to add a time consuming log to a function.
  6. Support for using the mo_ctl function to control log printing.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// FieldNameServiceUUID service uuid field name
	FieldNameServiceUUID = "uuid"
	// FieldNameProcess process of the mo, e.g. transaction
	FieldNameProcess = "process"
	// FieldNameProcessID the log of a processing process may be distributed in
	// many places, we can search all the logs associated with the current process
	// by process-id. For example, we can use the transaction ID as process-id to
	// find all logs of this transaction in the cluster.
	FieldNameProcessID = "process-id"
	// FieldNameCost cost field name, this field is used to log how long a function,
	// operation or other action takes
	FieldNameCost = "cost"
)
View Source
var (
	// SystemInit system init process
	SystemInit = Process("system-init")
	// Txn transaction process
	Txn = Process("txn")
	// Close close serivce or components process. e.g. close cn/dn/log service
	Close = Process("close")
)
View Source
var (
	// TxnClient txn client module
	TxnClient = Module("txn-client")
)

Functions

This section is empty.

Types

type LogOptions

type LogOptions struct {
	// contains filtered or unexported fields
}

LogOptions log options

func DefaultLogOptions

func DefaultLogOptions() LogOptions

DefaultLogOptions default log options

func (LogOptions) AddCallerSkip

func (opts LogOptions) AddCallerSkip(n int) LogOptions

AddCallerSkip help to show the logger real caller, by skip n call stack

func (LogOptions) WithContext

func (opts LogOptions) WithContext(ctx context.Context) LogOptions

WithContext set log trace context.

Example
package main

import (
	"context"

	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
	"github.com/matrixorigin/matrixone/pkg/util/trace"
	"go.uber.org/zap"
)

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}

func main() {
	ctx, span := trace.Start(context.Background(), "ExampleLogOptions_WithContext")
	defer span.End()
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
	logger.Log("this is an info log 1, with Context in LogOptions.", log.DefaultLogOptions().WithLevel(zap.InfoLevel).WithContext(ctx))
	logger.Log("this is an info log 2, without Context.", log.DefaultLogOptions().WithLevel(zap.InfoLevel))
	// 2022/12/17 16:37:22.995817 +0800 INFO cn-service log/example_log_test.go:162 this is an info log 1, with Context in LogOptions. {"uuid": "cn0", "span": {"trace_id": "9f0907c7-7fa6-bb7c-2a25-384f52e03cd5", "span_id": "068f75a50921c85f"}}
	// 2022/12/17 16:37:22.995820 +0800 INFO cn-service log/example_log_test.go:163 this is an info log 2, without Context. {"uuid": "cn0"}
}
Output:

func (LogOptions) WithLevel

func (opts LogOptions) WithLevel(level zapcore.Level) LogOptions

WithLevel set log print level

func (LogOptions) WithProcess

func (opts LogOptions) WithProcess(process Process, processID string) LogOptions

WithProcess if the current log belongs to a certain process, the process name and process ID can be recorded. When analyzing the log, all related logs can be retrieved according to the process ID.

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	processStep1InCN("txn uuid")
	processStep2InTN("txn uuid")
	processStep3InLOG("txn uuid")

	// Output logs:
	// 2022/11/17 15:36:04.724470 +0800 INFO cn-service log/logger.go:51 step 1 {"uuid": "cn0", "process": "txn", "process-id": "txn uuid"}
	// 2022/11/17 15:36:04.724797 +0800 INFO dn-service log/logger.go:51 step 2 {"uuid": "dn0", "process": "txn", "process-id": "txn uuid"}
	// 2022/11/17 15:36:04.724812 +0800 INFO log-service log/logger.go:51 step 3 {"uuid": "log0", "process": "txn", "process-id": "txn uuid"}
}

func processStep1InCN(id string) {
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
	logger.Log("step 1", log.DefaultLogOptions().WithProcess(log.Txn, id))
}

func processStep2InTN(id string) {
	logger := getServiceLogger(metadata.ServiceType_TN, "dn0")
	logger.Log("step 2", log.DefaultLogOptions().WithProcess(log.Txn, id))
}

func processStep3InLOG(id string) {
	logger := getServiceLogger(metadata.ServiceType_LOG, "log0")
	logger.Log("step 3", log.DefaultLogOptions().WithProcess(log.Txn, id))
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (LogOptions) WithSample

func (opts LogOptions) WithSample(sampleType SampleType) LogOptions

WithSample sample print the log, using log counts as sampling frequency. First time must output.

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")

	n := 2
	for i := 0; i < n; i++ {
		logger.Log("example sample log",
			log.DefaultLogOptions().WithSample(log.ExampleSample))
	}
	// Output logs:
	// 2022/11/17 15:43:14.645242 +0800 INFO cn-service log/example_log_test.go:116 example sample log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

type MOLogger

type MOLogger struct {
	// contains filtered or unexported fields
}

MOLogger mo logger based zap.logger. To standardize and standardize the logging output of MO, the native zap.logger should not be used for logging in MO, but rather MOLogger. MOLogger is compatible with the zap.logger log printing method signature

func GetModuleLogger

func GetModuleLogger(logger *MOLogger, module Module) *MOLogger

GetModuleLogger returns the module logger, it will add ".module" to logger name. e.g. if the logger's name is cn-service, module is txn, the new logger's name is "cn-service.txn".

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	cnServiceLogger := getServiceLogger(metadata.ServiceType_CN, "cn0")
	txnClientLogger := log.GetModuleLogger(cnServiceLogger, log.TxnClient)
	txnClientLogger.Info("this is a info log")
	// Output logs:
	// 2022/11/17 15:27:24.562799 +0800 INFO cn-service.txn-client log/example_log_test.go:40 this is a info log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func GetServiceLogger

func GetServiceLogger(logger *zap.Logger, service metadata.ServiceType, uuid string) *MOLogger

GetServiceLogger returns service logger, it will using the service as the logger name, and append FieldNameServiceUUID field to the logger

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
	logger.Info("this is a info log")
	// Output logs:
	// 2022/11/17 15:25:49.375367 +0800 INFO cn-service log/example_log_test.go:32 this is a info log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) Debug

func (l *MOLogger) Debug(msg string, fields ...zap.Field) bool

Debug shortcuts to print debug log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Debug("this is a debug log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 DEBUG cn-service log/example_log_test.go:52 this is a debug log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) DebugAction added in v0.8.0

func (l *MOLogger) DebugAction(msg string, fields ...zap.Field) func()

InfoDebugAction shortcuts to print debug action log

func (*MOLogger) Enabled

func (l *MOLogger) Enabled(level zapcore.Level) bool

Enabled returns true if the level is enabled

func (*MOLogger) Error

func (l *MOLogger) Error(msg string, fields ...zap.Field) bool

Error shortcuts to print error log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Error("this is a error log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 ERROR cn-service log/example_log_test.go:58 this is a error log {"uuid": "cn0"}
	// error stacks
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) Fatal

func (l *MOLogger) Fatal(msg string, fields ...zap.Field) bool

Fatal shortcuts to print fatal log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Fatal("this is a fatal log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 FATAL cn-service log/example_log_test.go:78 this is a fatal log {"uuid": "cn0"}
	// fatal stacks...
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) Info

func (l *MOLogger) Info(msg string, fields ...zap.Field) bool

Info shortcuts to print info log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Info("this is a info log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 INFO cn-service log/example_log_test.go:46 this is a info log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) InfoAction

func (l *MOLogger) InfoAction(msg string, fields ...zap.Field) func()

InfoAction shortcuts to print info action log

func (*MOLogger) Log

func (l *MOLogger) Log(msg string, opts LogOptions, fields ...zap.Field) bool

Log is the entry point for mo log printing. Return true to indicate that the log is being recorded by the current LogContext.

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
	"go.uber.org/zap"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Log("this is a example log",
		log.DefaultLogOptions(),
		zap.String("field-1", "field-1"),
		zap.String("field-2", "field-2"))
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 INFO cn-service log/example_log_test.go:85 this is a example log {"uuid": "cn0", "field-1": "field-1", "field-2": "field-2"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) LogAction

func (l *MOLogger) LogAction(action string, opts LogOptions, fields ...zap.Field) func()

LogAction used to log an action, or generate 2 logs, the first log occurring at the place where the call is made and the second log occurring at the end of the function where the LogAction is called, with the additional time consuming. e.g.:

func LogActionExample() {
    defer log.Info(zapLogger).LogAction("example action")()
}

This method should often be used to log the elapsed time of a function and, as the logs appear in pairs, can also be used to check whether a function has been executed.

Example
package main

import (
	"time"

	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	someAction()
	// Output logs:
	// 2022/11/17 15:28:15.599321 +0800 INFO cn-service log/example_log_test.go:125 do action {"uuid": "cn0"}
	// 2022/11/17 15:28:16.600754 +0800 INFO cn-service log/example_log_test.go:127 do action {"uuid": "cn0", "cost": "1.001430792s"}
}

func someAction() {
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
	defer logger.InfoAction("do action")()
	time.Sleep(time.Second)
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) Named

func (l *MOLogger) Named(name string) *MOLogger

Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.

func (*MOLogger) Panic

func (l *MOLogger) Panic(msg string, fields ...zap.Field) bool

Panic shortcuts to print panic log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Panic("this is a panic log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 PANIC cn-service log/example_log_test.go:71 this is a panic log {"uuid": "cn0"}
	// panic stacks...
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) RawLogger

func (l *MOLogger) RawLogger() *zap.Logger

RawLogger returns the raw zap logger

func (*MOLogger) Warn

func (l *MOLogger) Warn(msg string, fields ...zap.Field) bool

Warn shortcuts to print warn log

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	getServiceLogger(metadata.ServiceType_CN, "cn0").Warn("this is a warn log")
	// Output logs:
	// 2022/11/17 15:27:52.036861 +0800 WARN cn-service log/example_log_test.go:65 this is a warn log {"uuid": "cn0"}
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

func (*MOLogger) With

func (l *MOLogger) With(fields ...zap.Field) *MOLogger

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 (*MOLogger) WithContext

func (l *MOLogger) WithContext(ctx context.Context) *MOLogger
Example
package main

import (
	"context"

	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
	"github.com/matrixorigin/matrixone/pkg/util/trace"
)

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}

func main() {
	ctx, span := trace.Start(context.Background(), "ExampleMOLogger_WithContext")
	defer span.End()
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0").WithContext(ctx)
	logger.Info("info log 1, with Context in MOLogger.")
	logger.Info("info log 2, with Context in MOLogger.")
	// 2022/12/17 16:37:22.995805 +0800 INFO cn-service log/example_log_test.go:152 info log 1, with Context in MOLogger. {"uuid": "cn0", "span": {"trace_id": "349315cb-2044-f2dd-b4b1-e20365c8a944", "span_id": "fd898a22b375f34e"}}
	// 2022/12/17 16:37:22.995812 +0800 INFO cn-service log/example_log_test.go:153 info log 2, with Context in MOLogger. {"uuid": "cn0", "span": {"trace_id": "349315cb-2044-f2dd-b4b1-e20365c8a944", "span_id": "fd898a22b375f34e"}}
}
Output:

func (*MOLogger) WithOptions added in v0.8.0

func (l *MOLogger) WithOptions(opts ...zap.Option) *MOLogger

WithOptions creates a child logger with zap options.

func (*MOLogger) WithProcess

func (l *MOLogger) WithProcess(process Process) *MOLogger

WithProcess if the current log belongs to a certain process, the process name and process ID can be recorded. When analyzing the log, all related logs can be retrieved according to the process ID.

Example
package main

import (
	"github.com/matrixorigin/matrixone/pkg/common/log"
	"github.com/matrixorigin/matrixone/pkg/logutil"
	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
)

func main() {
	processStep1InCNWithoutID()
	processStep2InTNWithoutID()
	processStep3InLOGWithoutID()

	// Output logs:
	// 2022/11/17 15:36:04.724470 +0800 INFO cn-service log/example_log_test.go:131 step 1 {"uuid": "cn0", "process": "txn", "process-id": "txn uuid"}
	// 2022/11/17 15:36:04.724797 +0800 INFO dn-service log/example_log_test.go:136 step 2 {"uuid": "dn0", "process": "txn", "process-id": "txn uuid"}
	// 2022/11/17 15:36:04.724812 +0800 INFO log-service log/example_log_test.go:141 step 3 {"uuid": "log0", "process": "txn", "process-id": "txn uuid"}
}

func processStep1InCNWithoutID() {
	logger := getServiceLogger(metadata.ServiceType_CN, "cn0").WithProcess(log.Txn)
	logger.Log("step 1", log.DefaultLogOptions())
}

func processStep2InTNWithoutID() {
	logger := getServiceLogger(metadata.ServiceType_TN, "dn0").WithProcess(log.Txn)
	logger.Log("step 2", log.DefaultLogOptions())
}

func processStep3InLOGWithoutID() {
	logger := getServiceLogger(metadata.ServiceType_LOG, "log0").WithProcess(log.Txn)
	logger.Log("step 3", log.DefaultLogOptions())
}

func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
}
Output:

type Module

type Module string

Module used to describe which component module the log belongs to

type Process

type Process string

Process used to describe which process the log belongs to. We can filter out all process-related logs by specifying the process field as the value to analyse the logs.

type SampleType

type SampleType int

SampleType there are some behaviours in the system that print debug logs frequently, such as the scheduling of HAKeeper, which may print hundreds of logs a second. What these logs do is that these behaviours are still happening at debug level. So we just need to sample the output.

const (
	SystemInitSample SampleType
	// ExampleSample used in examples
	ExampleSample = math.MaxInt
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL