def log(logger, severity, message, *args, extras=None): """Log a message with severity |severity|. If using stack driver logging then |extras| is also logged (in addition to default extras).""" message = str(message) if args: message = message % args if utils.is_local(): if extras: message += ' Extras: ' + str(extras) logging.log(severity, message) return if logger is None: logger = _default_logger assert logger struct_message = { 'message': message, } all_extras = _default_extras.copy() extras = extras or {} all_extras.update(extras) struct_message.update(all_extras) severity = LogSeverity(severity).name logger.log_struct(struct_message, severity=severity)
def __init__(self, name, default_extras=None, log_level=logging.INFO): if not utils.is_local(): _initialize_cloud_clients() self.logger = _log_client.logger(name) else: self.logger = logging.getLogger(name) logging.getLogger(name).setLevel(log_level) logging.getLogger(name).addFilter(LengthFilter()) self.default_extras = default_extras if default_extras else {}
def _initialize_cloud_clients(): """Initialize clients for Google Cloud Logging and Error reporting.""" assert not utils.is_local() global _log_client if _log_client: return _log_client = google.cloud.logging.Client() logging_handler = CloudLoggingHandler(_log_client) logging.getLogger().addHandler(logging_handler) global _error_reporting_client _error_reporting_client = error_reporting.Client()
def initialize(name='fuzzbench', default_extras=None, log_level=logging.INFO): """Initializes stackdriver logging if running on Google Cloud.""" logging.getLogger().setLevel(log_level) logging.getLogger().addFilter(LengthFilter()) # Don't log so much with SQLalchemy to avoid stressing the logging library. # See crbug.com/1044343. logging.getLogger('sqlalchemy').setLevel(logging.ERROR) if utils.is_local(): return _initialize_cloud_clients() global _default_logger _default_logger = _log_client.logger(name) default_extras = {} if default_extras is None else default_extras _set_instance_name(default_extras) _set_experiment(default_extras) global _default_extras _default_extras.update(default_extras)
def error(message, *args, extras=None, logger=None): """Logs |message| to stackdriver logging and error reporting (including exception if there was one.""" @retry.wrap(NUM_RETRIES, RETRY_DELAY, 'common.logs.error._report_error_with_retries') def _report_error_with_retries(message): _error_reporting_client.report(message) if utils.is_local(): logging.error(message, *args) return if not any(sys.exc_info()): _report_error_with_retries(message % args) log(logger, logging.ERROR, message, *args, extras=extras) return # I can't figure out how to include both the message and the exception # other than this having the exception message preceed the log message # (without using private APIs). _report_error_with_retries(traceback.format_exc() + '\nMessage: ' + message % args) extras = {} if extras is None else extras extras['traceback'] = traceback.format_exc() log(logger, logging.ERROR, message, *args, extras=extras)
def _report_error_with_retries(message): if utils.is_local(): return _error_reporting_client.report(message)