Package logging
Overview ▹
Index ▹
Variables
var DATADOG_API_KEY string
var DATADOG_LOGS bool
var DATADOG_LOGS_URL string
var DATADOG_METRICS bool
var DATADOG_METRICS_URL string
var DATADOG_SERVICE_NAME string
var DATADOG_SOURCE string
var DATADOG_STAGE string
var DATADOG_VERSION string
Initialize config variables
var ERROR_FILE_LOCATION string
var LOCAL_LOGS bool
var LOCAL_LOGS_LOCATION string
var LOG_LEVEL string
func InitLogger ¶
func InitLogger(GlobalConfig *config.Config)
InitLogger initializes the global logger.
The function creates a new zap logger with the specified configuration and sets the global logger variable to the new logger.
Parameters:
- GlobalConfig: The global configuration from the config package.
func entryCallerToString ¶
func entryCallerToString(ec zapcore.EntryCaller) string
entryCallerToString converts a zapcore.EntryCaller to a string representation.
Parameters:
- ec: The zapcore.EntryCaller to be converted.
Returns:
- string: The string representation of the zapcore.EntryCaller.
func initLoggerConfig ¶
func initLoggerConfig(config Config)
initLoggerConfig initializes the global configuration variables for the logging package.
The function sets the global configuration variables to the values specified in the provided Config struct.
Parameters:
- config: The Config struct containing the configuration values to set.
func levelToString ¶
func levelToString(level zapcore.Level) string
levelToString converts a zapcore.Level to its string representation.
Parameters:
- level: The zapcore.Level to be converted.
Returns:
- string: The string representation of the zapcore.Level.
func mapsToJSONBytes ¶
func mapsToJSONBytes(maps []map[string]interface{}) ([]byte, error)
mapsToJSONBytes converts a slice of maps to a JSON-encoded byte slice. It takes an array of maps, marshals it to JSON format, and returns the resulting byte slice.
Parameters:
- maps: The slice of maps to be converted.
Returns:
- []byte: The JSON-encoded byte slice.
- error: An error if the conversion fails.
func sendLogs ¶
func sendLogs(ctx *ContextMap, level zapcore.Level, time time.Time, message string, caller zapcore.EntryCaller, stack string, function string, arguments ...interface{})
The function constructs a log entry with the specified parameters and context, and then sends it to Datadog if enabled in the global configuration. It also writes log entries to a local file if local logs are enabled. If any errors occur during this process, they are logged and written to the local error log file. Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- level: The log entry's severity level (e.g., Debug, Info, Error).
- time: The timestamp of the log entry.
- message: The log message.
- caller: Information about the caller of the log entry.
- stack: The stack trace of the log entry.
- function: The function where the log entry was created.
- arguments: Additional log entry arguments.
func sendMetrics ¶
func sendMetrics(name string, count float64)
sendMetrics sends a metric to Datadog using the specified name and count. The function creates a metric object, converts it to JSON, and sends a POST request to Datadog's metrics endpoint.
The function constructs a Metrics object containing the metric data and associated resource information. It then converts the object to JSON and sends it as a POST request to Datadog for metrics reporting. Any errors encountered during this process are logged and written to the local error log file.
Parameters:
- name: The name of the metric.
- count: The value of the metric.
func sendPostRequestToDatadog ¶
func sendPostRequestToDatadog(url string, requestBody []byte, apiKey string) (*http.Response, error)
sendPostRequestToDatadog sends the metric or logs post request to Datadog.
Parameters:
- url: The URL to which the request is sent.
- requestBody: The request body.
- apiKey: The Datadog API key.
Returns:
- *http.Response: The response from the POST request.
- error: An error if the POST request fails.
func timeToString ¶
func timeToString(t time.Time) string
timeToString converts a time.Time value to a string representation using the "2006-01-02 15:04:05.000" layout.
Parameters:
- t: The time.Time value to be converted.
Returns:
- string: The string representation of the time.Time value.
func writeInterfaceToFile ¶
func writeInterfaceToFile(filename string, data interface{}) error
writeInterfaceToFile writes data, which is an interface{} representing structured data, to a file in JSON format. It adds a timestamp to each entry.
Parameters:
- filename: The name of the file to write to.
- data: The structured data to be written to the file.
Returns:
- error: An error if writing to the file fails.
func writeStringToFile ¶
func writeStringToFile(filename string, data string) error
writeStringToFile appends a string message to a file, including a timestamp.
Parameters:
- filename: The name of the file to write to.
- data: The string message to be written to the file.
Returns:
- error: An error if writing to the file fails.
type Config ¶
Config represents the configuration for the logging package.
type Config struct { ErrorFileLocation string LogLevel string LocalLogs bool LocalLogsLocation string DatadogLogs bool DatadogSource string DatadogStage string DatadogVersion string DatadogService string DatadogAPIKey string DatadogLogsURL string DatadogMetrics bool DatadogMetricsURL string }
type ContextKey ¶
ContextKey defines the supported context keys.
type ContextKey string
const ( InstructionGuid ContextKey = "instructionGuid" AdapterType ContextKey = "adapterType" WatchFolderPath ContextKey = "watchFolderPath" WatchFilePath ContextKey = "watchFilePath" ReaderGuid ContextKey = "readerGuid" ClientGuid ContextKey = "clientGuid" Action ContextKey = "action" Rest_Call_Id ContextKey = "restCallId" Rest_Call ContextKey = "restCall" UserMail ContextKey = "userMail" )
type ContextMap ¶
ContextMap represents a context for managing key-value pairs with specific context keys. It allows setting, retrieving, and copying context data associated with various keys.
type ContextMap struct { data sync.Map }
func (*ContextMap) Copy ¶
func (ctx *ContextMap) Copy() *ContextMap
Copy function copies the current contextMap so new uses of Set do not overwrite existing values
Returns:
- *ContextMap: A copy of the current ContextMap.
func (*ContextMap) Get ¶
func (ctx *ContextMap) Get(key ContextKey) (interface{}, bool)
Get function retrieves the value for a ContextKey
Parameters:
- key: The ContextKey for which to retrieve the value.
Returns:
- interface{}: The value associated with the specified ContextKey.
- bool: A boolean indicating whether the ContextKey exists.
func (*ContextMap) Set ¶
func (ctx *ContextMap) Set(key ContextKey, value interface{})
Set function sets ContextKeys equal to any value
type Metric ¶
Metric represents a time series metric.
type Metric struct { Metric string `json:"metric"` Type int `json:"type"` Points []Point `json:"points"` Resources []Resource `json:"resources"` }
type Metrics ¶
Metrics represents a collection of metrics.
type Metrics struct { Series []Metric `json:"series"` }
type Point ¶
Point represents a data point in a time series metric.
type Point struct { Timestamp int64 `json:"timestamp"` Value float64 `json:"value"` }
type Resource ¶
Resource represents a named resource associated with a metric.
type Resource struct { Name string `json:"name"` Type string `json:"type"` }
type loggerWrapper ¶
loggerWrapper represents a wrapper for the zap.Logger to provide custom logging functionality.
type loggerWrapper struct { lw *zap.Logger }
Initialize the global logger variable.
var Log loggerWrapper
func (*loggerWrapper) Debugf ¶
func (logger *loggerWrapper) Debugf(ctx *ContextMap, format string, args ...interface{})
Debugf logs a formatted message with Debug level if the global log level is set to "debug."
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- format: The format of the log message.
- args: The log message.
func (*loggerWrapper) Error ¶
func (logger *loggerWrapper) Error(ctx *ContextMap, args ...interface{})
Error logs a message with Error level if the global log level is not set to "fatal".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- args: The log message.
func (*loggerWrapper) Errorf ¶
func (logger *loggerWrapper) Errorf(ctx *ContextMap, format string, args ...interface{})
Errorf logs a formatted message with Error level if the global log level is not set to "fatal".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- format: The format of the log message.
- args: The log message.
func (*loggerWrapper) Fatal ¶
func (logger *loggerWrapper) Fatal(ctx *ContextMap, args ...interface{})
Fatal logs a message with Fatal level and terminates the program.
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- args: The log message.
func (*loggerWrapper) Fatalf ¶
func (logger *loggerWrapper) Fatalf(ctx *ContextMap, format string, args ...interface{})
Fatalf logs a formatted message with Fatal level and terminates the program.
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- format: The format of the log message.
- args: The log message.
func (*loggerWrapper) Info ¶
func (logger *loggerWrapper) Info(ctx *ContextMap, args ...interface{})
Info logs a message with Error level if the global log level is not set to "warn".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- args: The log message.
func (*loggerWrapper) Infof ¶
func (logger *loggerWrapper) Infof(ctx *ContextMap, format string, args ...interface{})
Infof logs a message with Error level if the global log level is not set to "warn".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- format: The format of the log message.
- args: The log message.
func (*loggerWrapper) Metrics ¶
func (logger *loggerWrapper) Metrics(name string, count float64)
Metrics sends a metric event with the specified name and count to Datadog if Datadog metrics are enabled.
Parameters:
- name: The name of the metric.
- count: The value of the metric.
func (*loggerWrapper) Warn ¶
func (logger *loggerWrapper) Warn(ctx *ContextMap, args ...interface{})
Warn logs a message with Error level if the global log level is not set to "error".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- args: The log message.
func (*loggerWrapper) Warnf ¶
func (logger *loggerWrapper) Warnf(ctx *ContextMap, format string, args ...interface{})
Warnf logs a message with Error level if the global log level is not set to "error".
Parameters:
- ctx: A ContextMap containing context information to be included in the log entry.
- format: The format of the log message.
- args: The log message.