Class LoggingHandler
- java.lang.Object
-
- java.util.logging.Handler
-
- com.google.cloud.logging.LoggingHandler
-
public class LoggingHandler extends Handler
A logging handler that outputs logs generated withLoggerto Cloud Logging.Java logging levels (see
Level) are mapped to the following Google Cloud Logging severities:Mapping of Java logging level to Cloud Logging severities Java Level Cloud Logging Severity SEVERE ERROR WARNING WARNING INFO INFO CONFIG INFO FINE DEBUG FINER DEBUG FINEST DEBUG Original Java logging levels are added as labels (with
levelNameandlevelValuekeys, respectively) to the corresponding Cloud LoggingLogEntry. You can read entry labels usingLogEntry.getLabels(). To use logging levels that correspond to Cloud Logging severities you can useLoggingLevel.Configuration: By default each
LoggingHandleris initialized using the followingLogManagerconfiguration properties (that you can set in thelogging.propertiesfile). If properties are not defined (or have invalid values) then the specified default values are used.com.google.cloud.logging.LoggingHandler.logthe log name (defaults tojava.log).com.google.cloud.logging.LoggingHandler.levelspecifies the default level for the handler (defaults toLevel.INFO).com.google.cloud.logging.LoggingHandler.filterspecifies the name of aFilterclass to use (defaults to no filter).com.google.cloud.logging.LoggingHandler.formatterspecifies the name of aFormatterclass to use (defaults toSimpleFormatter).com.google.cloud.logging.LoggingHandler.flushLevelspecifies the flush log level. When a log with this level is published, logs are transmitted to the Cloud Logging service (defaults toLoggingLevel.ERROR).com.google.cloud.logging.LoggingHandler.enhancersspecifies a comma separated list ofLoggingEnhancerclasses. This handler will call each enhancer list whenever it builds aLogEntryinstance (defaults to empty list).com.google.cloud.logging.LoggingHandler.resourceTypethe type name to use when creating the defaultMonitoredResource(defaults to auto-detected resource type, else "global").com.google.cloud.logging.Synchronicitythe synchronicity of the write method to use to write logs to the Cloud Logging service (defaults toSynchronicity.ASYNC).com.google.cloud.logging.LoggingHandler.autoPopulateMetadatais a boolean flag that opts-out the population of the log entries metadata before the logs are sent to Cloud Logging (defaults totrue).com.google.cloud.logging.LoggingHandler.redirectToStdoutis a boolean flag that opts-in redirecting the output of the handler to STDOUT instead of ingesting logs to Cloud Logging using Logging API (defaults tofalse). Redirecting logs can be used in Google Cloud environments with installed logging agent to delegate log ingestions to the agent. Redirected logs are formatted as one line Json string following the structured logging guidelines. This flag is deprecated; usecom.google.cloud.logging.LoggingHandler.logTargetinstead.com.google.cloud.logging.LoggingHandler.logTargetis an enumeration controlling log routing (defaults toCLOUD_LOGGING). If set to STDOUT or STDERR, logs will be printed to the corresponding stream in the Json format that can be parsed by the logging agent. If set to CLOUD_LOGGING, logs will be sent directly to the Google Cloud Logging API.
To add a
LoggingHandlerto an existingLoggerand be sure to avoid infinite recursion when logging, use theaddHandler(Logger, LoggingHandler)method. Alternatively you can add the handler vialogging.properties. For example using the following line:com.example.mypackage.handlers=com.google.cloud.logging.LoggingHandler- See Also:
- Structured logging
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLoggingHandler.LogTargetWhere to send logs.
-
Constructor Summary
Constructors Constructor Description LoggingHandler()Creates an handler that publishes messages to Cloud Logging.LoggingHandler(String log)Creates a handler that publishes messages to Cloud Logging.LoggingHandler(String log, LoggingOptions options)Creates a handler that publishes messages to Cloud Logging.LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource)Creates a handler that publishes messages to Cloud Logging.LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource, List<LoggingEnhancer> enhancers)Creates a handler that publishes messages to Cloud Logging.LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource, List<LoggingEnhancer> enhancers, LogDestinationName destination)Creates a handler that publishes messages to Cloud Logging.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddHandler(Logger logger, LoggingHandler handler)Adds the providedLoggingHandlertologger.voidclose()Closes the handler and the associatedLoggingobject.voidflush()BooleangetAutoPopulateMetadata()Gets the metadata auto population flag.LevelgetFlushLevel()Get the flush log level.LoggingHandler.LogTargetgetLogTarget()BooleangetRedirectToStdout()Deprecated.SynchronicitygetSynchronicity()Get the flush log level.protected LogEntry.BuilderlogEntryFor(LogRecord record)voidpublish(LogRecord record)voidsetAutoPopulateMetadata(boolean value)Sets the metadata auto population flag.voidsetFlushLevel(Level flushLevel)Sets minimum logging level to log immediately and flush any pending writes.voidsetLogTarget(LoggingHandler.LogTarget value)Configure the destination for ingested logs.voidsetRedirectToStdout(boolean value)Deprecated.voidsetSynchronicity(Synchronicity synchronicity)Sets synchronicity of logging writes.-
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
-
-
-
-
Constructor Detail
-
LoggingHandler
public LoggingHandler()
Creates an handler that publishes messages to Cloud Logging.
-
LoggingHandler
public LoggingHandler(String log)
Creates a handler that publishes messages to Cloud Logging.- Parameters:
log- the name of the log to which log entries are written
-
LoggingHandler
public LoggingHandler(String log, LoggingOptions options)
Creates a handler that publishes messages to Cloud Logging.- Parameters:
log- the name of the log to which log entries are writtenoptions- options for the Cloud Logging service
-
LoggingHandler
public LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource)
Creates a handler that publishes messages to Cloud Logging.- Parameters:
log- the name of the log to which log entries are writtenoptions- options for the Cloud Logging servicemonitoredResource- the monitored resource to which log entries refer. If it is null then a default resource is created based on the project ID and deployment environment.
-
LoggingHandler
public LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource, List<LoggingEnhancer> enhancers)
Creates a handler that publishes messages to Cloud Logging.- Parameters:
log- the name of the log to which log entries are writtenoptions- options for the Cloud Logging servicemonitoredResource- the monitored resource to which log entries refer. If it is null then a default resource is created based on the project ID and deployment environment.enhancers- List ofLoggingEnhancerinstances used to enhance anyLogEntryinstances built by this handler.
-
LoggingHandler
public LoggingHandler(String log, LoggingOptions options, com.google.cloud.MonitoredResource monitoredResource, List<LoggingEnhancer> enhancers, LogDestinationName destination)
Creates a handler that publishes messages to Cloud Logging. Auto-population of the logs metadata can be opted-out inoptionsargument or in the configuration file. At least one flagLoggingOptionsorLoggingConfighas to be explicitly set tofalsein order to opt-out the metadata auto-population.- Parameters:
log- the name of the log to which log entries are writtenoptions- options for the Cloud Logging servicemonitoredResource- the monitored resource to which log entries refer. If it is null then a default resource is created based on the project ID and deployment environment.enhancers- List ofLoggingEnhancerinstances used to enhance anyLogEntryinstances built by this handler.destination- the log destinationLogDestinationName(see 'logName' parameter in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
-
-
Method Detail
-
logEntryFor
protected LogEntry.Builder logEntryFor(LogRecord record)
-
close
public void close()
Closes the handler and the associatedLoggingobject.
-
getFlushLevel
public Level getFlushLevel()
Get the flush log level.
-
setFlushLevel
public void setFlushLevel(Level flushLevel)
Sets minimum logging level to log immediately and flush any pending writes.- Parameters:
flushLevel- minimum log level to trigger flush
-
setSynchronicity
public void setSynchronicity(Synchronicity synchronicity)
Sets synchronicity of logging writes. By default, writes are asynchronous.- Parameters:
synchronicity-Synchronicity
-
getSynchronicity
public Synchronicity getSynchronicity()
Get the flush log level.
-
setAutoPopulateMetadata
public void setAutoPopulateMetadata(boolean value)
Sets the metadata auto population flag.
-
getAutoPopulateMetadata
public Boolean getAutoPopulateMetadata()
Gets the metadata auto population flag.
-
setRedirectToStdout
@Deprecated public void setRedirectToStdout(boolean value)
Deprecated.Enable/disable redirection to STDOUT. If set totrue, logs will be printed to STDOUT in the Json format that can be parsed by the logging agent. If set tofalse, logs will be ingested to Cloud Logging by calling Logging API.This method is mutually exclusive with
setLogTarget(LogTarget).
-
getRedirectToStdout
@Deprecated public Boolean getRedirectToStdout()
Deprecated.
-
setLogTarget
public void setLogTarget(LoggingHandler.LogTarget value)
Configure the destination for ingested logs. If set to STDOUT or STDERR, logs will be printed to the corresponding stream in the Json format that can be parsed by the logging agent. If set to CLOUD_LOGGING, logs will be sent directly to the Google Cloud Logging API.This method is mutually exclusive with
setRedirectToStdout(boolean).
-
getLogTarget
public LoggingHandler.LogTarget getLogTarget()
-
addHandler
public static void addHandler(Logger logger, LoggingHandler handler)
Adds the providedLoggingHandlertologger. Use this method to register Cloud Logging handlers instead ofLogger.addHandler(Handler)to avoid infinite recursion when logging.
-
-