-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Replace print
calls with Logger.log
#86
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import logging | ||
|
||
from .pretty_term import OutStream | ||
from ..workflow import FunctionNode | ||
from inspect import Parameter | ||
import sys | ||
|
||
logger = logging.getLogger("noodles") | ||
|
||
|
||
def _format_arg_list(a, v): | ||
if len(a) == 0: | ||
|
@@ -32,9 +36,8 @@ def __init__(self, error_filter=None): | |
|
||
def print_message(self, key, msg): | ||
if key in self.jobs: | ||
print("{1:12} | {2}".format( | ||
key, '['+msg.upper()+']', self.jobs[key]['name']), | ||
file=sys.stderr) | ||
logger.info("{1:12} | {2}".format( | ||
key, '['+msg.upper()+']', self.jobs[key]['name'])) | ||
|
||
def add_job(self, key, name): | ||
self.jobs[key] = {'name': name} | ||
|
@@ -50,21 +53,20 @@ def report(self): | |
self.out << "[ERROR!]\n\n" | ||
|
||
for job, e in self.errors: | ||
msg = 'ERROR ' | ||
if 'display' in job.hints: | ||
msg += job.hints['display'].format( | ||
msg = job.hints['display'].format( | ||
**job.bound_args.arguments) | ||
else: | ||
msg += 'calling {} with {}'.format( | ||
msg = 'calling {} with {}'.format( | ||
job.foo.__name__, dict(job.bound_args.arguments) | ||
) | ||
|
||
print(msg) | ||
logger.error(msg) | ||
err_msg = self.error_filter(e) | ||
if err_msg: | ||
print(err_msg) | ||
logger.error(err_msg) | ||
else: | ||
print(e) | ||
logger.error(e) | ||
|
||
def __call__(self, msg): | ||
key, status, data, err = msg | ||
|
@@ -96,7 +98,10 @@ def __exit__(self, exc_type, exc_val, exc_tb): | |
self.out << "\n" << "User interrupt detected, abnormal exit.\n" | ||
return True | ||
|
||
print("Internal error encountered. Contact the developers.") | ||
logger.critical( | ||
"Internal error encountered. Contact the developers.", | ||
exc_info=exc_val, | ||
) | ||
Comment on lines
+101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example of a logging message where a traceback will be included. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example of such a tracebacked logging output from https://github.com/SCM-NV/nano-qmflows/runs/6536779272?check_suite_focus=true: Click to expand logging output
|
||
return False | ||
|
||
self.report() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
> python3 -m noodles.worker -online [-use <worker>] | ||
""" | ||
|
||
import logging | ||
import argparse | ||
import sys | ||
import uuid | ||
|
@@ -34,6 +35,8 @@ | |
|
||
from .run.remote.io import (JSONObjectReader, JSONObjectWriter) | ||
|
||
logger = logging.getLogger("noodles") | ||
|
||
|
||
def run_online_mode(args): | ||
"""Run jobs. | ||
|
@@ -46,9 +49,9 @@ def run_online_mode(args): | |
|
||
Messages can be encoded as either JSON or MessagePack. | ||
""" | ||
print("\033[47;30m Netherlands\033[48;2;0;174;239;37m▌" | ||
"\033[38;2;255;255;255me\u20d2Science\u20d2\033[37m▐" | ||
"\033[47;30mcenter \033[m Noodles worker", file=sys.stderr) | ||
logger.info("\033[47;30m Netherlands\033[48;2;0;174;239;37m▌" | ||
"\033[38;2;255;255;255me\u20d2Science\u20d2\033[37m▐" | ||
"\033[47;30mcenter \033[m Noodles worker") | ||
|
||
if args.n == 1: | ||
registry = look_up(args.registry)() | ||
|
@@ -72,7 +75,7 @@ def run_online_mode(args): | |
if isinstance(msg, JobMessage): | ||
key, job = msg | ||
elif msg is EndOfWork: | ||
print("received EndOfWork, bye", file=sys.stderr) | ||
logger.info("received EndOfWork, bye") | ||
sys.exit(0) | ||
elif isinstance(msg, tuple): | ||
key, job = msg | ||
|
@@ -88,17 +91,19 @@ def run_online_mode(args): | |
os.chdir("noodles-{0}".format(key.hex)) | ||
|
||
if args.verbose: | ||
print("worker: ", | ||
job.foo.__name__, | ||
job.bound_args.args, | ||
job.bound_args.kwargs, | ||
file=sys.stderr, flush=True) | ||
logger.info( | ||
f"worker: {job.foo.__name__} {job.bound_args.args} {job.bound_args.kwarg}" | ||
) | ||
for handler in logger.handlers: | ||
handler.flush() | ||
Comment on lines
+97
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not even 100% sure if flushing is required for the |
||
|
||
with redirect_stdout(sys.stderr): | ||
result = run_job(key, job) | ||
|
||
if args.verbose: | ||
print("result: ", result.value, file=sys.stderr, flush=True) | ||
logger.info(f"result: {result.value}") | ||
for handler in logger.handlers: | ||
handler.flush() | ||
|
||
if args.jobdirs: | ||
# parent directory | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to manually prefix
ERROR
if it's already included via the logging level.