' - {duration_ms:>8.3f}ms' ' - {parent_depth_indent}{end_message}') begin_fmt = ('{status_char}+{import_delta_s}' ' --------------' ' {parent_depth_indent}{begin_message}') stderr_fmtr = SensibleFormatter(fmt, begin=begin_fmt) stderr_emtr = StreamEmitter('stderr') stderr_filter = SensibleFilter(success='info', failure='debug', exception='debug') stderr_sink = SensibleSink(formatter=stderr_fmtr, emitter=stderr_emtr, filters=[stderr_filter]) chert_log.add_sink(stderr_sink) try: from lithoxyl.emitters import SyslogEmitter except Exception: pass else: syslog_filter = SensibleFilter(success='critical', failure='critical', exception='critical') syslog_emt = SyslogEmitter('chert') syslog_sink = SensibleSink(formatter=stderr_fmtr, emitter=syslog_emt, filters=[syslog_filter]) if os.getenv('CHERT_SYSLOG'): chert_log.add_sink(syslog_sink)
class LoggingMiddleware(Middleware): provides = ( 'api_log', 'api_act', ) def __init__(self, log_path, act_name='API'): self.log_path = os.path.abspath(log_path) root, ext = os.path.splitext(self.log_path) self.exc_log_path = '%s.exc%s' % (root, ext) self.act_name = act_name self._setup_api_log() def request(self, next, request, _route): try: act_name = '%s%s' % (request.method, _route.pattern) with self.api_log.critical(act_name) as api_act: # basic redacted url api_act['path'] = request.path api_act.data_map.update(request.args.items()) try: ret = next(api_act=api_act, api_log=self.api_log) except clastic.errors.BadRequest as br: api_act.data_map.update(br.to_dict()) api_act.failure() ret = br api_act['code'] = ret.status_code except Exception: exc_info = ExceptionInfo.from_current() text = u'\n\n' + exc_info.get_formatted() + '\n\n' self.exc_log_file_obj.write(text.encode('utf8')) self.exc_log_file_obj.flush() raise return ret def endpoint(self, next, api_act): ret = next() try: api_act['username'] = ret['user'].username except Exception: pass return ret def _setup_api_log(self): self.log_file_obj = open(self.log_path, 'ab') self.exc_log_file_obj = open(self.exc_log_path, 'ab') self.api_log = Logger(self.act_name.lower() + '_log') self._api_fmtr = SensibleFormatter(API_LOG_FMT) self._api_emtr = StreamEmitter(self.log_file_obj) self._api_fltr = SensibleFilter(success='info', failure='debug', exception='debug') self._api_sink = SensibleSink(formatter=self._api_fmtr, emitter=self._api_emtr, filters=[self._api_fltr]) self.api_log.add_sink(self._api_sink) self._exc_emtr = StreamEmitter(self.exc_log_file_obj) self._exc_fltr = SensibleFilter(success=None, failure=None, exception='info') self._exc_sink = SensibleSink(formatter=self._api_fmtr, emitter=self._exc_emtr, filters=[self._exc_fltr]) self.api_log.add_sink(self._exc_sink)
' - {parent_depth_indent}{end_message}') begin_fmt = ('{status_char}+{import_delta_s}' ' --------------' ' {parent_depth_indent}{begin_message}') stderr_fmtr = SensibleFormatter(fmt, begin=begin_fmt) stderr_emtr = StreamEmitter('stderr') stderr_filter = SensibleFilter(success='info', failure='debug', exception='debug') stderr_sink = SensibleSink(formatter=stderr_fmtr, emitter=stderr_emtr, filters=[stderr_filter]) chert_log.add_sink(stderr_sink) try: from lithoxyl.emitters import SyslogEmitter except Exception: pass else: syslog_filter = SensibleFilter(success='critical', failure='critical', exception='critical') syslog_emt = SyslogEmitter('chert') syslog_sink = SensibleSink(formatter=stderr_fmtr, emitter=syslog_emt, filters=[syslog_filter]) if os.getenv('CHERT_SYSLOG'): chert_log.add_sink(syslog_sink)
def __init__(self, filepath, encoding=None, **kwargs): self.encoding = encoding super(FixedFileEmitter, self).__init__(filepath, encoding, **kwargs) CUR_PATH = os.path.dirname(os.path.abspath(__file__)) LOG_FILE_PATH = os.path.join(CUR_PATH, 'logs', 'update_log.txt') tlog = Logger('toplog') file_fmt = SensibleFormatter('{status_char}{iso_end_local_noms_notz} - {duration_s}s - {action_name} - {event_message}') file_emt = FixedFileEmitter(LOG_FILE_PATH) file_filter = SensibleFilter(success='critical', failure='info', exception='debug') file_sink = SensibleSink(formatter=file_fmt, emitter=file_emt, filters=[file_filter]) tlog.add_sink(file_sink) def set_debug(enable=True): if not enable: raise NotImplementedError() dbg_fmtr = file_fmt dbg_emtr = StreamEmitter('stderr') dbg_sink = SensibleSink(formatter=dbg_fmtr, emitter=dbg_emtr) tlog.add_sink(dbg_sink)
return for i in range(20): one_two() import os print os.getpid() class CommentSink(object): def on_comment(self, comment_event): print comment_event, comment_event.message # import pdb;pdb.set_trace() def emit_cur_time_hook(logger): logger.comment('simpler heartbeats for a simpler time') log.preflush_hooks.append(emit_cur_time_hook) log.add_sink(CommentSink()) # log.flush() log.comment('{} {hah}', 'hah!', hah='HAH!') import pdb;pdb.set_trace()
from lithoxyl import Logger, SensibleSink, Formatter, StreamEmitter from lithoxyl.filters import ThresholdFilter tlog = Logger('toplog') stderr_fmt = Formatter('{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}') stderr_emt = StreamEmitter('stderr') stderr_filter = ThresholdFilter(success='debug', failure='debug', exception='debug') stderr_sink = SensibleSink(formatter=stderr_fmt, emitter=stderr_emt, filters=[stderr_filter]) # on=['begin', 'complete']) # TODO: clunk tlog.add_sink(stderr_sink) class DevDebugSink(object): # TODO: configurable max number of traceback signatures, after # which exit/ignore? def __init__(self, reraise=False, post_mortem=False): self.reraise = reraise self.post_mortem = post_mortem #def on_complete(self, record): # some conditions and a pdb perhaps def on_exception(self, record, exc_type, exc_obj, exc_tb): if self.post_mortem:
raise ValueError("oh no, one of those") return for i in range(20): one_two() import os print os.getpid() class CommentSink(object): def on_comment(self, comment_event): print comment_event, comment_event.message # import pdb;pdb.set_trace() def emit_cur_time_hook(logger): logger.comment('simpler heartbeats for a simpler time') log.preflush_hooks.append(emit_cur_time_hook) log.add_sink(CommentSink()) # log.flush() log.comment('{} {hah}', 'hah!', hah='HAH!') import pdb pdb.set_trace()
import os from lithoxyl import Logger, SensibleSink, Formatter, StreamEmitter from lithoxyl.filters import ThresholdFilter tlog = Logger("toplog") stderr_fmt = Formatter("{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}") stderr_emt = StreamEmitter("stderr") stderr_filter = ThresholdFilter(success="debug", failure="debug", exception="debug") stderr_sink = SensibleSink(formatter=stderr_fmt, emitter=stderr_emt, filters=[stderr_filter]) # on=['begin', 'complete']) # TODO: clunk tlog.add_sink(stderr_sink) class DevDebugSink(object): # TODO: configurable max number of traceback signatures, after # which exit/ignore? def __init__(self, reraise=False, post_mortem=False): self.reraise = reraise self.post_mortem = post_mortem # def on_complete(self, record): # some conditions and a pdb perhaps def on_exception(self, record, exc_type, exc_obj, exc_tb): if self.post_mortem: import pdb pdb.post_mortem()
def __init__(self, filepath, encoding=None, **kwargs): self.encoding = encoding super(FixedFileEmitter, self).__init__(filepath, encoding, **kwargs) CUR_PATH = os.path.dirname(os.path.abspath(__file__)) LOG_FILE_PATH = os.path.join(CUR_PATH, 'logs', 'update_log.txt') tlog = Logger('toplog') file_fmt = Formatter('{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}') file_emt = FixedFileEmitter(LOG_FILE_PATH) file_filter = ThresholdFilter(success='critical', failure='info', exception='debug') file_sink = SensibleSink(formatter=file_fmt, emitter=file_emt, filters=[file_filter]) tlog.add_sink(file_sink) def set_debug(enable=True): if not enable: raise NotImplementedError() dbg_fmtr = file_fmt dbg_emtr = StreamEmitter('stderr') dbg_sink = SensibleSink(formatter=dbg_fmtr, emitter=dbg_emtr) tlog.add_sink(dbg_sink)
from lithoxyl import (Logger, StreamEmitter, SensibleSink, SensibleFilter, SensibleFormatter) from lithoxyl.sinks import DevDebugSink # import lithoxyl; lithoxyl.get_context().enable_async() script_log = Logger('dev_script') fmt = ('{status_char}+{import_delta_s}' ' - {duration_ms:>8.3f}ms' ' - {parent_depth_indent}{end_message}') begin_fmt = ('{status_char}+{import_delta_s}' ' --------------' ' {parent_depth_indent}{begin_message}') stderr_fmtr = SensibleFormatter(fmt, begin=begin_fmt) stderr_emtr = StreamEmitter('stderr') stderr_filter = SensibleFilter(success='info', failure='debug', exception='debug') stderr_sink = SensibleSink(formatter=stderr_fmtr, emitter=stderr_emtr, filters=[stderr_filter]) script_log.add_sink(stderr_sink) dds = DevDebugSink(post_mortem=bool(os.getenv('ENABLE_PDB'))) script_log.add_sink(dds)
begin= '{status_char}{iso_begin_notz} - -- - {action_name} - {begin_message}') def build_stream_sink(stream): emt = StreamEmitter(stream) file_filter = SensibleFilter(success='info', failure='debug', exception='debug') file_sink = SensibleSink(formatter=file_fmt, emitter=emt, filters=[file_filter]) return file_sink default_file_sink = build_stream_sink(open(LOG_PATH, 'a')) tlog.add_sink(default_file_sink) stderr_fmt = file_fmt stderr_emt = StreamEmitter('stderr') stderr_filter = SensibleFilter(success='critical', failure='debug', exception='debug') stderr_sink = SensibleSink(formatter=stderr_fmt, emitter=stderr_emt, filters=[stderr_filter]) def enable_debug_log(): tlog.add_sink(stderr_sink)