log

package
v0.0.0-...-ce264d9 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: Apache-2.0 Imports: 13 Imported by: 9

Documentation

Overview

Package log provides a wrapper to the underlying logging library.

Logging should be done by accessing the log.Logger object to issue log statements.

Using any of the base loggers (Debug, Info, Warn, Error) any additional fields will be concatenated onto the "msg" field. This may cause serialization issues.

It's suggested to use the "with" log methods (Debugw, Infow, Warnw, Errorw) to include additional fields in the log line.

Examples:

  Log with additional fields in JSON:
	  log.Logger.Infow("My message to log", "key1", "value1", "key2", 9999, "key3", errors.New("an error"))
		or
		log.Logger.Infow("My message to log", log.String("key1", "value1"))

	Log data directly into the message:
	  log.Logger.Info("My message to log with data", " key1: ", SOMEVAR, " key2: ", 9999)

NOT RECOMMENDED

Log data in the message and additional fields:
	log.Logger.Infow(fmt.Sprintf("My message to log with data 5v"), "error", error)

Log data directly into the message without serialization:
	log.Logger.Infof("My message to log with data %v", 77.33232)
	log.Logger.Infof("My message to log with data %d", 77.33232)

Note:

    When using the logger if you use the "f" methods such as Infof the data in the message will
		be serialized unless you use Sprintf style in the message and the correct type on the data
		such as %v, %s, %d.  The suggested approach is to separate any data into separate fields for
		logging.

Index

Constants

View Source
const CtxKey contextKeyType = "context"

CtxKey is a key of the type contextKeyType for context logging.

Variables

View Source
var (
	// Logger used for logging structured messages
	Logger *zap.SugaredLogger
)

Functions

func CloudLoggingClient

func CloudLoggingClient(ctx context.Context, projectID string) *logging.Client

CloudLoggingClient create a logging.Client for writing logs to CloudLogging, will be nil if a ping fails.

func CloudLoggingClientWithUserAgent

func CloudLoggingClientWithUserAgent(ctx context.Context, projectID string, userAgent string) *logging.Client

CloudLoggingClientWithUserAgent create a logging.Client for writing logs to CloudLogging, will be nil if a ping fails.

func CreateClient

func CreateClient(ctx context.Context, projectID string) (*logging.Client, error)

CreateClient creates a logging.Client for writing logs to CloudLogging.

func CreateClientWithUserAgent

func CreateClientWithUserAgent(ctx context.Context, projectID string, userAgent string) (*logging.Client, error)

CreateClientWithUserAgent creates a logging.Client for writing logs to CloudLogging and overrides the user agent.

func CreateWindowsLogBasePath

func CreateWindowsLogBasePath() string

CreateWindowsLogBasePath returns the base path to either the program data or files directory.

func CtxLogger

func CtxLogger(ctx context.Context) *zap.SugaredLogger

CtxLogger returns a zap logger with as much context as possible

func DefaultOTEPath

func DefaultOTEPath(agentName string, osType string, logFilePath string) string

DefaultOTEPath returns the default OTE path for the agent/command. {COMMAND} is a placeholder for the command name.

func Error

func Error(err error) zap.Field

Error creates a zap Field for an error.

func Float64

func Float64(key string, value float64) zap.Field

Float64 creates a zap Field for a float64.

func FlushCloudLog

func FlushCloudLog()

FlushCloudLog will flush any buffered log entries to cloud logging if it is enabled.

func GetLevel

func GetLevel() string

GetLevel will return the current logging level as a string.

func GetLogFile

func GetLogFile() string

GetLogFile will return the current logfile as a string.

func Int64

func Int64(key string, value int64) zap.Field

Int64 creates a zap Field for an int64.

func OTEFilePath

func OTEFilePath(agentName string, oteName string, osType string, logFilePath string) string

OTEFilePath returns the log file path for the OTE invoked depending if it is invoked internally or via command line.

func Print

func Print(msg string)

Print will use the go default log to print messages to the console, should only be used by main.

func SetCtx

func SetCtx(ctx context.Context, key, value string) context.Context

SetCtx returns context objects with additional key value pairs.

func SetUpLoggingForCloudRun

func SetUpLoggingForCloudRun(params Parameters)

SetUpLoggingForCloudRun sets up logging for Cloud Run.

func SetupLogging

func SetupLogging(params Parameters)

SetupLogging uses the agent configuration to set up the file Logger.

func SetupLoggingForTest

func SetupLoggingForTest()

SetupLoggingForTest creates the Logger to log to the console during unit tests.

func SetupLoggingToDiscard

func SetupLoggingToDiscard()

SetupLoggingToDiscard provides the configuration of the Logger to discard all logs. Discarding logs is only used when the agent is run remotely during workload manager metrics remote collection.

func String

func String(key string, value string) zap.Field

String creates a zap Field for a string.

func StringLevelToZapcore

func StringLevelToZapcore(level string) zapcore.Level

StringLevelToZapcore returns the equivalent of the string log level. It defaults to info level in case unknown log level is identified.

Types

type CloudCore

type CloudCore struct {
	GoogleCloudLogger GoogleCloudLogger
	LogLevel          zapcore.Level
}

CloudCore that will be used as a zapcore.Core

func (*CloudCore) Check

Check implements zapcore.Core.

func (*CloudCore) Enabled

func (c *CloudCore) Enabled(l zapcore.Level) bool

Enabled implements zapcore.Core.

func (*CloudCore) Sync

func (c *CloudCore) Sync() error

Sync implements zapcore.Core, flushes the CloudCore's GoogleCloudLogger.

func (*CloudCore) With

func (c *CloudCore) With(additionalFields []zapcore.Field) zapcore.Core

With implements zapcore.Core.

func (*CloudCore) Write

func (c *CloudCore) Write(ze zapcore.Entry, additionalFields []zapcore.Field) error

Write implements zapcore.Core. It writes a log entry to Cloud Logging.

type GoogleCloudLogger

type GoogleCloudLogger interface {
	Flush() error
	Log(e logging.Entry)
}

GoogleCloudLogger interface

type Parameters

type Parameters struct {
	LogToCloud         bool
	CloudLoggingClient *logging.Client
	OSType             string
	Level              zapcore.Level
	LogFileName        string
	LogFilePath        string
	CloudLogName       string
}

Parameters for setting up logging

func SetupLoggingForOTE

func SetupLoggingForOTE(agentName, command string, params Parameters) Parameters

SetupLoggingForOTE creates logging config for the agent's one time execution.

Directories

Path Synopsis
Package fake implements a fake version of the Cloud Logging interface.
Package fake implements a fake version of the Cloud Logging interface.

Jump to

Keyboard shortcuts

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