log

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

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 (
	// LinuxDaemonLogPath is the log path for daemon mode features on linux.
	LinuxDaemonLogPath = `/var/log/google-cloud-sap-agent.log`
	// WindowsDaemonLogPath is the log path for daemon mode features on windows.
	WindowsDaemonLogPath = `C:\Program Files\Google\google-cloud-sap-agent\logs\google-cloud-sap-agent.log`

	// LinuxOneTimeLogPrefix is the prefix of the log path to be used by One Time Execution features.
	LinuxOneTimeLogPrefix = `/var/log/google-cloud-sap-agent-`
	// WindowsOneTimeLogPrefix is the prefix of the log path to be used by One Time Execution features.
	WindowsOneTimeLogPrefix = `C:\Program Files\Google\google-cloud-sap-agent\logs\google-cloud-sap-agent-`
)

Variables

Logger used for logging structured messages

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 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 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 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.

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              cpb.Configuration_LogLevel
	SubCommandName     string
	LogFileName        string
	CloudLogName       string
}

Parameters for setting up logging

func SetupDaemonLogging

func SetupDaemonLogging(params Parameters) Parameters

SetupDaemonLogging creates logging config for the agent's daemon mode.

func SetupOneTimeLogging

func SetupOneTimeLogging(params Parameters, subcommandName string) Parameters

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

Jump to

Keyboard shortcuts

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