def test_readme(): import mylogging mylogging.set_warnings(debug=1) mylogging.warn( "Hessian matrix copmputation failed for example", caption="RuntimeError on model x", ) print( "We can log / warn tracebacks from expected errors and continue runtime." ) try: print(10 / 0) except ZeroDivisionError: mylogging.traceback("Maybe try to use something different than 0.") print( "Info will not trigger warning, but just print to console (but follows the rule in set_warnings(debug))." ) mylogging.info("I am interesting info")
def test_readme_configs(): import mylogging mylogging.config.COLOR = ( 0 # Turn off colorization on all functions to get rid of weird symbols ) mylogging.info("Not color")
def kill_process(name): """ Kill the process with the specified name. """ for p in psutil.process_iter(): cmd = " ".join(p.cmdline()) if ('java' in cmd) and (name in cmd): log.info(cmd) log.info(p.pid) p.kill() break
def kill_process(name): """ Kill the process with the specified name. """ for p in psutil.process_iter(): cmd = " ".join(p.cmdline()) if ('java' in cmd) and (name in cmd): log.info(cmd) log.info(p.pid) p.kill() break
def main(): while True: try: if is_process_running(PROCESS_NAME): sleep(WAIT) else: log.info("vuze is not running, restarting") sleep(10) os.system("./start_vuze.sh") sleep(10) except KeyboardInterrupt: kill_process(PROCESS_NAME) log.info("interrupted by the user") break # print()
def main(): while True: try: if is_process_running(PROCESS_NAME): sleep(WAIT) else: log.info("vuze is not running, restarting") sleep(10) os.system("./start_vuze.sh") sleep(10) except KeyboardInterrupt: kill_process(PROCESS_NAME) log.info("interrupted by the user") break # print()
def test_logs(): mylogging.config.TO_FILE = "delete.log" errors = [] def check_log(): with open("delete.log") as log: log_content = log.read() os.remove("delete.log") if log_content: return True else: return False mylogging.info( "Hessian matrix copmputation failed for example", caption="RuntimeError on model x", ) if not check_log(): errors.append("Info not created") mylogging.warn( "Hessian matrix copmputation failed for example", caption="RuntimeError on model x", ) if not check_log(): errors.append("Warning not created") try: print(10 / 0) except Exception: mylogging.traceback("Maybe try to use something different than 0") if not check_log(): errors.append("Traceback not created") for i in [info_outside, warn_outside, traceback_outside]: i("Message") if not check_log(): errors.append("Outside function not working")
def process(): """ Take a task, execute it, then remove it. Check the task directory. If there are no tasks, return None (and the daemon will stop). If there is a task, execute it and then remove it. In this case return True. """ batches = [t for t in os.listdir(cfg.QDIR) if len(t)==4 and t.isdigit()] if len(batches) == 0: return None # else first = sorted(batches)[0] fpath = "{qdir}/{fname}".format(qdir=cfg.QDIR, fname=first) info('start: process {fname}:'.format(fname=first)) with open(fpath) as f: for line in f: info(" " + line.rstrip("\n")) # sleep(10) # used for tests os.system(fpath) info('end: process {fname}'.format(fname=first)) os.unlink(fpath) return True
def test_readme_to_file(): import mylogging mylogging.config.TO_FILE = "log.log" # You can use relative (just log.log) # Then it's the same mylogging.warn( "Hessian matrix copmputation failed for example", caption="RuntimeError on model x", ) try: print(10 / 0) except ZeroDivisionError: mylogging.traceback("Maybe try to use something different than 0.") mylogging.info("I am interesting info") mylogging.config.TO_FILE = False os.remove("log.log")
def check_if_files_exist(): while True: mylogging.info("check file.") file_to_monitor = file_queue.get(True) mylogging.info("Waiting for " + file_to_monitor) while not os.path.isfile(file_to_monitor): time.sleep(5) file_returned = file_to_monitor mylogging.info("Got " + file_returned) notify_queue.put(file_returned)
def info_outside(message): mylogging.info(message)
while not os.path.isfile(file_to_monitor): time.sleep(5) file_returned = file_to_monitor mylogging.info("Got " + file_returned) notify_queue.put(file_returned) if __name__ == "__main__": t = Thread(target=check_if_files_exist, args=()) t.setDaemon(True) t.start() retry = 0 is_success = False sleep_between_retry = 5 while retry <= max_retry and not is_success: mylogging.info("Try again") if os.path.isfile(source_file): mylogging.info("file found!") file_queue.put(source_file) time.sleep(sleep_between_retry) is_success = True retry = retry + 1 notify_file = notify_queue.get(True, 3) count = 0 with open(notify_file) as fp: Lines = fp.readlines() for line in Lines: count += 1 mylogging.info("Line{}: {}".format(count, line.strip())) mylogging.info("finish")