def startService(self): self.stdlib_cleanup = _stdlib_logging_to_eliot_configuration( getLogger()) self.twisted_observer = _TwistedLoggerToEliotObserver() globalLogPublisher.addObserver(self.twisted_observer) add_destinations(*self.destinations) return Service.startService(self)
def results_or_logs(results): if results is None: # Error, check the logs print("Error. Please check the following log:") eliot.add_destinations(log_to_stdout) raise SystemExit(1) return results
def startService(self): if self.task_fields: self.task = start_task(**self.task_fields) self.task.__enter__() if self.capture_logs: self.stdlib_cleanup = _stdlib_logging_to_eliot_configuration( getLogger()) self.twisted_observer = _TwistedLoggerToEliotObserver() globalLogPublisher.addObserver(self.twisted_observer) add_destinations(*self.destinations) return Service.startService(self)
def _setUp(self): self.logs = [] add_destinations(self.logs.append) self.addCleanup(lambda: remove_destination(self.logs.append)) self.addDetail( self.LOG_DETAIL_NAME, Content( UTF8_TEXT, # Safeguard the logs against _tearDown. Capture the list # object in the lambda's defaults. lambda logs=self.logs: [_eliottree(logs).encode("utf-8")], ), )
def __init__(self, filename, num_logs=4, auto=True, max_bytes=5000000): ''' If auto rotation is disabled, then the logs will only be rotated 1) At startup (class init) 2) By explicitly calling to do_rotate_check(). ''' self.filename = filename self.file = open(filename, "a") self.max_bytes = max_bytes self.num_logs = num_logs self.auto = auto self.do_rotate_check() # Register this handler as an eliot logging destination eliot.add_destinations(self.write)
def setup_live_logging(logging_settings, live_settings): event_type = logging_settings.get("event_type", "dda_log") level = logging_settings.get("level", default_level) username = live_settings.get("username") password = live_settings.get("password") logging_url = f"{live_settings['url']}{live_settings['rest_input']}" global log_level log_level = level log_function = async_event_sender(live_settings) if event_type and logging_url and username and password: add_destinations( partial(log_to_live, log_function=log_function, event_type=event_type, min_level=level) )
def test_tree(self): """ ``_eliottree`` returns a ``bytes`` string containing a rendered tree of Eliot actions and messages. """ events = [] add_destinations(events.append) self.addCleanup(lambda: remove_destination(events.append)) with start_action(action_type=u"foo"): pass # I don't know exactly what the tree rendering looks like. That's why # I'm using eliot-tree! So this assertion is sort of lame. self.assertThat( _eliottree(events), Contains(u"foo/1 \N{RIGHTWARDS DOUBLE ARROW} started"), )
if "progress" not in message: return done = message["progress"]["done"] total = message["progress"]["total"] percentage = int(round(float(done) / float(total) * 100)) channel_layer = get_channel_layer() root_task_uuid = str(message["task_uuid"]) tree = [root_task_uuid] + message["task_level"] combined_task_uuid = "--".join([str(x) for x in tree]) progress_task_uuid = "--".join([str(x) for x in tree[:-1]]) message = { "type": "progress.update", # 'task_uuid': root_task_uuid, "progress_uuid": progress_task_uuid, "progress_name": "whatever", "progress_percentage": percentage, "action_type": message.get("action_type", ""), "message_type": message.get("message_type", ""), } print(message) async_to_sync(channel_layer.group_send)(group=PROGRESS_GROUP_NAME, message=message) # add_global_fields( # process_id='{}:{}'.format(sys.argv[0], os.getpid()), # ) add_destinations(stdout) add_destinations(send_to_progress_group) to_file(open("/app/tmp/timelapse.log", "ab"))
if len(tests_names) > 1: # Multiple Apollo tests modules loaded, test name unknown. now = datetime.now().strftime("%y-%m-%d_%H:%M:%S") test_name = f"apollo_run_{now}" else: # Single Apollo module loaded, test name known. test_name = f"{tests_names.pop()}_{storage_type}" # Create logs directory if not exist if not os.path.isdir("logs"): os.mkdir("logs") test_name = f"logs/{test_name}.log" if os.path.isfile(test_name): # Clean logs if file already exist open(test_name, "w").close() # Set the log file path to_file(open(test_name, "a")) # Set logs to the console def stdout(message): if message is not "": print(message) add_destinations(stdout) set_file_destination()
def startService(self): self.stdlib_cleanup = _stdlib_logging_to_eliot_configuration(getLogger()) self.twisted_observer = _TwistedLoggerToEliotObserver() globalLogPublisher.addObserver(self.twisted_observer) add_destinations(*self.destinations) return Service.startService(self)
""" Write some logs to journald. """ from __future__ import print_function from eliot import Message, start_action, add_destinations from eliot.journald import JournaldDestination add_destinations(JournaldDestination()) def divide(a, b): with start_action(action_type="divide", a=a, b=b): return a / b print(divide(10, 2)) Message.log(message_type="inbetween") print(divide(10, 0))
) parser.add_argument("--live_url", dest="live_url", required=True, help="The url Intelie Live") parser.add_argument("--username", dest="username", required=True, help="Live username") parser.add_argument("--password", dest="password", required=True, help="Live password") return parser.parse_args(argv[1:]) def build_settings(args): return {"live": {"url": args.live_url, "username": args.username, "password": args.password}} if __name__ == "__main__": """ Displays the list of chat rooms on an Intelie Live instance """ args = parse_arguments(sys.argv) settings = build_settings(args) rooms_list = list_rooms(settings) if rooms_list is None: # Error, check the logs print("Error. Please check the following log:") eliot.add_destinations(log_to_stdout) else: print(f"List of chat rooms on {args.live_url}") template = "- Room {id}, name: {name}, users: {users}" for item in rooms_list: print(template.format(**item))
import sys import requests from eliot import start_action, add_destinations, log_call from eliottree import render_tasks, tasks_from_iterable messages = [] def collect_messages(message): messages.append(message) add_destinations(collect_messages) @log_call def get_some_data(url): requests.get(url) try: with start_action(action_type='SomeOuterAction', x=123) as action: action.log(message_type='my_dummy_message', text='something is about to happen') url = 'http://4ut23y74283ty872y3t47823t.com/' with start_action(action_type='SomeInnerAction', url=url): get_some_data(url) except: render_tasks(sys.stdout.write, tasks_from_iterable(messages),
def new_destination(self, file): add_destinations(AsyncFileDestination(file, self))
import random import functools import time from celery import Celery, Task from celery.exceptions import SoftTimeLimitExceeded, MaxRetriesExceededError import celery.signals from eliot import start_action, add_destinations, to_file, Action, Message to_file(open('a.log', 'wb')) add_destinations(print) app = Celery(__name__) @celery.signals.setup_logging.connect() def _setup_logging(*args, **kwargs): pass class TaskFailure(Exception): """ Custom exception to raise to request retry. """ class CeleryTask(Task): def __call__(self, *args, **kwargs): try: try: super().__call__(*args, **kwargs) except SoftTimeLimitExceeded:
""" Write some logs to journald. """ from __future__ import print_function from eliot import log_message, start_action, add_destinations from eliot.journald import JournaldDestination add_destinations(JournaldDestination()) def divide(a, b): with start_action(action_type="divide", a=a, b=b): return a / b print(divide(10, 2)) log_message(message_type="inbetween") print(divide(10, 0))
except IndexError: # Fallback to Let's Encrypt staging server. acme_url = 'https://acme-staging-v02.api.letsencrypt.org/directory' requested_domains = [d.strip().decode('utf-8') for d in sys.argv[1].split(',')] print('\n\n') print('-' * 70) print('Using ACME at %s' % (acme_url, )) print('Managing a single certificate for %s' % (requested_domains, )) print('HTTP-01 responser at ' 'http://localhost:%s/.well-known/acme-challenge/test.txt' % (HTTP_O1_PORT, )) print('-' * 70) print('\n\n') add_destinations( EliotTreeDestination(colorize=True, colorize_tree=True, human_readable=True)) def main(reactor): d = get_things_done() d.addErrback(eb_general_failure) return d if __name__ == '__main__': task.react(main)