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
- Variables
- func CloudLoggingClient(ctx context.Context, projectID string) *logging.Client
- func CloudLoggingClientWithUserAgent(ctx context.Context, projectID string, userAgent string) *logging.Client
- func CreateClient(ctx context.Context, projectID string) (*logging.Client, error)
- func CreateClientWithUserAgent(ctx context.Context, projectID string, userAgent string) (*logging.Client, error)
- func CreateWindowsLogBasePath() string
- func CtxLogger(ctx context.Context) *zap.SugaredLogger
- func DefaultOTEPath(agentName string, osType string, logFilePath string) string
- func Error(err error) zap.Field
- func Float64(key string, value float64) zap.Field
- func FlushCloudLog()
- func GetLevel() string
- func GetLogFile() string
- func Int64(key string, value int64) zap.Field
- func OTEFilePath(agentName string, oteName string, osType string, logFilePath string) string
- func Print(msg string)
- func SetCtx(ctx context.Context, key, value string) context.Context
- func SetUpLoggingForCloudRun(params Parameters)
- func SetupLogging(params Parameters)
- func SetupLoggingForTest()
- func SetupLoggingToDiscard()
- func String(key string, value string) zap.Field
- func StringLevelToZapcore(level string) zapcore.Level
- type CloudCore
- func (c *CloudCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry
- func (c *CloudCore) Enabled(l zapcore.Level) bool
- func (c *CloudCore) Sync() error
- func (c *CloudCore) With(additionalFields []zapcore.Field) zapcore.Core
- func (c *CloudCore) Write(ze zapcore.Entry, additionalFields []zapcore.Field) error
- type GoogleCloudLogger
- type Parameters
Constants ¶
const CtxKey contextKeyType = "context"
CtxKey is a key of the type contextKeyType for context logging.
Variables ¶
var ( // Logger used for logging structured messages Logger *zap.SugaredLogger )
Functions ¶
func CloudLoggingClient ¶
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 ¶
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 ¶
DefaultOTEPath returns the default OTE path for the agent/command. {COMMAND} is a placeholder for the command name.
func FlushCloudLog ¶
func FlushCloudLog()
FlushCloudLog will flush any buffered log entries to cloud logging if it is enabled.
func OTEFilePath ¶
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 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 StringLevelToZapcore ¶
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 ¶
func (c *CloudCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry
Check implements zapcore.Core.
type GoogleCloudLogger ¶
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.