def load_plugin(name): try: __plugins__.append(__import__(name)) except BaseException: log.get_logger(__name__).error("Error importing plugin `%s`", name) log.log_traceback(__name__) log.get_logger(__name__).info("Loaded plugin: %s", name)
def log_delta(req, msg, delta=None): ''' Log delta between now and entry of route context ''' if delta is None: ctx = context.ctx_get() delta = time.time() - ctx['tracer_context_start'] log.get_logger(req.robj.fname).debug("%.4f: %s(%r,%r): %s", delta, req.robj.fname, req.args, req.kwargs, msg)
def handler(req): req.ret = message if message else req.exception.message lmsg = log_msg if log_msg else "%s: %s" % (req.exception.__class__.__name__, req.exception.message) log.get_logger(__name__).error(lmsg) req.response.status = code if callback is not None: callback(req)
def filter(self, args): ''' Filter out unknown arguments. @param args: The argument C{dict} passed in by the user. @return: Filtered argument C{dict} ''' out = dict(args) unknown_args = [(k, out.pop(k))[0] for k in args if k not in self.args] if unknown_args: log.get_logger(__name__).debug( "Filtered arguments: %r" % unknown_args) return out
def end_trace(fname): ''' Disable profiler ''' ctx = context.ctx_get() profiler = ctx.get('tracer_profiler') if profiler is None: return logger = log.get_logger(__name__) profiler.disable() logger.debug("Profiler disabled") lines = ctx['tracer_lines'] sort = ctx['tracer_sort'].split(',') trace_buf = BytesIO() stats = pstats.Stats(profiler, stream=trace_buf).sort_stats(*sort) stats.print_stats(lines) logger.debug("Dumping profiling data") log.get_logger(fname).debug(trace_buf.getvalue())
def trace_route_context(ctx): ''' Trace timing of route context setup ''' req = ctx['request'] if not trace_enabled(req): return logger = log.get_logger(req.robj.fname) logger.debug("Request tracing enabled") ctx_time = time.time() profiler = cProfile.Profile() lines = int(req.request.headers.get('Woodstove-Trace-Lines', '25')) sort = req.request.headers.get('Woodstove-Trace-Sort', 'cumulative') ctx.update({"tracer_context_start": ctx_time, "tracer_profiler": profiler, "tracer_lines": lines, "tracer_sort": sort}) logger.debug("Starting profiler") profiler.enable() log_delta(req, "Route context entered", 0.0) log.get_logger(__name__).debug("%f: Route context entered", time.time())
def call_hooks(table, hook, *args, **kwargs): logger = get_logger(__name__) table = get_hook_table(table) for callback in table.get(hook, []): try: callback(*args, **kwargs) except exceptions.HookStopException: logger.info("Stopping hook processing") break except: logger.error("exception")
def check(self, args, func=None, check_type=True): ''' Check L{args} for valid value for this argument. @param args: The arguments passed by the user. @keyword func: Function calling validate. @keyword check_type: Should types be verified. @raise ArgumentException: If any validation hook fails. ''' try: arg = args[self.key] except KeyError: try: if func not in self.required_funcs: return args except TypeError: pass if self.optional: if self.default is not __sentinel__: args[self.key] = self.default return args raise exceptions.ArgumentException('Missing required argument: %s' % self.key) if self.cast is not None: try: arg = self.cast(arg) except Exception: log.get_logger(__name__).debug(traceback.format_exc()) msg = 'Unable to cast argument %s to %r' % (self.key, self.cast) raise exceptions.ArgumentException(msg) for hook, opts in six.iteritems(self.hooks): larg = '*' * 10 if self.censor else arg log.get_logger(__name__).debug("Calling hook %r(%r, %r, %r, %r)" % ( hook, self, larg, func, check_type)) if opts['funcs'] and func not in opts['funcs']: continue try: opts['check_type'] = check_type args[self.key] = hook(self, arg, func, opts) except exceptions.ArgumentException: raise except Exception: log.get_logger(__name__).error("Exception in hook %r: %s" % ( hook, traceback.format_exc())) raise exceptions.ArgumentException( "Error in argument validation") return args
def duration_exit_hook(req): ''' Log request duration ''' log.get_logger(__name__).debug("Request handled in %f seconds", time.time()-req.start)